Skip to content

Commit

Permalink
List unbuilt libraries as such rather than err
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed May 12, 2023
1 parent 7e6b868 commit 75190fb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
35 changes: 35 additions & 0 deletions cargo-dylint/tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,41 @@ libraries = [
}
}

#[test]
fn list() {
let tempdir = tempdir().unwrap();

dylint_internal::cargo::init("package `list_test`", false)
.current_dir(&tempdir)
.args(["--name", "list_test"])
.success()
.unwrap();

let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(tempdir.path().join("Cargo.toml"))
.unwrap();

write!(
file,
r#"
[[workspace.metadata.dylint.libraries]]
git = "https://github.com/trailofbits/dylint"
pattern = "examples/general/crate_wide_allow"
"#,
)
.unwrap();

std::process::Command::cargo_bin("cargo-dylint")
.unwrap()
.current_dir(&tempdir)
.args(["dylint", "list"])
.assert()
.success()
.stdout(predicate::str::contains("<unbuilt>"));
}

#[test]
fn nonexistent_git_library() {
let tempdir = tempdir().unwrap();
Expand Down
1 change: 1 addition & 0 deletions dylint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "A tool for running Rust lints from dynamic libraries"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/trailofbits/dylint"
rust-version = "1.64"

[dependencies]
ansi_term = "0.12"
Expand Down
22 changes: 11 additions & 11 deletions dylint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,7 @@ fn list_libs(name_toolchain_map: &NameToolchainMap) -> Result<()> {
for (toolchain, maybe_libraries) in toolchain_map {
for maybe_library in maybe_libraries {
let location = display_location(&maybe_library.path())?;

println!(
"{name:<name_width$} {toolchain:<toolchain_width$} {}",
location.to_string_lossy()
);
println!("{name:<name_width$} {toolchain:<toolchain_width$} {location}",);
}
}
}
Expand Down Expand Up @@ -418,7 +414,7 @@ fn list_lints(opts: &Dylint, resolved: &ToolchainMap) -> Result<()> {
}
if paths.len() >= 2 {
let location = display_location(path)?;
print!(" ({})", location.to_string_lossy());
print!(" ({location})");
}
println!();

Expand All @@ -440,18 +436,22 @@ fn list_lints(opts: &Dylint, resolved: &ToolchainMap) -> Result<()> {
Ok(())
}

fn display_location(path: &Path) -> Result<PathBuf> {
fn display_location(path: &Path) -> Result<String> {
let current_dir = current_dir().with_context(|| "Could not get current directory")?;
let path_buf = path
.canonicalize()
.with_context(|| format!("Could not canonicalize {path:?}"))?;
let path_buf = match path.canonicalize() {
Ok(path_buf) => path_buf,
Err(_) => {
return Ok("<unbuilt>".to_owned());
}
};
let parent = path_buf
.parent()
.ok_or_else(|| anyhow!("Could not get parent directory"))?;
Ok(parent
.strip_prefix(&current_dir)
.unwrap_or(parent)
.to_path_buf())
.to_string_lossy()
.to_string())
}

fn check_or_fix(opts: &Dylint, resolved: &ToolchainMap) -> Result<()> {
Expand Down

0 comments on commit 75190fb

Please sign in to comment.