Adds full support for rename and corrects a few bugs

This commit is contained in:
Dandaedre 2023-05-23 05:20:03 +02:00
parent c16adefa56
commit 1678e4ad6a
6 changed files with 83 additions and 11 deletions

1
.gitignore vendored
View File

@ -21,5 +21,6 @@ Cargo.lock
/target
/coverage
/llvm
/releases
tarpaulin-report.html
!coverage/html/badges/flat.svg

View File

@ -12,7 +12,7 @@ test = []
opt-level = 0
[profile.release]
opt-level = "z"
opt-level = "s"
[dependencies]
clap = { version = "4.2.7", features = ["derive"] }

View File

@ -20,7 +20,15 @@ script = [
script_runner = "@shell"
ignore_errors = true
script = [
"CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu"
"CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu --release"
]
[tasks.linux_build]
script_runner = "@shell"
ignore_errors = true
script = [
"cargo build --release",
# "CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-linux-gnu --release"
]
[tasks.win_test]
@ -30,3 +38,15 @@ script = [
"CROSS_CONTAINER_ENGINE=podman cross test --features test --target x86_64-pc-windows-gnu"
]
[tasks.release]
dependencies = ["win_build","linux_build"]
script_runner = "@shell"
ignore_errors = true
script = [
"mkdir -p releases",
"rm -f releases/*",
"cp target/x86_64-pc-windows-gnu/release/snp.exe releases/",
"cp target/release/snp releases/",
"strip releases/snp",
"strip releases/snp.exe",
]

View File

@ -2,7 +2,6 @@
[ ] Benchmarks
[ ] Better use of file modes
[ ] Document non coverage
[ ] Have the the EditWhole save under a different path if name was changed (and edit too)
[ ] Find a solution for the .badpocalypse
### Currently working on
@ -30,6 +29,7 @@
[*] Add an option to delete a snippet
[*] Coverage
[*] Test populate_actions
[*] Have the the EditWhole save under a different path if name was changed (and edit too)
## Posponed or rendered useless
[x] Chain the methods when possible with result ?

View File

@ -54,7 +54,7 @@ impl fmt::Display for SnippetError {
p.file_name().unwrap(),
),
SnippetError::ReparsingError => write!(f,"The snipped couldn't be re-parsed, it might be caused by a bad value in your edit. \nThe snipped was saved as a raw file instead but cant be used as is."),
SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {0} arguments",i),
SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {} arguments",i,i+1),
}
}
}

View File

@ -136,7 +136,6 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
}
}
};
// During tests, the editor cannot be tested
#[cfg(not(feature = "test"))]
let buf = edit::edit(buf)?;
@ -144,12 +143,35 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
// Try reparsing, on error no modifications will be made
match Snippet::from_string(&buf) {
Ok(snp) => {
snp.write_over(&p)?; // TODO: Consider not giving a path, as name can be changed in config
println!(
"The snippet {:?} was succesfully saved",
p.file_name().unwrap()
);
Ok(())
if cli.snippet == snp.name {
// No renaming has occured, the file can safely be overriten
snp.write_over(&p)?;
println!(
"The snippet {:?} was succesfully saved",
p.file_name().unwrap()
);
Ok(())
} else {
let (f, p) = match check_dir_and_open(&snp.name)? {
FileStatus::Opened((p, f)) => {
if cli.force {
// Erasing will take place
(File::create(&p)?, p)
} else {
// No erasing/overriting
println!(
"Impossible to save the snippet as {:}, as it already exists. The snippet will be saved under it's old name.",
snp.name
);
(f, p)
}
}
FileStatus::NotFound(p) => (File::create(&p)?, p),
};
snp.save_in(&f)?;
println!("The snippet was successfuly saved in {:?}", p);
Ok(())
}
}
Err(_) => {
@ -410,4 +432,33 @@ pub mod test {
println!("{:?}", res);
assert!(res.is_ok());
}
#[test]
fn test_resaving_new_name() {
// Try to resave under another name
_ = std::fs::remove_file(snippet_path("resave_snippet").unwrap());
_ = std::fs::remove_file(snippet_path("new_snippet").unwrap());
let p = snippet_path("resave_snippet").unwrap();
let mut f = File::create(p).unwrap();
// Populate file with another name
let snp_str = Snippet::empty("new_snippet".to_string(), vec![]);
write!(f, "{}", snp_str).unwrap();
// Try to resave
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet"));
println!("Trying to resave without destination {:?}", res);
assert!(res.is_ok());
assert!(create_snippet(false_cli!("-c", "new_snippet")).is_err());
assert!(create_snippet(false_cli!("-c", "resave_snippet")).is_err());
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet", "-f"));
println!("Sucessfully erasing {:?}", res);
assert!(res.is_ok());
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet"));
println!("Failing to overwrite {:?}", res);
assert!(res.is_err());
}
}