diff --git a/CHANGELOG.md b/CHANGELOG.md index 121124e..af751fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - ReleaseDate +### Added +- [PR#34](https://github.com/EmbarkStudios/krates/pull/34) added support for the [`--locked`, `--offline`, and `--frozen`](https://doc.rust-lang.org/cargo/commands/cargo-metadata.html#manifest-options) arguments. + ## [0.8.0] - 2021-07-16 ### Changed - [PR#32](https://github.com/EmbarkStudios/krates/pull/32) replaced the use of `difference` with `similar`. Thanks [@j-k](https://github.com/06kellyjac)! diff --git a/Cargo.toml b/Cargo.toml index 726c92a..5c52430 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,6 @@ semver = "1.0" [dev-dependencies] # Used to print colored diffs in case of test failures -similar = "1.3" +similar-asserts = "1.1" # Used to deserialize test files into metadata we can load serde_json = "1.0" diff --git a/src/builder.rs b/src/builder.rs index 338b4d0..81d807c 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -17,6 +17,32 @@ pub struct Cmd { other_options: Vec, all_features: bool, no_default_features: bool, + frozen: bool, + locked: bool, + offline: bool, +} + +#[derive(Copy, Clone)] +pub struct LockOptions { + /// Requires that the Cargo.lock file is up-to-date. If the lock file is + /// missing, or it needs to be updated, Cargo will exit with an error. + /// Prevents Cargo from attempting to access the network to determine if it + /// is out-of-date. + pub frozen: bool, + /// Requires that the Cargo.lock file is up-to-date. If the lock file is + /// missing, or it needs to be updated, Cargo will exit with an error. + pub locked: bool, + /// Prevents Cargo from accessing the network for any reason. Without this + /// flag, Cargo will stop with an error if it needs to access the network + /// and the network is not available. With this flag, Cargo will attempt to + /// proceed without the network if possible. + /// + /// Beware that this may result in different dependency resolution than + /// online mode. Cargo will restrict itself to crates that are downloaded + /// locally, even if there might be a newer version as indicated in the + /// local copy of the index. See the [cargo fetch](https://doc.rust-lang.org/cargo/commands/cargo-fetch.html) + /// command to download dependencies before going offline. + pub offline: bool, } impl Cmd { @@ -66,6 +92,16 @@ impl Cmd { self } + /// Sets the various [lock options](https://doc.rust-lang.org/cargo/commands/cargo-metadata.html#manifest-options) + /// for determining if cargo can access the network and if the lockfile must + /// be present and can be modified + pub fn lock_opts(&mut self, lopts: LockOptions) -> &mut Self { + self.frozen = lopts.frozen; + self.locked = lopts.locked; + self.offline = lopts.offline; + self + } + /// Arbitrary command line flags to pass to `cargo`. These will be added to /// the end of the command line invocation. pub fn other_options(&mut self, options: impl IntoIterator) -> &mut Self { @@ -123,6 +159,18 @@ impl From for cm::MetadataCommand { opts.append(&mut cmd.features); } + if cmd.frozen { + opts.push("--frozen".to_owned()); + } + + if cmd.locked { + opts.push("--locked".to_owned()); + } + + if cmd.offline { + opts.push("--offline".to_owned()); + } + opts.append(&mut cmd.other_options); mdc.other_options(opts); diff --git a/src/lib.rs b/src/lib.rs index 596c8c9..3280cfc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,7 +95,7 @@ mod builder; mod errors; mod pkgspec; -pub use builder::{Builder, Cmd, NoneFilter, OnFilter, Scope, Target}; +pub use builder::{Builder, Cmd, LockOptions, NoneFilter, OnFilter, Scope, Target}; pub use errors::Error; pub use pkgspec::PkgSpec; diff --git a/tests/util.rs b/tests/util.rs index 8620fee..eb6c63d 100644 --- a/tests/util.rs +++ b/tests/util.rs @@ -1,6 +1,5 @@ #![allow(dead_code)] -use similar::{ChangeTag, TextDiff}; use std::{fmt, path::Path}; pub struct JustId(krates::Kid); @@ -225,21 +224,6 @@ pub struct EdgeFilter<'a> { pub cfg: Option<&'a str>, } -fn diff(orig_text: &str, edit_text: &str) -> String { - let mut buf = String::new(); - let diff = TextDiff::from_lines(orig_text, edit_text); - - for change in diff.iter_all_changes() { - let c = match change.tag() { - ChangeTag::Delete => format!("\x1b[91m{}\x1b[0m", change.value()), - ChangeTag::Insert => format!("\x1b[92m{}\x1b[0m", change.value()), - ChangeTag::Equal => change.value().to_string(), - }; - buf.push_str(&c); - } - buf -} - pub fn cmp bool, EF: Fn(EdgeFilter<'_>) -> bool>( grafs: Grafs, node_filter: NF, @@ -249,26 +233,10 @@ pub fn cmp bool, EF: Fn(EdgeFilter<'_>) -> bool>( use krates::petgraph::dot::Dot; - let expected = format!("{}", Dot::new(&expected)); - let actual = format!("{}", Dot::new(&grafs.actual.graph())); - - if expected != actual { - println!("{:#?}", grafs.filtered); - panic!("{}", diff(&expected, &actual)); - } + similar_asserts::assert_str_eq!( + Dot::new(&expected), + Dot::new(&grafs.actual.graph()), + "filtered: {:#?}", + grafs.filtered + ); } - -// pub fn assert_filtered(actual: &[krates::Kid], expected: &mut [krates::Kid]) { -// expected.sort(); - -// if actual != expected { -// let expected = format!("{:#?}", expected); -// let actual = format!("{:#?}", actual); - -// assert!( -// false, -// "{}", -// difference::Changeset::new(&expected, &actual, "\n") -// ); -// } -// }