Skip to content

Commit 8d16266

Browse files
authored
Use indoc for multiline test strings (#22723)
Replace embedded newline escapes in multiline test strings with `indoc!` and `formatdoc!` so fixtures and expected output are easier to read. Update scenario generation to emit the same style.
1 parent 502f93c commit 8d16266

65 files changed

Lines changed: 1188 additions & 376 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/uv-build-backend/src/lib.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,10 @@ mod tests {
10081008
.unwrap();
10091009
fs_err::write(
10101010
src.path().join("license.txt"),
1011-
"Copy carefully.\nSincerely, the authors",
1011+
indoc! {"
1012+
Copy carefully.
1013+
Sincerely, the authors
1014+
"},
10121015
)
10131016
.unwrap();
10141017

‎crates/uv-client/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ anyhow = { workspace = true }
9090
http-body-util = { workspace = true }
9191
hyper = { workspace = true }
9292
hyper-util = { workspace = true }
93+
indoc = { workspace = true }
9394
insta = { workspace = true }
9495
rcgen = { workspace = true }
9596
regex = { workspace = true, features = ["perf", "unicode"] }

‎crates/uv-client/tests/it/ssl_certs.rs‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
55
use std::str::FromStr;
66

77
use anyhow::Result;
8+
use indoc::{formatdoc, indoc};
89
use rcgen::CustomExtension;
910
use temp_env::async_with_vars;
1011
use tempfile::{NamedTempFile, TempDir};
@@ -78,11 +79,13 @@ impl TestCertificate {
7879
let client_cert_path = temp_dir.path().join("client.pem");
7980
fs_err::write(
8081
&client_cert_path,
81-
format!(
82-
"{}\n{}",
82+
formatdoc! {"
83+
{}
84+
{}
85+
",
8386
client.public.pem(),
8487
client.private.serialize_pem()
85-
),
88+
},
8689
)?;
8790

8891
Ok(Self {
@@ -99,7 +102,10 @@ impl TestCertificate {
99102
let mut file = NamedTempFile::new().unwrap();
100103
write!(
101104
file,
102-
"{}\n{}",
105+
indoc! {"
106+
{}
107+
{}
108+
"},
103109
self.ca.public.pem(),
104110
self.server.public.pem()
105111
)
@@ -126,7 +132,13 @@ impl TestCertificate {
126132
let dir = TempDir::new().unwrap();
127133
fs_err::write(
128134
dir.path().join("bundle.pem"),
129-
format!("{}\n{}", self.ca.public.pem(), self.server.public.pem()),
135+
formatdoc! {"
136+
{}
137+
{}
138+
",
139+
self.ca.public.pem(),
140+
self.server.public.pem()
141+
},
130142
)
131143
.unwrap();
132144
dir
@@ -719,7 +731,10 @@ async fn test_ssl_cert_file_bundle_ignores_invalid_trust_anchor() -> Result<()>
719731
let mut bundle = NamedTempFile::new()?;
720732
write!(
721733
bundle,
722-
"{}\n{}",
734+
indoc! {"
735+
{}
736+
{}
737+
"},
723738
invalid_cert.ca.public.pem(),
724739
valid_cert.ca.public.pem()
725740
)?;

‎crates/uv-dev/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ walkdir = { workspace = true }
7070
yaml_serde = { version = "0.10.7", default-features = false, features = ["std"] }
7171

7272
[dev-dependencies]
73+
indoc = { workspace = true }
7374
insta = { workspace = true }
7475

7576
[lib]

‎crates/uv-dev/src/generate_preview_features_reference.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fn render(feature: PreviewFeature, description: &str) -> String {
6363

6464
#[cfg(test)]
6565
mod tests {
66+
use indoc::indoc;
6667
use insta::assert_snapshot;
6768
use uv_preview::PreviewFeature;
6869

@@ -73,7 +74,19 @@ mod tests {
7374
assert_snapshot!(
7475
render(
7576
PreviewFeature::Pylock,
76-
"A first paragraph,\ncontinued on the next line.\n\nA second paragraph:\n\n- A nested list item.\n- Another nested list item.\n\n```toml\nkey = \"value\"\n```",
77+
indoc! {r#"
78+
A first paragraph,
79+
continued on the next line.
80+
81+
A second paragraph:
82+
83+
- A nested list item.
84+
- Another nested list item.
85+
86+
```toml
87+
key = "value"
88+
```"#
89+
},
7790
),
7891
@r##"
7992
### `pylock` {#pylock}

‎crates/uv-dev/src/generate_scenarios.rs‎

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ fn render_compile(output: &mut String, cases: &[&ScenarioCase]) -> Result<()> {
384384
output.push_str("use anyhow::Result;\n");
385385
output.push_str("use assert_cmd::assert::OutputAssertExt;\n");
386386
output.push_str("use assert_fs::fixture::{FileWriteStr, PathChild};\n");
387+
if cases
388+
.iter()
389+
.any(|case| case.scenario.root.requires.len() > 1)
390+
{
391+
output.push_str("use indoc::indoc;\n");
392+
}
387393
output.push_str("use predicates::prelude::predicate;\n\n");
388394
output.push_str("use uv_static::EnvVars;\n");
389395
output.push_str("use uv_test::packse::PackseServer;\n");
@@ -451,7 +457,21 @@ fn render_compile_case(output: &mut String, case: &ScenarioCase) -> Result<()> {
451457
output.push('\n');
452458
output.push_str(" let requirements_in = context.temp_dir.child(\"requirements.in\");\n");
453459
let requirements = compile_requirements(&case.scenario.root.requires);
454-
writeln!(output, " requirements_in.write_str({requirements:?})?;").unwrap();
460+
if case.scenario.root.requires.len() > 1 {
461+
let requirements = requirements
462+
.lines()
463+
.map(|line| {
464+
let quoted = format!("{line:?}");
465+
format!(" {}", &quoted[1..quoted.len() - 1])
466+
})
467+
.join("\n");
468+
writeln!(
469+
output,
470+
" requirements_in.write_str(indoc! {{\"\n{requirements}\n \"}})?;"
471+
)?;
472+
} else {
473+
writeln!(output, " requirements_in.write_str({requirements:?})?;")?;
474+
}
455475
output.push('\n');
456476
render_expected_explanation(output, &case.scenario, " // ");
457477
output.push_str(
@@ -929,6 +949,8 @@ fn requirement_specifiers(requirement: &Requirement) -> Option<&uv_pep440::Versi
929949

930950
#[cfg(test)]
931951
mod tests {
952+
use indoc::{formatdoc, indoc};
953+
932954
use super::{
933955
TemplateKind, check_generated_file, compile_requirements, load_scenarios,
934956
load_scenarios_from, module_name, render, scenarios_for_template,
@@ -941,7 +963,13 @@ mod tests {
941963
"b>=2.0.0".parse().expect("valid requirement"),
942964
];
943965

944-
assert_eq!(compile_requirements(&requirements), "a==1.0.0\nb>=2.0.0");
966+
assert_eq!(
967+
compile_requirements(&requirements),
968+
indoc! {"
969+
a==1.0.0
970+
b>=2.0.0"
971+
}
972+
);
945973
}
946974

947975
#[test]
@@ -982,10 +1010,12 @@ mod tests {
9821010

9831011
assert!(universal_cases.peek().is_some());
9841012
for case in universal_cases {
985-
assert!(output.contains(&format!(
986-
"#[cfg(feature = \"test-universal\")]\n#[test]\nfn {}()",
1013+
assert!(output.contains(&formatdoc! {r#"
1014+
#[cfg(feature = "test-universal")]
1015+
#[test]
1016+
fn {}()"#,
9871017
module_name(&case.scenario.name)
988-
)));
1018+
}));
9891019
}
9901020
}
9911021

@@ -1070,13 +1100,26 @@ kind = "compile"
10701100
let path = temporary_directory.path().join("scenario.rs");
10711101
fs_err::write(
10721102
&path,
1073-
"/// Literal @\"accepted\" remains significant.\nfn scenario() {\n uv_snapshot!(filters, cmd, @\"\naccepted output\n\");\n}\n",
1103+
indoc! {r#"
1104+
/// Literal @"accepted" remains significant.
1105+
fn scenario() {
1106+
uv_snapshot!(filters, cmd, @"
1107+
accepted output
1108+
");
1109+
}
1110+
"#},
10741111
)
10751112
.expect("temporary file should be written");
10761113

10771114
check_generated_file(
10781115
&path,
1079-
"/// Literal @\"accepted\" remains significant.\nfn scenario() {\n uv_snapshot!(filters, cmd, @r#\"<snapshot>\n\"#);\n}\n",
1116+
indoc! {r##"
1117+
/// Literal @"accepted" remains significant.
1118+
fn scenario() {
1119+
uv_snapshot!(filters, cmd, @r#"<snapshot>
1120+
"#);
1121+
}
1122+
"##},
10801123
)
10811124
.expect("accepted snapshot should not make generated file stale");
10821125
}
@@ -1088,13 +1131,26 @@ kind = "compile"
10881131
let path = temporary_directory.path().join("scenario.rs");
10891132
fs_err::write(
10901133
&path,
1091-
"/// Literal @\"current\" remains significant.\nfn scenario() {\n uv_snapshot!(filters, cmd, @\"\naccepted output\n\");\n}\n",
1134+
indoc! {r#"
1135+
/// Literal @"current" remains significant.
1136+
fn scenario() {
1137+
uv_snapshot!(filters, cmd, @"
1138+
accepted output
1139+
");
1140+
}
1141+
"#},
10921142
)
10931143
.expect("temporary file should be written");
10941144

10951145
check_generated_file(
10961146
&path,
1097-
"/// Literal @\"generated\" remains significant.\nfn scenario() {\n uv_snapshot!(filters, cmd, @r#\"<snapshot>\n\"#);\n}\n",
1147+
indoc! {r##"
1148+
/// Literal @"generated" remains significant.
1149+
fn scenario() {
1150+
uv_snapshot!(filters, cmd, @r#"<snapshot>
1151+
"#);
1152+
}
1153+
"##},
10981154
)
10991155
.expect_err("documentation changes should make generated output stale");
11001156
}

‎crates/uv-distribution-types/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ url = { workspace = true }
5858
version-ranges = { workspace = true }
5959

6060
[dev-dependencies]
61+
indoc = { workspace = true }
6162
toml = { workspace = true }
6263

6364
[features]

‎crates/uv-distribution-types/src/installed_modules.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ fn add_module_components(components: &[&str], modules: &mut BTreeSet<ModuleName>
164164
mod tests {
165165
use std::collections::BTreeSet;
166166

167+
use indoc::indoc;
167168
use uv_pypi_types::ModuleName;
168169

169170
use super::add_record_module;
@@ -203,7 +204,14 @@ mod tests {
203204
add_record_module("package/module.pyc", &[], &mut modules);
204205
add_record_module("legacy.pyc", &[], &mut modules);
205206

206-
assert_eq!(module_names(modules), "legacy\npackage\npackage.module");
207+
assert_eq!(
208+
module_names(modules),
209+
indoc! {"
210+
legacy
211+
package
212+
package.module"
213+
}
214+
);
207215
}
208216

209217
#[test]
@@ -252,7 +260,14 @@ mod tests {
252260

253261
assert_eq!(
254262
module_names(modules),
255-
"package\npackage.extension\npackage.free_threaded\npackage.limited\npackage.windows\nplain"
263+
indoc! {"
264+
package
265+
package.extension
266+
package.free_threaded
267+
package.limited
268+
package.windows
269+
plain"
270+
}
256271
);
257272
}
258273

0 commit comments

Comments
 (0)