Skip to content

Commit

Permalink
Default cargo-fuzz init --create-workspace to false
Browse files Browse the repository at this point in the history
With this change, users will be required to either add
`fuzz` directory to the workspace manually or add
`--create-workspace=true`.
  • Loading branch information
kdarkhan committed Jan 11, 2024
1 parent 22b12fa commit 143d918
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ $ cargo install -f cargo-fuzz

Initialize a `cargo fuzz` project for your crate!

### Add `fuzz` directory to `workspace.members` in root `Cargo.toml`

`fuzz` directory can be either a part of an existing workspace (default)
or use an independent workspace. If latter is desired, you can use
`cargo fuzz init --create-workspace=true`.

### `cargo fuzz add <target>`

Create a new fuzzing target!
Expand Down
2 changes: 1 addition & 1 deletion src/options/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Init {
/// Name of the first fuzz target to create
pub target: String,

#[arg(long, value_parser = clap::builder::BoolishValueParser::new(), default_value = "true")]
#[arg(long, value_parser = clap::builder::BoolishValueParser::new(), default_value = "false")]
/// Whether to create a separate workspace for fuzz targets crate
pub create_workspace: Option<bool>,

Expand Down
33 changes: 31 additions & 2 deletions tests/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,35 @@ fn help() {
}

#[test]
fn init() {
fn init_with_workspace() {
let project = project("init").build();
project
.cargo_fuzz()
.arg("init")
.arg("--create-workspace=true")
.assert()
.success();
assert!(project.fuzz_dir().is_dir());
assert!(project.fuzz_cargo_toml().is_file());
assert!(project.fuzz_targets_dir().is_dir());
assert!(project.fuzz_target_path("fuzz_target_1").is_file());
project
.cargo_fuzz()
.arg("run")
.arg("fuzz_target_1")
.arg("--")
.arg("-runs=1")
.assert()
.success();
}

#[test]
fn init_no_workspace() {
let mut project_builder = project("init_no_workspace");
let project = project_builder.build();
project.cargo_fuzz().arg("init").assert().success();
project_builder.set_workspace_members(&["fuzz"]);

assert!(project.fuzz_dir().is_dir());
assert!(project.fuzz_cargo_toml().is_file());
assert!(project.fuzz_targets_dir().is_dir());
Expand All @@ -28,20 +54,23 @@ fn init() {
.cargo_fuzz()
.arg("run")
.arg("fuzz_target_1")
.arg("--fuzz-dir")
.arg(project.fuzz_dir().to_str().unwrap())
.arg("--")
.arg("-runs=1")
.assert()
.success();
}

#[test]
fn init_with_target() {
fn init_with_target_and_workspace() {
let project = project("init_with_target").build();
project
.cargo_fuzz()
.arg("init")
.arg("-t")
.arg("custom_target_name")
.arg("--create-workspace=true")
.assert()
.success();
assert!(project.fuzz_dir().is_dir());
Expand Down
20 changes: 20 additions & 0 deletions tests/tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ impl ProjectBuilder {
self.file(path, body)
}

pub fn set_workspace_members(&mut self, members: &[&str]) -> &mut Self {
let cargo_toml = self.root().join("Cargo.toml");
let manifest = fs::read_to_string(cargo_toml.clone()).unwrap();

let with_members = manifest.replace(
"[workspace]",
&format!(
"[workspace]\nmembers=[{}]",
members
.iter()
.map(|&v| format!("\"{}\"", v))
.collect::<Vec<_>>()
.join(", ")
),
);

fs::write(cargo_toml, with_members).unwrap();
self
}

pub fn file<B: AsRef<Path>>(&mut self, path: B, body: &str) -> &mut Self {
self._file(path.as_ref(), body);
self
Expand Down

0 comments on commit 143d918

Please sign in to comment.