Skip to content

Commit db58bba

Browse files
author
Jon Gjengset
committed
Also handle --examples
1 parent a9645e1 commit db58bba

File tree

2 files changed

+83
-18
lines changed

2 files changed

+83
-18
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,27 +331,29 @@ impl<'cfg, 'a> InstallablePackage<'cfg, 'a> {
331331
// target selection, and --bin=requires_a without --features=a will fail with "target
332332
// .. requires the features ..". But rather than assume that's the case, we define the
333333
// behavior for this fallback case as well.
334-
if matches!(
335-
self.opts.filter,
336-
CompileFilter::Only {
337-
bins: FilterRule::Just(..),
338-
..
334+
if let CompileFilter::Only { bins, examples, .. } = &self.opts.filter {
335+
let mut any_specific = false;
336+
if let FilterRule::Just(ref v) = bins {
337+
if !v.is_empty() {
338+
any_specific = true;
339+
}
340+
}
341+
if let FilterRule::Just(ref v) = examples {
342+
if !v.is_empty() {
343+
any_specific = true;
344+
}
345+
}
346+
if any_specific {
347+
bail!("no binaries are available for install using the selected features");
339348
}
340-
) {
341-
bail!("no binaries are available for install using the selected features");
342349
}
343350

344-
// If the user did not specify a filter and there _are_ binaries available, but none
345-
// were selected given the current set of features, let the user know.
346-
if matches!(
347-
self.opts.filter,
348-
CompileFilter::Default { .. }
349-
| CompileFilter::Only {
350-
bins: FilterRule::All,
351-
..
352-
}
353-
) && self.pkg.targets().iter().any(|t| t.is_bin())
354-
{
351+
// If there _are_ binaries available, but none were selected given the current set of
352+
// features, let the user know.
353+
//
354+
// Note that we know at this point that _if_ bins or examples is set to `::Just`,
355+
// they're `::Just([])`, which is `FilterRule::none()`.
356+
if self.pkg.targets().iter().any(|t| t.is_bin()) {
355357
self.config
356358
.shell()
357359
.warn("none of the package's binaries are available for install using the selected features")?;

tests/testsuite/required_features.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,20 +754,51 @@ fn install_multiple_required_features() {
754754
name = "foo_2"
755755
path = "src/foo_2.rs"
756756
required-features = ["a"]
757+
758+
[[example]]
759+
name = "foo_3"
760+
path = "src/foo_3.rs"
761+
required-features = ["b", "c"]
762+
763+
[[example]]
764+
name = "foo_4"
765+
path = "src/foo_4.rs"
766+
required-features = ["a"]
757767
"#,
758768
)
759769
.file("src/foo_1.rs", "fn main() {}")
760770
.file("src/foo_2.rs", "fn main() {}")
771+
.file("src/foo_3.rs", "fn main() {}")
772+
.file("src/foo_4.rs", "fn main() {}")
761773
.build();
762774

763775
p.cargo("install --path .").run();
764776
assert_has_not_installed_exe(cargo_home(), "foo_1");
765777
assert_has_installed_exe(cargo_home(), "foo_2");
778+
assert_has_not_installed_exe(cargo_home(), "foo_3");
779+
assert_has_not_installed_exe(cargo_home(), "foo_4");
780+
p.cargo("uninstall foo").run();
781+
782+
p.cargo("install --path . --bins --examples").run();
783+
assert_has_not_installed_exe(cargo_home(), "foo_1");
784+
assert_has_installed_exe(cargo_home(), "foo_2");
785+
assert_has_not_installed_exe(cargo_home(), "foo_3");
786+
assert_has_installed_exe(cargo_home(), "foo_4");
766787
p.cargo("uninstall foo").run();
767788

768789
p.cargo("install --path . --features c").run();
769790
assert_has_installed_exe(cargo_home(), "foo_1");
770791
assert_has_installed_exe(cargo_home(), "foo_2");
792+
assert_has_not_installed_exe(cargo_home(), "foo_3");
793+
assert_has_not_installed_exe(cargo_home(), "foo_4");
794+
p.cargo("uninstall foo").run();
795+
796+
p.cargo("install --path . --features c --bins --examples")
797+
.run();
798+
assert_has_installed_exe(cargo_home(), "foo_1");
799+
assert_has_installed_exe(cargo_home(), "foo_2");
800+
assert_has_installed_exe(cargo_home(), "foo_3");
801+
assert_has_installed_exe(cargo_home(), "foo_4");
771802
p.cargo("uninstall foo").run();
772803

773804
p.cargo("install --path . --no-default-features")
@@ -776,11 +807,43 @@ fn install_multiple_required_features() {
776807
[INSTALLING] foo v0.0.1 ([..])
777808
[FINISHED] release [optimized] target(s) in [..]
778809
[WARNING] none of the package's binaries are available for install using the selected features
810+
",
811+
)
812+
.run();
813+
p.cargo("install --path . --no-default-features --bins")
814+
.with_stderr(
815+
"\
816+
[INSTALLING] foo v0.0.1 ([..])
817+
[WARNING] Target filter `bins` specified, but no targets matched. This is a no-op
818+
[FINISHED] release [optimized] target(s) in [..]
819+
[WARNING] none of the package's binaries are available for install using the selected features
820+
",
821+
)
822+
.run();
823+
p.cargo("install --path . --no-default-features --examples")
824+
.with_stderr(
825+
"\
826+
[INSTALLING] foo v0.0.1 ([..])
827+
[WARNING] Target filter `examples` specified, but no targets matched. This is a no-op
828+
[FINISHED] release [optimized] target(s) in [..]
829+
[WARNING] none of the package's binaries are available for install using the selected features
830+
",
831+
)
832+
.run();
833+
p.cargo("install --path . --no-default-features --bins --examples")
834+
.with_stderr(
835+
"\
836+
[INSTALLING] foo v0.0.1 ([..])
837+
[WARNING] Target filters `bins`, `examples` specified, but no targets matched. This is a no-op
838+
[FINISHED] release [optimized] target(s) in [..]
839+
[WARNING] none of the package's binaries are available for install using the selected features
779840
",
780841
)
781842
.run();
782843
assert_has_not_installed_exe(cargo_home(), "foo_1");
783844
assert_has_not_installed_exe(cargo_home(), "foo_2");
845+
assert_has_not_installed_exe(cargo_home(), "foo_3");
846+
assert_has_not_installed_exe(cargo_home(), "foo_4");
784847
}
785848

786849
#[cargo_test]

0 commit comments

Comments
 (0)