Skip to content

Commit

Permalink
rust-remove: check path for a regular file
Browse files Browse the repository at this point in the history
Summary: check if a file path to remove is a regular file. If so, the state machine flow should be moving to `RegFile` state (added in this diff) and the operations required should be handled there (to be implemented in future diffs).

Reviewed By: MichaelCuevas

Differential Revision: D64564138

fbshipit-source-id: 840fb033a865f361fd03447871f82c08e935c021
  • Loading branch information
lXXXw authored and facebook-github-bot committed Oct 20, 2024
1 parent 52d1fe6 commit 002a8cf
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions eden/fs/cli_rs/edenfs-commands/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use clap::Parser;
use dialoguer::Confirm;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::trace;

use crate::ExitCode;
use crate::Subcommand;
Expand Down Expand Up @@ -82,12 +80,28 @@ impl SanityCheck {
#[derive(Debug)]
struct Determination {}
impl Determination {
fn next(&self, context: &mut RemoveContext) -> Result<Option<State>> {
let path = context.canonical_path.as_path();

if path.is_file() {
debug!("path {} determined to be a regular file", path.display());
return Ok(Some(State::RegFile(RegFile {})));
}

error!("Determination State for directory is not implemented!");
Err(anyhow!("Rust remove(Determination) is not implemented!"))
}
}

#[derive(Debug)]
struct RegFile {}
impl RegFile {
fn next(&self, _context: &mut RemoveContext) -> Result<Option<State>> {
if Confirm::new()
.with_prompt("Determination State is not implemented yet... proceed?")
.with_prompt("RegFile State is not implemented yet... proceed?")
.interact()?
{
return Err(anyhow!("Rust remove(Determination) is not implemented!"));
return Err(anyhow!("Rust remove(RegFile) is not implemented!"));
}
Ok(None)
}
Expand All @@ -104,7 +118,7 @@ enum State {
// ActiveEdenMount,
// InactiveEdenMount,
// CleanUp,
// RegFile,
RegFile(RegFile),
// Unknown,
}

Expand All @@ -117,6 +131,7 @@ impl State {
match self {
State::SanityCheck(_) => "SanityCheck",
State::Determination(_) => "Determination",
State::RegFile(_) => "RegFile",
}
}

Expand All @@ -130,6 +145,7 @@ impl State {
match self {
State::SanityCheck(inner) => inner.next(context),
State::Determination(inner) => inner.next(context),
State::RegFile(inner) => inner.next(context),
}
}
}
Expand Down Expand Up @@ -162,6 +178,8 @@ impl Subcommand for RemoveCmd {

#[cfg(test)]
mod tests {
use std::fs;

use anyhow::Context;
use tempfile::tempdir;
use tempfile::TempDir;
Expand Down Expand Up @@ -218,4 +236,36 @@ mod tests {
.contains(PATH_NOT_FOUND_ERROR_MSG)
);
}

#[test]
fn test_determine_regular_file() {
let temp_dir = prepare_directory();
let file_path_buf = temp_dir.path().join("temporary-file.txt");
fs::write(file_path_buf.as_path(), "anything").unwrap_or_else(|err| {
panic!(
"cannot write to a file at {}: {}",
file_path_buf.display(),
err
)
});

// When context includes a path to a regular file
let mut file_context = RemoveContext::new(file_path_buf.display().to_string());
let mut state = State::start().run(&mut file_context).unwrap().unwrap();
assert!(
matches!(state, State::Determination(_)),
"Expected Determination state"
);
state = state.run(&mut file_context).unwrap().unwrap();
assert!(matches!(state, State::RegFile(_)), "Expected RegFile state");

// When context includes a path to a directory
let mut dir_context = RemoveContext::new(temp_dir.path().to_str().unwrap().to_string());
state = State::start().run(&mut dir_context).unwrap().unwrap();
assert!(
matches!(state, State::Determination(_)),
"Expected Determination state"
);
assert!(state.run(&mut dir_context).is_err());
}
}

0 comments on commit 002a8cf

Please sign in to comment.