Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from difference to similar #32

Merged
merged 3 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "0.11" # Using the same version as cargo_metadata

[dev-dependencies]
# Used to print colored diffs in case of test failures
difference = "2.0"
similar = "1.3"
# Used to deserialize test files into metadata we can load
serde_json = "1.0"
5 changes: 1 addition & 4 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
[advisories]
vulnerability = "deny"
unmaintained = "deny"
ignore = [
# difference is unmaintained
"RUSTSEC-2020-0095",
]
ignore = []

[licenses]
unlicensed = "deny"
Expand Down
34 changes: 17 additions & 17 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ impl Cmd {
}
}

impl Into<cm::MetadataCommand> for Cmd {
fn into(mut self) -> cm::MetadataCommand {
impl From<Cmd> for cm::MetadataCommand {
fn from(mut cmd: Cmd) -> cm::MetadataCommand {
let mut mdc = cm::MetadataCommand::new();

if let Some(cp) = self.cargo_path {
if let Some(cp) = cmd.cargo_path {
mdc.cargo_path(cp);
}

Expand All @@ -87,43 +87,43 @@ impl Into<cm::MetadataCommand> for Cmd {
// edge case where you can run cargo metadata from a directory outside
// of a workspace which could fail if eg. there is a reference to a
// registry that is defined in the workspace's .cargo/config
if let Some(mp) = self.manifest_path {
self.current_dir = Some(mp.parent().unwrap().to_owned());
if let Some(mp) = cmd.manifest_path {
cmd.current_dir = Some(mp.parent().unwrap().to_owned());
mdc.manifest_path("Cargo.toml");
}

if let Some(cd) = self.current_dir {
if let Some(cd) = cmd.current_dir {
mdc.current_dir(cd);
}

// Everything else we specify as additional options, as MetadataCommand
// does not handle features correctly, eg. you cannot disable default
// and set specific ones at the same time
// https://github.com/oli-obk/cargo_metadata/issues/79
self.features.sort();
self.features.dedup();
cmd.features.sort();
cmd.features.dedup();

let mut opts = Vec::with_capacity(
self.features.len()
+ self.other_options.len()
+ if self.no_default_features { 1 } else { 0 }
+ if self.all_features { 1 } else { 0 },
cmd.features.len()
+ cmd.other_options.len()
+ if cmd.no_default_features { 1 } else { 0 }
+ if cmd.all_features { 1 } else { 0 },
);

if self.no_default_features {
if cmd.no_default_features {
opts.push("--no-default-features".to_owned());
}

if self.all_features {
if cmd.all_features {
opts.push("--all-features".to_owned());
}

if !self.features.is_empty() {
if !cmd.features.is_empty() {
opts.push("--features".to_owned());
opts.append(&mut self.features);
opts.append(&mut cmd.features);
}

opts.append(&mut self.other_options);
opts.append(&mut cmd.other_options);
mdc.other_options(opts);

mdc
Expand Down
2 changes: 1 addition & 1 deletion src/pkgspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl std::str::FromStr for PkgSpec {
}
}
} else {
let (name, version) = name_and_or_version(Some(&s[..]), None)?;
let (name, version) = name_and_or_version(Some(s), None)?;

Ok(Self {
url: None,
Expand Down
18 changes: 17 additions & 1 deletion tests/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(dead_code)]

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

pub struct JustId(krates::Kid);
Expand Down Expand Up @@ -224,6 +225,21 @@ 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 @@ -238,7 +254,7 @@ pub fn cmp<NF: Fn(&krates::Kid) -> bool, EF: Fn(EdgeFilter<'_>) -> bool>(

if expected != actual {
println!("{:#?}", grafs.filtered);
panic!("{}", difference::Changeset::new(&expected, &actual, "\n"));
panic!("{}", diff(&expected, &actual));
}
}

Expand Down