Skip to content

Commit d1fe89e

Browse files
committed
Add tests
1 parent 0dfdba6 commit d1fe89e

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,24 @@ pub fn basic_lib_manifest(name: &str) -> String {
16221622
)
16231623
}
16241624

1625+
pub fn basic_bin_manifest_with_readme(name: &str, readme_filename: &str) -> String {
1626+
format!(
1627+
r#"
1628+
[package]
1629+
1630+
name = "{}"
1631+
version = "0.5.0"
1632+
authors = ["wycats@example.com"]
1633+
readme = "{}"
1634+
1635+
[[bin]]
1636+
1637+
name = "{}"
1638+
"#,
1639+
name, readme_filename, name
1640+
)
1641+
}
1642+
16251643
pub fn path2url<P: AsRef<Path>>(p: P) -> Url {
16261644
Url::from_file_path(p).ok().unwrap()
16271645
}

tests/testsuite/read_manifest.rs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
//! Tests for the `cargo read-manifest` command.
22
3-
use cargo_test_support::{basic_bin_manifest, main_file, project};
3+
use cargo_test_support::{basic_bin_manifest, basic_bin_manifest_with_readme, main_file, project};
44

5-
static MANIFEST_OUTPUT: &str = r#"
6-
{
5+
fn manifest_output(readme_value: &str) -> String {
6+
format!(
7+
r#"
8+
{{
79
"authors": [
810
"wycats@example.com"
911
],
1012
"categories": [],
1113
"name":"foo",
12-
"readme": null,
14+
"readme": {},
1315
"repository": null,
1416
"version":"0.5.0",
1517
"id":"foo[..]0.5.0[..](path+file://[..]/foo)",
@@ -21,19 +23,25 @@ static MANIFEST_OUTPUT: &str = r#"
2123
"edition": "2015",
2224
"source":null,
2325
"dependencies":[],
24-
"targets":[{
26+
"targets":[{{
2527
"kind":["bin"],
2628
"crate_types":["bin"],
2729
"doctest": false,
2830
"edition": "2015",
2931
"name":"foo",
3032
"src_path":"[..]/foo/src/foo.rs"
31-
}],
32-
"features":{},
33+
}}],
34+
"features":{{}},
3335
"manifest_path":"[..]Cargo.toml",
3436
"metadata": null,
3537
"publish": null
36-
}"#;
38+
}}"#, readme_value
39+
)
40+
}
41+
42+
fn manifest_output_no_readme() -> String {
43+
manifest_output("null")
44+
}
3745

3846
#[cargo_test]
3947
fn cargo_read_manifest_path_to_cargo_toml_relative() {
@@ -44,7 +52,7 @@ fn cargo_read_manifest_path_to_cargo_toml_relative() {
4452

4553
p.cargo("read-manifest --manifest-path foo/Cargo.toml")
4654
.cwd(p.root().parent().unwrap())
47-
.with_json(MANIFEST_OUTPUT)
55+
.with_json(&manifest_output_no_readme())
4856
.run();
4957
}
5058

@@ -58,7 +66,7 @@ fn cargo_read_manifest_path_to_cargo_toml_absolute() {
5866
p.cargo("read-manifest --manifest-path")
5967
.arg(p.root().join("Cargo.toml"))
6068
.cwd(p.root().parent().unwrap())
61-
.with_json(MANIFEST_OUTPUT)
69+
.with_json(&manifest_output_no_readme())
6270
.run();
6371
}
6472

@@ -104,5 +112,32 @@ fn cargo_read_manifest_cwd() {
104112
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
105113
.build();
106114

107-
p.cargo("read-manifest").with_json(MANIFEST_OUTPUT).run();
115+
p.cargo("read-manifest").with_json(&manifest_output_no_readme()).run();
116+
}
117+
118+
#[cargo_test]
119+
fn cargo_read_manifest_default_readme() {
120+
let readme_filenames = ["README.md", "README.txt", "README"];
121+
122+
for readme in readme_filenames.iter() {
123+
let p = project()
124+
.file("Cargo.toml", &basic_bin_manifest("foo"))
125+
.file(readme, "Sample project")
126+
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
127+
.build();
128+
129+
p.cargo("read-manifest").with_json(&manifest_output(&format!(r#""{}""#, readme))).run();
130+
}
131+
}
132+
133+
#[cargo_test]
134+
fn cargo_read_manifest_suppress_default_readme() {
135+
let p = project()
136+
.file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "false"))
137+
.file("README.txt", "Sample project")
138+
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
139+
.build();
140+
141+
p.cargo("read-manifest").with_json(&manifest_output_no_readme()).run();
108142
}
143+

0 commit comments

Comments
 (0)