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

Feat/clean command #101

Merged
merged 5 commits into from
Sep 19, 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
3 changes: 3 additions & 0 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub use explorer::{explorer, ExplorerCommand};

mod init;
pub use init::init;

mod clean;
pub use clean::clean;
9 changes: 9 additions & 0 deletions crates/cli/src/command/clean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use anyhow::Error;
use fehler::throws;
use trdelnik_client::Cleaner;

#[throws]
pub async fn clean() {
let cleaner = Cleaner::new();
cleaner.clean_target().await?;
}
4 changes: 4 additions & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod command;
// bring nested subcommand enums into scope
use command::ExplorerCommand;
use command::FuzzCommand;

use command::KeyPairCommand;

#[derive(Parser)]
Expand Down Expand Up @@ -56,6 +57,8 @@ enum Command {
#[arg(short, long)]
skip_fuzzer: bool,
},
/// Removes target contents except for KeyPair and removes hfuzz_target folder
Clean,
}

#[throws]
Expand All @@ -70,5 +73,6 @@ pub async fn start() {
Command::Localnet => command::localnet().await?,
Command::Explorer { subcmd } => command::explorer(subcmd).await?,
Command::Init { skip_fuzzer } => command::init(skip_fuzzer).await?,
Command::Clean => command::clean().await?,
}
}
59 changes: 59 additions & 0 deletions crates/client/src/cleaner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::config::Config;
use fehler::{throw, throws};
use std::{
io,
path::{Path, PathBuf},
};
use thiserror::Error;
use tokio::{fs, process::Command};

#[derive(Error, Debug)]
pub enum Error {
#[error("{0:?}")]
Io(#[from] io::Error),
#[error("Cannot find the Anchor.toml file to locate the root folder")]
BadWorkspace,
}

pub struct Cleaner;

impl Default for Cleaner {
fn default() -> Self {
Self::new()
}
}

impl Cleaner {
pub fn new() -> Self {
Self
}
#[throws]
pub async fn clean_target(&self) {
let root = match Config::discover_root() {
Ok(root) => root,
Err(_) => throw!(Error::BadWorkspace),
};
self.clean_anchor_target().await?;
self.clean_hfuzz_target(&root).await?;
}

#[throws]
async fn clean_anchor_target(&self) {
Command::new("anchor").arg("clean").spawn()?.wait().await?;
}
#[throws]
async fn clean_hfuzz_target(&self, root: &PathBuf) {
let hfuzz_target_path = Path::new(root)
.join(crate::test_generator::TESTS_WORKSPACE)
.join(crate::test_generator::HFUZZ_TARGET);
if hfuzz_target_path.exists() {
fs::remove_dir_all(hfuzz_target_path).await?;
} else {
println!(
"skipping {}/{} directory: not found",
crate::test_generator::TESTS_WORKSPACE,
crate::test_generator::HFUZZ_TARGET
)
}
}
}
3 changes: 3 additions & 0 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ pub use test_generator::TestGenerator;

pub mod error_reporter;
pub use error_reporter::*;

pub mod cleaner;
pub use cleaner::*;
1 change: 1 addition & 0 deletions crates/client/src/test_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const TESTS_DIRECTORY: &str = "tests";
const FUZZ_DIRECTORY: &str = "src/bin";
const TESTS_FILE_NAME: &str = "test.rs";
const FUZZ_TEST_FILE_NAME: &str = "fuzz_target.rs";
pub(crate) const HFUZZ_TARGET: &str = "hfuzz_target";

#[derive(Error, Debug)]
pub enum Error {
Expand Down
Loading