Skip to content

Commit

Permalink
fix(workspace): prevent initialization of an initialized workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorin committed Apr 22, 2023
1 parent cbd7860 commit 5808f76
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/cmd/workspace/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pub fn execute_workspace_init(arg_matches: &ArgMatches) -> Result<()> {
let cache_path = Path::new(config.cache_directory.as_str());
let source_path = Path::new(config.source_directory.as_str());
let manifest_path = source_path.join(&config.workspace_manifest);
// stop if manifest already exists
if manifest_path.exists() {
return Err(anyhow::Error::msg(format!(
"The manifest {} already exists",
manifest_path.to_str().unwrap(),
)))?;
}
// create cache directory
create_directory(cache_path)?;
// create source directory
Expand All @@ -42,6 +49,7 @@ pub fn execute_workspace_init(arg_matches: &ArgMatches) -> Result<()> {
#[cfg(test)]
mod test {
use crate::cli::build_cli;
use crate::constants::WORKSPACE_MANIFEST;
use crate::utils::delete_file_or_directory;

use super::*;
Expand All @@ -64,11 +72,49 @@ mod test {
.subcommand_matches("init")
.unwrap(),
)
.unwrap();
.unwrap();
let path_workspace_manifest =
Path::new("target/tests/cmd/workspace/init/source/.pgen-workspace.yaml");
assert!(path_workspace_manifest.exists());
let path_cache_directory = Path::new("target/tests/cmd/workspace/init/cache");
assert!(path_cache_directory.exists());
}

#[test]
fn test_init_when_manifest_already_exist() {
let source_path = Path::new("target/tests/cmd/workspace/init/source");
let manifest_path = source_path.join(WORKSPACE_MANIFEST);

delete_file_or_directory(source_path).unwrap();
create_parent_directory(&manifest_path).unwrap();

std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(manifest_path)
.expect("Couldn't open file");

let arg_matches = build_cli().get_matches_from([
"plantuml-generator",
"-l=Debug",
"workspace",
"init",
"-s=target/tests/cmd/workspace/init/source",
"-C=target/tests/cmd/workspace/init/cache",
]);
match execute_workspace_init(
arg_matches
.subcommand_matches("workspace")
.unwrap()
.subcommand_matches("init")
.unwrap(),
) {
Ok(_) => {
panic!("should raised an error")
}
Err(e) => {
assert_eq!(e.to_string(), "The manifest target/tests/cmd/workspace/init/source/.pgen-workspace.yaml already exists")
}
};
}
}

0 comments on commit 5808f76

Please sign in to comment.