From 7cc75c3a3c1bba7b26f1fe7909e904371924cdca Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sun, 18 Oct 2015 11:35:54 +0100 Subject: [PATCH 1/3] Treat missing sections as empty, not as error This allows you to execute cargo list for a crate that does not have any dependencies. A downside of this approach is that you don't get an error anymore if you misspell the section name. Sample `Cargo.toml` that it rejected before: [package] name = "name" version = "0.0.1" authors = ["Author Name "] --- src/bin/list/list.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bin/list/list.rs b/src/bin/list/list.rs index bd9a3d3011..7f635af69f 100644 --- a/src/bin/list/list.rs +++ b/src/bin/list/list.rs @@ -8,10 +8,12 @@ use list_error::ListError; #[allow(deprecated)] // connect -> join pub fn list_section(manifest: &Manifest, section: &str) -> Result { let mut output = vec![]; + let empty_list = Default::default(); let list = try!(manifest.data .get(section) - .and_then(|field| field.as_table()) + .map(|field| field.as_table()) + .unwrap_or(Some(&empty_list)) .ok_or_else(|| ListError::SectionMissing(String::from(section)))); let name_max_len = list.keys().map(|k| k.len()).max().unwrap_or(0); @@ -76,10 +78,9 @@ lorem-ipsum 0.4.2"); } #[test] - #[should_panic] - fn unknown_section() { + fn treat_unknown_section_as_empty() { let manifile: Manifest = DEFAULT_CARGO_TOML.parse().unwrap(); - list_section(&manifile, "lol-dependencies").unwrap(); + assert_eq!(list_section(&manifile, "lol-dependencies").unwrap(), ""); } } From 923938f6504f73df0c0d1bc5fef7fa9c8ff86e32 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sat, 24 Oct 2015 14:46:46 +0100 Subject: [PATCH 2/3] Do checking for the nonexistent list one layer higher --- src/bin/list/list.rs | 9 ++++----- src/bin/list/main.rs | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/bin/list/list.rs b/src/bin/list/list.rs index 7f635af69f..bd9a3d3011 100644 --- a/src/bin/list/list.rs +++ b/src/bin/list/list.rs @@ -8,12 +8,10 @@ use list_error::ListError; #[allow(deprecated)] // connect -> join pub fn list_section(manifest: &Manifest, section: &str) -> Result { let mut output = vec![]; - let empty_list = Default::default(); let list = try!(manifest.data .get(section) - .map(|field| field.as_table()) - .unwrap_or(Some(&empty_list)) + .and_then(|field| field.as_table()) .ok_or_else(|| ListError::SectionMissing(String::from(section)))); let name_max_len = list.keys().map(|k| k.len()).max().unwrap_or(0); @@ -78,9 +76,10 @@ lorem-ipsum 0.4.2"); } #[test] - fn treat_unknown_section_as_empty() { + #[should_panic] + fn unknown_section() { let manifile: Manifest = DEFAULT_CARGO_TOML.parse().unwrap(); - assert_eq!(list_section(&manifile, "lol-dependencies").unwrap(), ""); + list_section(&manifile, "lol-dependencies").unwrap(); } } diff --git a/src/bin/list/main.rs b/src/bin/list/main.rs index 31a853d85f..44322cd042 100644 --- a/src/bin/list/main.rs +++ b/src/bin/list/main.rs @@ -24,6 +24,7 @@ mod list_error; mod tree; use list::list_section; +use list_error::ListError; use tree::parse_lock_file as list_tree; static USAGE: &'static str = r" @@ -79,6 +80,10 @@ fn handle_list(args: &Args) -> Result> { } else { let manifest = try!(Manifest::open(&args.flag_manifest_path.as_ref().map(|s| &s[..]))); list_section(&manifest, args.get_section()) + .or_else(|err| match err { + ListError::SectionMissing(..) => Ok("".into()), + _ => Err(err), + }) } .map_err(From::from) } From 225b35e5683e035461f2685221002308ebabce39 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Sat, 24 Oct 2015 16:49:39 +0200 Subject: [PATCH 3/3] Add Integration Tests for Listing Empty Deps --- tests/cargo-list.rs | 38 ++++++++++++++++++++-------- tests/fixtures/list-empty/Cargo.toml | 7 +++++ 2 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 tests/fixtures/list-empty/Cargo.toml diff --git a/tests/cargo-list.rs b/tests/cargo-list.rs index 130220ee35..263ee45cc7 100644 --- a/tests/cargo-list.rs +++ b/tests/cargo-list.rs @@ -1,6 +1,20 @@ #[macro_use] extern crate assert_cli; +#[test] +fn listing() { + assert_cli!("target/debug/cargo-list", + &["list", "--manifest-path=tests/fixtures/list/Cargo.toml"] => + Success, r#"cargo-edit path: "../../../" +clippy git: "https://github.com/Manishearth/rust-clippy.git" (optional) +docopt 0.6 +pad 0.1 +rustc-serialize 0.3 +semver 0.1 +toml 0.1"#) + .unwrap(); +} + #[test] fn listing_dev() { assert_cli!("target/debug/cargo-list", @@ -18,17 +32,21 @@ fn listing_build() { } #[test] -fn listing() { +fn treat_missing_section_as_empty() { + // empty dependencies assert_cli!("target/debug/cargo-list", - &["list", "--manifest-path=tests/fixtures/list/Cargo.toml"] => - Success, r#"cargo-edit path: "../../../" -clippy git: "https://github.com/Manishearth/rust-clippy.git" (optional) -docopt 0.6 -pad 0.1 -rustc-serialize 0.3 -semver 0.1 -toml 0.1"#) - .unwrap(); + &["list", "--manifest-path=tests/fixtures/list-empty/Cargo.toml"] => + Success, "\n").unwrap(); + + // empty dev-dependencies + assert_cli!("target/debug/cargo-list", + &["list", "--dev", "--manifest-path=tests/fixtures/list-empty/Cargo.toml"] => + Success, "\n").unwrap(); + + // empty build-dependencies + assert_cli!("target/debug/cargo-list", + &["list", "--build", "--manifest-path=tests/fixtures/list-empty/Cargo.toml"] => + Success, "\n").unwrap(); } #[test] diff --git a/tests/fixtures/list-empty/Cargo.toml b/tests/fixtures/list-empty/Cargo.toml new file mode 100644 index 0000000000..6915b5b336 --- /dev/null +++ b/tests/fixtures/list-empty/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "cargo-list-empty-test-fixture" +version = "0.1.0" + +[[bin]] +name = "main" +path = "src/main.rs"