Skip to content

Commit

Permalink
Add support for the various lock options (#34)
Browse files Browse the repository at this point in the history
* Add support for the various lock options

* Update CHANGELOG

* Use similar-asserts
  • Loading branch information
Jake-Shadle authored Jul 20, 2021
1 parent fb9f9bc commit 5a51c2e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## [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)!
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
48 changes: 48 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ pub struct Cmd {
other_options: Vec<String>,
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 {
Expand Down Expand Up @@ -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<Item = String>) -> &mut Self {
Expand Down Expand Up @@ -123,6 +159,18 @@ impl From<Cmd> 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);

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
44 changes: 6 additions & 38 deletions tests/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(dead_code)]

use similar::{ChangeTag, TextDiff};
use std::{fmt, path::Path};

pub struct JustId(krates::Kid);
Expand Down Expand Up @@ -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<NF: Fn(&krates::Kid) -> bool, EF: Fn(EdgeFilter<'_>) -> bool>(
grafs: Grafs,
node_filter: NF,
Expand All @@ -249,26 +233,10 @@ pub fn cmp<NF: Fn(&krates::Kid) -> 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")
// );
// }
// }

0 comments on commit 5a51c2e

Please sign in to comment.