mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
Auto merge of #5485 - patriksvensson:feature/GH-5474, r=matklad
Do not allow running library examples. This is my first contribution to anything Rust related that I haven't written myself, and I'm not a very proficient Rust programmer (yet), so would love some feedback on improvements. I also might have solved this the wrong way... 😄 Closes #5474 (cc @matklad)
This commit is contained in:
commit
556f80f3dc
@ -160,7 +160,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
|
|||||||
unit.target.name().to_string(),
|
unit.target.name().to_string(),
|
||||||
output.path.clone(),
|
output.path.clone(),
|
||||||
));
|
));
|
||||||
} else if unit.target.is_bin() || unit.target.is_example() {
|
} else if unit.target.is_bin() || unit.target.is_bin_example() {
|
||||||
self.compilation.binaries.push(bindst.clone());
|
self.compilation.binaries.push(bindst.clone());
|
||||||
} else if unit.target.is_lib() {
|
} else if unit.target.is_lib() {
|
||||||
let pkgid = unit.pkg.package_id().clone();
|
let pkgid = unit.pkg.package_id().clone();
|
||||||
|
@ -2,7 +2,7 @@ use std::path::Path;
|
|||||||
|
|
||||||
use ops::{self, Packages};
|
use ops::{self, Packages};
|
||||||
use util::{self, CargoResult, ProcessError};
|
use util::{self, CargoResult, ProcessError};
|
||||||
use core::Workspace;
|
use core::{TargetKind, Workspace};
|
||||||
|
|
||||||
pub fn run(
|
pub fn run(
|
||||||
ws: &Workspace,
|
ws: &Workspace,
|
||||||
@ -36,7 +36,7 @@ pub fn run(
|
|||||||
options.filter.target_run(a)
|
options.filter.target_run(a)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|bin| bin.name())
|
.map(|bin| (bin.name(), bin.kind()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if bins.is_empty() {
|
if bins.is_empty() {
|
||||||
@ -46,13 +46,29 @@ pub fn run(
|
|||||||
// this will be verified in cargo_compile
|
// this will be verified in cargo_compile
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if bins.len() == 1 {
|
||||||
|
let &(name, kind) = bins.first().unwrap();
|
||||||
|
match kind {
|
||||||
|
&TargetKind::ExampleLib(..) => {
|
||||||
|
bail!(
|
||||||
|
"example target `{}` is a library and cannot be executed",
|
||||||
|
name
|
||||||
|
)
|
||||||
|
},
|
||||||
|
_ => { }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if bins.len() > 1 {
|
if bins.len() > 1 {
|
||||||
if !options.filter.is_specific() {
|
if !options.filter.is_specific() {
|
||||||
|
let names: Vec<&str> = bins.into_iter().map(|bin| bin.0).collect();
|
||||||
bail!(
|
bail!(
|
||||||
"`cargo run` requires that a project only have one \
|
"`cargo run` requires that a project only have one \
|
||||||
executable; use the `--bin` option to specify which one \
|
executable; use the `--bin` option to specify which one \
|
||||||
to run\navailable binaries: {}",
|
to run\navailable binaries: {}",
|
||||||
bins.join(", ")
|
names.join(", ")
|
||||||
|
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
bail!(
|
bail!(
|
||||||
|
@ -387,6 +387,38 @@ fn run_example() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_library_example() {
|
||||||
|
let p = project("foo")
|
||||||
|
.file(
|
||||||
|
"Cargo.toml",
|
||||||
|
r#"
|
||||||
|
[package]
|
||||||
|
name = "foo"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = []
|
||||||
|
[[example]]
|
||||||
|
name = "bar"
|
||||||
|
crate_type = ["lib"]
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.file("src/lib.rs", "")
|
||||||
|
.file(
|
||||||
|
"examples/bar.rs",
|
||||||
|
r#"
|
||||||
|
fn foo() {}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert_that(
|
||||||
|
p.cargo("run").arg("--example").arg("bar"),
|
||||||
|
execs()
|
||||||
|
.with_status(101)
|
||||||
|
.with_stderr("[ERROR] example target `bar` is a library and cannot be executed"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn autodiscover_examples_project(rust_edition: &str, autoexamples: Option<bool>) -> Project {
|
fn autodiscover_examples_project(rust_edition: &str, autoexamples: Option<bool>) -> Project {
|
||||||
let autoexamples = match autoexamples {
|
let autoexamples = match autoexamples {
|
||||||
None => "".to_string(),
|
None => "".to_string(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user