Skip to content

Commit 660ce6b

Browse files
committed
Add tests
Signed-off-by: hi-rustin <[email protected]>
1 parent 0c5f348 commit 660ce6b

File tree

4 files changed

+239
-11
lines changed

4 files changed

+239
-11
lines changed

src/bin/cargo/commands/rustc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn cli() -> App {
3939
.arg(multi_opt(
4040
CRATE_TYPE_ARG_NAME,
4141
"CRATE-TYPE",
42-
"Rustc crate-types",
42+
"Comma separated list of types of crates for the compiler to emit (unstable)",
4343
))
4444
.arg_target_dir()
4545
.arg_manifest_path()

src/cargo/core/compiler/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -861,14 +861,19 @@ fn build_base_args(
861861
add_error_format_and_color(cx, cmd, cx.rmeta_required(unit));
862862
add_allow_features(cx, cmd);
863863

864+
let mut contains_dy_lib = false;
864865
if !test {
865-
if let Some(crate_types) = cx.bcx.rustc_crate_types_args_for(unit) {
866-
for crate_type in crate_types.iter() {
867-
cmd.arg("--crate-type").arg(crate_type);
868-
}
869-
} else {
870-
for crate_type in crate_types.iter() {
871-
cmd.arg("--crate-type").arg(crate_type.as_str());
866+
let mut crate_types = &crate_types
867+
.iter()
868+
.map(|t| t.as_str().to_string())
869+
.collect::<Vec<String>>();
870+
if let Some(types) = cx.bcx.rustc_crate_types_args_for(unit) {
871+
crate_types = types;
872+
}
873+
for crate_type in crate_types.iter() {
874+
cmd.arg("--crate-type").arg(crate_type);
875+
if crate_type == CrateType::Dylib.as_str() {
876+
contains_dy_lib = true;
872877
}
873878
}
874879
}
@@ -885,7 +890,7 @@ fn build_base_args(
885890
}
886891

887892
let prefer_dynamic = (unit.target.for_host() && !unit.target.is_custom_build())
888-
|| (crate_types.contains(&CrateType::Dylib) && !cx.is_primary_package(unit));
893+
|| (contains_dy_lib && !cx.is_primary_package(unit));
889894
if prefer_dynamic {
890895
cmd.arg("-C").arg("prefer-dynamic");
891896
}

src/cargo/ops/cargo_compile.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub struct CompileOptions {
7171
/// The specified target will be compiled with all the available arguments,
7272
/// note that this only accounts for the *final* invocation of rustc
7373
pub target_rustc_args: Option<Vec<String>>,
74+
/// Crate types to be passed to rustc (single target only)
7475
pub target_rustc_crate_types: Option<Vec<String>>,
7576
/// Extra arguments passed to all selected targets for rustdoc.
7677
pub local_rustdoc_args: Option<Vec<String>>,
@@ -653,7 +654,7 @@ pub fn create_bcx<'a, 'cfg>(
653654
anyhow::bail!(
654655
"crate types to rustc can only be passed to one \
655656
target, consider filtering\nthe package by passing, \
656-
e.g., `--lib` to specify a single target"
657+
e.g., `--lib` or `--example` to specify a single target"
657658
);
658659
}
659660
match units[0].target.kind() {
@@ -662,7 +663,7 @@ pub fn create_bcx<'a, 'cfg>(
662663
}
663664
_ => {
664665
anyhow::bail!(
665-
"crate types can only be specified for libraries and examples. \
666+
"crate types can only be specified for libraries and example libraries.\n\
666667
Binaries, tests, and benchmarks are always the `bin` crate type"
667668
);
668669
}

tests/testsuite/rustc.rs

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,228 @@ fn fails_with_args_to_all_binaries() {
134134
.run();
135135
}
136136

137+
#[cargo_test]
138+
fn fails_with_crate_type_and_without_unstable_options() {
139+
let p = project().file("src/lib.rs", r#" "#).build();
140+
141+
p.cargo("rustc --crate-type lib")
142+
.masquerade_as_nightly_cargo()
143+
.with_status(101)
144+
.with_stderr(
145+
"[ERROR] the `crate-type` flag is unstable, pass `-Z unstable-options` to enable it
146+
See https://github.com/rust-lang/cargo/issues/10083 for more information about the `crate-type` flag.",
147+
)
148+
.run();
149+
}
150+
151+
#[cargo_test]
152+
fn fails_with_crate_type_to_multi_binaries() {
153+
let p = project()
154+
.file("src/bin/foo.rs", "fn main() {}")
155+
.file("src/bin/bar.rs", "fn main() {}")
156+
.file("src/bin/baz.rs", "fn main() {}")
157+
.file("src/lib.rs", r#" "#)
158+
.build();
159+
160+
p.cargo("rustc --crate-type lib -Zunstable-options")
161+
.masquerade_as_nightly_cargo()
162+
.with_status(101)
163+
.with_stderr(
164+
"[ERROR] crate types to rustc can only be passed to one target, consider filtering
165+
the package by passing, e.g., `--lib` or `--example` to specify a single target",
166+
)
167+
.run();
168+
}
169+
170+
#[cargo_test]
171+
fn fails_with_crate_type_to_multi_examples() {
172+
let p = project()
173+
.file(
174+
"Cargo.toml",
175+
r#"
176+
[package]
177+
name = "foo"
178+
version = "0.0.1"
179+
authors = []
180+
181+
[[example]]
182+
name = "ex1"
183+
crate-type = ["rlib"]
184+
[[example]]
185+
name = "ex2"
186+
crate-type = ["rlib"]
187+
"#,
188+
)
189+
.file("src/lib.rs", "")
190+
.file("examples/ex1.rs", "")
191+
.file("examples/ex2.rs", "")
192+
.build();
193+
194+
p.cargo("rustc -v --example ex1 --example ex2 --crate-type lib,cdylib -Zunstable-options")
195+
.masquerade_as_nightly_cargo()
196+
.with_status(101)
197+
.with_stderr(
198+
"[ERROR] crate types to rustc can only be passed to one target, consider filtering
199+
the package by passing, e.g., `--lib` or `--example` to specify a single target",
200+
)
201+
.run();
202+
}
203+
204+
#[cargo_test]
205+
fn fails_with_crate_type_to_binary() {
206+
let p = project().file("src/bin/foo.rs", "fn main() {}").build();
207+
208+
p.cargo("rustc --crate-type lib -Zunstable-options")
209+
.masquerade_as_nightly_cargo()
210+
.with_status(101)
211+
.with_stderr(
212+
"[ERROR] crate types can only be specified for libraries and example libraries.
213+
Binaries, tests, and benchmarks are always the `bin` crate type",
214+
)
215+
.run();
216+
}
217+
218+
#[cargo_test]
219+
fn build_with_crate_type_for_foo() {
220+
let p = project()
221+
.file("src/main.rs", "fn main() {}")
222+
.file("src/lib.rs", r#" "#)
223+
.build();
224+
225+
p.cargo("rustc -v --lib --crate-type lib -Zunstable-options")
226+
.masquerade_as_nightly_cargo()
227+
.with_stderr(
228+
"\
229+
[COMPILING] foo v0.0.1 ([CWD])
230+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
231+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
232+
",
233+
)
234+
.run();
235+
}
236+
237+
#[cargo_test]
238+
fn build_with_crate_types_for_foo() {
239+
let p = project()
240+
.file("src/main.rs", "fn main() {}")
241+
.file("src/lib.rs", r#" "#)
242+
.build();
243+
244+
p.cargo("rustc -v --lib --crate-type lib,cdylib -Zunstable-options")
245+
.masquerade_as_nightly_cargo()
246+
.with_stderr(
247+
"\
248+
[COMPILING] foo v0.0.1 ([CWD])
249+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib,cdylib [..]
250+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
251+
",
252+
)
253+
.run();
254+
}
255+
256+
#[cargo_test]
257+
fn build_with_crate_type_to_example() {
258+
let p = project()
259+
.file(
260+
"Cargo.toml",
261+
r#"
262+
[package]
263+
name = "foo"
264+
version = "0.0.1"
265+
authors = []
266+
267+
[[example]]
268+
name = "ex"
269+
crate-type = ["rlib"]
270+
"#,
271+
)
272+
.file("src/lib.rs", "")
273+
.file("examples/ex.rs", "")
274+
.build();
275+
276+
p.cargo("rustc -v --example ex --crate-type cdylib -Zunstable-options")
277+
.masquerade_as_nightly_cargo()
278+
.with_stderr(
279+
"\
280+
[COMPILING] foo v0.0.1 ([CWD])
281+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
282+
[RUNNING] `rustc --crate-name ex examples/ex.rs [..]--crate-type cdylib [..]
283+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
284+
",
285+
)
286+
.run();
287+
}
288+
289+
#[cargo_test]
290+
fn build_with_crate_types_to_example() {
291+
let p = project()
292+
.file(
293+
"Cargo.toml",
294+
r#"
295+
[package]
296+
name = "foo"
297+
version = "0.0.1"
298+
authors = []
299+
300+
[[example]]
301+
name = "ex"
302+
crate-type = ["rlib"]
303+
"#,
304+
)
305+
.file("src/lib.rs", "")
306+
.file("examples/ex.rs", "")
307+
.build();
308+
309+
p.cargo("rustc -v --example ex --crate-type lib,cdylib -Zunstable-options")
310+
.masquerade_as_nightly_cargo()
311+
.with_stderr(
312+
"\
313+
[COMPILING] foo v0.0.1 ([CWD])
314+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
315+
[RUNNING] `rustc --crate-name ex examples/ex.rs [..]--crate-type lib,cdylib [..]
316+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
317+
",
318+
)
319+
.run();
320+
}
321+
322+
#[cargo_test]
323+
fn build_with_crate_types_to_one_of_multi_examples() {
324+
let p = project()
325+
.file(
326+
"Cargo.toml",
327+
r#"
328+
[package]
329+
name = "foo"
330+
version = "0.0.1"
331+
authors = []
332+
333+
[[example]]
334+
name = "ex1"
335+
crate-type = ["rlib"]
336+
[[example]]
337+
name = "ex2"
338+
crate-type = ["rlib"]
339+
"#,
340+
)
341+
.file("src/lib.rs", "")
342+
.file("examples/ex1.rs", "")
343+
.file("examples/ex2.rs", "")
344+
.build();
345+
346+
p.cargo("rustc -v --example ex1 --crate-type lib,cdylib -Zunstable-options")
347+
.masquerade_as_nightly_cargo()
348+
.with_stderr(
349+
"\
350+
[COMPILING] foo v0.0.1 ([CWD])
351+
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
352+
[RUNNING] `rustc --crate-name ex1 examples/ex1.rs [..]--crate-type lib,cdylib [..]
353+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
354+
",
355+
)
356+
.run();
357+
}
358+
137359
#[cargo_test]
138360
fn build_with_args_to_one_of_multiple_tests() {
139361
let p = project()

0 commit comments

Comments
 (0)