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

fix: use cargo-metadata to support init command in workspace #349

Merged
merged 5 commits into from
Dec 12, 2023
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
120 changes: 112 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ clap = { version = "4.0.29", features = ["derive", "deprecated"] }
tempfile = "3.3.0"
toml = "0.5.9"
rustc_version = "0.4.0"
cargo_metadata = "0.18.1"

[dev-dependencies]
assert_cmd = "2.0.7"
Expand Down
3 changes: 1 addition & 2 deletions src/options/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ pub struct Add {
impl RunCommand for Add {
fn run_command(&mut self) -> Result<()> {
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
let fuzz_manifest_path = project.fuzz_dir().join("Cargo.toml");
let manifest = Manifest::parse(&fuzz_manifest_path)?;
let manifest = Manifest::parse()?;
project.add_target(self, &manifest)
}
}
35 changes: 13 additions & 22 deletions src/project.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::options::{self, BuildMode, BuildOptions, Sanitizer};
use crate::utils::default_target;
use anyhow::{anyhow, bail, Context, Result};
use cargo_metadata::MetadataCommand;
use std::collections::HashSet;
use std::io::Read;
use std::io::Write;
Expand All @@ -17,7 +18,7 @@
/// The project with fuzz targets
fuzz_dir: PathBuf,
/// The project being fuzzed
project_dir: PathBuf,

Check warning on line 21 in src/project.rs

View workflow job for this annotation

GitHub Actions / build

field `project_dir` is never read
targets: Vec<String>,
}

Expand Down Expand Up @@ -52,8 +53,7 @@
pub fn init(init: &options::Init, fuzz_dir_opt: Option<PathBuf>) -> Result<Self> {
let project = Self::manage_initial_instance(fuzz_dir_opt)?;
let fuzz_project = project.fuzz_dir();
let root_project_manifest_path = project.project_dir.join("Cargo.toml");
let manifest = Manifest::parse(&root_project_manifest_path)?;
let manifest = Manifest::parse()?;

// TODO: check if the project is already initialized
fs::create_dir(fuzz_project)
Expand Down Expand Up @@ -930,26 +930,17 @@
}

impl Manifest {
pub fn parse(path: &Path) -> Result<Self> {
let contents = fs::read(path)?;
let value: toml::Value = toml::from_slice(&contents)?;
let package = value
.as_table()
.and_then(|v| v.get("package"))
.and_then(toml::Value::as_table);
let crate_name = package
.and_then(|v| v.get("name"))
.and_then(toml::Value::as_str)
.with_context(|| anyhow!("{} (package.name) is malformed", path.display()))?
.to_owned();
let edition = package
.expect("can't be None at this point")
.get("edition")
.map(|v| match v.as_str() {
Some(s) => Ok(s.to_owned()),
None => bail!("{} (package.edition) is malformed", path.display()),
})
.transpose()?;
pub fn parse() -> Result<Self> {
let metatdata = MetadataCommand::new().exec()?;
let package = metatdata.packages.first().with_context(|| {
anyhow!(
"Expected to find at least one package in {}",
metatdata.target_directory
)
})?;
let crate_name = package.name.clone();
let edition = Some(String::from(package.edition.as_str()));

Ok(Manifest {
crate_name,
edition,
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ fn run_with_crash() {
.env("RUST_BACKTRACE", "1")
.assert()
.stderr(
predicate::str::contains("panicked at 'I'm afraid of number 7'")
predicate::str::contains("thread '<unnamed>' panicked at")
.and(predicate::str::contains("I'm afraid of number 7"))
.and(predicate::str::contains("ERROR: libFuzzer: deadly signal"))
.and(predicate::str::contains("run_with_crash::fail_fuzzing"))
.and(predicate::str::contains(
Expand Down Expand Up @@ -317,7 +318,8 @@ fn run_without_sanitizer_with_crash() {
.env("RUST_BACKTRACE", "1")
.assert()
.stderr(
predicate::str::contains("panicked at 'I'm afraid of number 7'")
predicate::str::contains("thread '<unnamed>' panicked at")
.and(predicate::str::contains("I'm afraid of number 7"))
.and(predicate::str::contains("ERROR: libFuzzer: deadly signal"))
.and(predicate::str::contains("run_without_sanitizer_with_crash::fail_fuzzing"))
.and(predicate::str::contains(
Expand Down
1 change: 1 addition & 0 deletions tests/tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl ProjectBuilder {
"Cargo.toml",
&format!(
r#"
[workspace]
[package]
name = "{name}"
version = "1.0.0"
Expand Down
Loading