Skip to content

Commit

Permalink
Merge pull request #349 from 0xalpharush/fix/init-in-workspace
Browse files Browse the repository at this point in the history
fix: use cargo-metadata to support init command in workspace
  • Loading branch information
fitzgen authored Dec 12, 2023
2 parents a860fd9 + f3865de commit c4a4d33
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 34 deletions.
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 Down Expand Up @@ -52,8 +53,7 @@ impl FuzzProject {
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 @@ pub struct Manifest {
}

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

0 comments on commit c4a4d33

Please sign in to comment.