Skip to content

Commit 2698bc6

Browse files
committed
Test for target auto-discovery error enhancement
1 parent 74390a4 commit 2698bc6

File tree

1 file changed

+253
-6
lines changed

1 file changed

+253
-6
lines changed

tests/testsuite/build.rs

Lines changed: 253 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,40 @@ fn explicit_examples() {
18991899
.run();
19001900
}
19011901

1902+
#[cargo_test]
1903+
fn non_existing_test() {
1904+
let p = project()
1905+
.file(
1906+
"Cargo.toml",
1907+
r#"
1908+
[package]
1909+
name = "foo"
1910+
version = "1.0.0"
1911+
1912+
[lib]
1913+
name = "foo"
1914+
path = "src/lib.rs"
1915+
1916+
[[test]]
1917+
name = "hello"
1918+
"#,
1919+
)
1920+
.file("src/lib.rs", "")
1921+
.build();
1922+
1923+
p.cargo("build --tests -v")
1924+
.with_status(101)
1925+
.with_stderr(
1926+
"\
1927+
[ERROR] failed to parse manifest at `[..]`
1928+
1929+
Caused by:
1930+
can't find `hello` test at `tests/hello.rs` or `tests/hello/main.rs`. \
1931+
Please specify test.path if you want to use a non-default path.",
1932+
)
1933+
.run();
1934+
}
1935+
19021936
#[cargo_test]
19031937
fn non_existing_example() {
19041938
let p = project()
@@ -1908,7 +1942,6 @@ fn non_existing_example() {
19081942
[package]
19091943
name = "foo"
19101944
version = "1.0.0"
1911-
authors = []
19121945
19131946
[lib]
19141947
name = "foo"
@@ -1921,14 +1954,49 @@ fn non_existing_example() {
19211954
.file("src/lib.rs", "")
19221955
.build();
19231956

1924-
p.cargo("test -v")
1957+
p.cargo("build --examples -v")
19251958
.with_status(101)
19261959
.with_stderr(
19271960
"\
19281961
[ERROR] failed to parse manifest at `[..]`
19291962
19301963
Caused by:
1931-
can't find `hello` example, specify example.path",
1964+
can't find `hello` example at `examples/hello.rs` or `examples/hello/main.rs`. \
1965+
Please specify example.path if you want to use a non-default path.",
1966+
)
1967+
.run();
1968+
}
1969+
1970+
#[cargo_test]
1971+
fn non_existing_benchmark() {
1972+
let p = project()
1973+
.file(
1974+
"Cargo.toml",
1975+
r#"
1976+
[package]
1977+
name = "foo"
1978+
version = "1.0.0"
1979+
1980+
[lib]
1981+
name = "foo"
1982+
path = "src/lib.rs"
1983+
1984+
[[bench]]
1985+
name = "hello"
1986+
"#,
1987+
)
1988+
.file("src/lib.rs", "")
1989+
.build();
1990+
1991+
p.cargo("build --benches -v")
1992+
.with_status(101)
1993+
.with_stderr(
1994+
"\
1995+
[ERROR] failed to parse manifest at `[..]`
1996+
1997+
Caused by:
1998+
can't find `hello` bench at `benches/hello.rs` or `benches/hello/main.rs`. \
1999+
Please specify bench.path if you want to use a non-default path.",
19322000
)
19332001
.run();
19342002
}
@@ -1948,7 +2016,184 @@ fn non_existing_binary() {
19482016
[ERROR] failed to parse manifest at `[..]`
19492017
19502018
Caused by:
1951-
can't find `foo` bin, specify bin.path",
2019+
can't find `foo` bin at `src/bin/foo.rs` or `src/bin/foo/main.rs`. \
2020+
Please specify bin.path if you want to use a non-default path.",
2021+
)
2022+
.run();
2023+
}
2024+
2025+
#[cargo_test]
2026+
fn commonly_wrong_path_of_test() {
2027+
let p = project()
2028+
.file(
2029+
"Cargo.toml",
2030+
r#"
2031+
[package]
2032+
name = "foo"
2033+
version = "1.0.0"
2034+
2035+
[lib]
2036+
name = "foo"
2037+
path = "src/lib.rs"
2038+
2039+
[[test]]
2040+
name = "foo"
2041+
"#,
2042+
)
2043+
.file("src/lib.rs", "")
2044+
.file("test/foo.rs", "")
2045+
.build();
2046+
2047+
p.cargo("build --tests -v")
2048+
.with_status(101)
2049+
.with_stderr(
2050+
"\
2051+
[ERROR] failed to parse manifest at `[..]`
2052+
2053+
Caused by:
2054+
can't find `foo` test at default paths, but found a file at `test/foo.rs`.
2055+
Perhaps rename the file to `tests/foo.rs` for target auto-discovery, \
2056+
or specify test.path if you want to use a non-default path.",
2057+
)
2058+
.run();
2059+
}
2060+
2061+
#[cargo_test]
2062+
fn commonly_wrong_path_of_example() {
2063+
let p = project()
2064+
.file(
2065+
"Cargo.toml",
2066+
r#"
2067+
[package]
2068+
name = "foo"
2069+
version = "1.0.0"
2070+
2071+
[lib]
2072+
name = "foo"
2073+
path = "src/lib.rs"
2074+
2075+
[[example]]
2076+
name = "foo"
2077+
"#,
2078+
)
2079+
.file("src/lib.rs", "")
2080+
.file("example/foo.rs", "")
2081+
.build();
2082+
2083+
p.cargo("build --examples -v")
2084+
.with_status(101)
2085+
.with_stderr(
2086+
"\
2087+
[ERROR] failed to parse manifest at `[..]`
2088+
2089+
Caused by:
2090+
can't find `foo` example at default paths, but found a file at `example/foo.rs`.
2091+
Perhaps rename the file to `examples/foo.rs` for target auto-discovery, \
2092+
or specify example.path if you want to use a non-default path.",
2093+
)
2094+
.run();
2095+
}
2096+
2097+
#[cargo_test]
2098+
fn commonly_wrong_path_of_benchmark() {
2099+
let p = project()
2100+
.file(
2101+
"Cargo.toml",
2102+
r#"
2103+
[package]
2104+
name = "foo"
2105+
version = "1.0.0"
2106+
2107+
[lib]
2108+
name = "foo"
2109+
path = "src/lib.rs"
2110+
2111+
[[bench]]
2112+
name = "foo"
2113+
"#,
2114+
)
2115+
.file("src/lib.rs", "")
2116+
.file("bench/foo.rs", "")
2117+
.build();
2118+
2119+
p.cargo("build --benches -v")
2120+
.with_status(101)
2121+
.with_stderr(
2122+
"\
2123+
[ERROR] failed to parse manifest at `[..]`
2124+
2125+
Caused by:
2126+
can't find `foo` bench at default paths, but found a file at `bench/foo.rs`.
2127+
Perhaps rename the file to `benches/foo.rs` for target auto-discovery, \
2128+
or specify bench.path if you want to use a non-default path.",
2129+
)
2130+
.run();
2131+
}
2132+
2133+
#[cargo_test]
2134+
fn commonly_wrong_path_binary() {
2135+
let p = project()
2136+
.file("Cargo.toml", &basic_bin_manifest("foo"))
2137+
.file("src/lib.rs", "")
2138+
.file("src/bins/foo.rs", "")
2139+
.build();
2140+
2141+
p.cargo("build -v")
2142+
.with_status(101)
2143+
.with_stderr(
2144+
"\
2145+
[ERROR] failed to parse manifest at `[..]`
2146+
2147+
Caused by:
2148+
can't find `foo` bin at default paths, but found a file at `src/bins/foo.rs`.
2149+
Perhaps rename the file to `src/bin/foo.rs` for target auto-discovery, \
2150+
or specify bin.path if you want to use a non-default path.",
2151+
)
2152+
.run();
2153+
}
2154+
2155+
#[cargo_test]
2156+
fn commonly_wrong_path_subdir_binary() {
2157+
let p = project()
2158+
.file("Cargo.toml", &basic_bin_manifest("foo"))
2159+
.file("src/lib.rs", "")
2160+
.file("src/bins/foo/main.rs", "")
2161+
.build();
2162+
2163+
p.cargo("build -v")
2164+
.with_status(101)
2165+
.with_stderr(
2166+
"\
2167+
[ERROR] failed to parse manifest at `[..]`
2168+
2169+
Caused by:
2170+
can't find `foo` bin at default paths, but found a file at `src/bins/foo/main.rs`.
2171+
Perhaps rename the file to `src/bin/foo/main.rs` for target auto-discovery, \
2172+
or specify bin.path if you want to use a non-default path.",
2173+
)
2174+
.run();
2175+
}
2176+
2177+
#[cargo_test]
2178+
fn found_multiple_target_files() {
2179+
let p = project()
2180+
.file("Cargo.toml", &basic_bin_manifest("foo"))
2181+
.file("src/lib.rs", "")
2182+
.file("src/bin/foo.rs", "")
2183+
.file("src/bin/foo/main.rs", "")
2184+
.build();
2185+
2186+
p.cargo("build -v")
2187+
.with_status(101)
2188+
// Don't assert the inferred pathes since the order is non-deterministic.
2189+
.with_stderr(
2190+
"\
2191+
[ERROR] failed to parse manifest at `[..]`
2192+
2193+
Caused by:
2194+
cannot infer path for `foo` bin
2195+
Cargo doesn't know which to use because multiple target files found \
2196+
at `src/bin/foo[..].rs` and `src/bin/foo[..].rs`.",
19522197
)
19532198
.run();
19542199
}
@@ -4319,7 +4564,7 @@ fn no_bin_in_src_with_lib() {
43194564
[ERROR] failed to parse manifest at `[..]`
43204565
43214566
Caused by:
4322-
can't find `foo` bin, specify bin.path",
4567+
can't find `foo` bin at `src/bin/foo.rs` or `src/bin/foo/main.rs`. [..]",
43234568
)
43244569
.run();
43254570
}
@@ -4529,7 +4774,9 @@ fn building_a_dependent_crate_witout_bin_should_fail() {
45294774

45304775
p.cargo("build")
45314776
.with_status(101)
4532-
.with_stderr_contains("[..]can't find `a_bin` bin, specify bin.path")
4777+
.with_stderr_contains(
4778+
"[..]can't find `a_bin` bin at `src/bin/a_bin.rs` or `src/bin/a_bin/main.rs`[..]",
4779+
)
45334780
.run();
45344781
}
45354782

0 commit comments

Comments
 (0)