Added unit tests for the file locking function
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
44ef95b440
commit
cd886522ec
@ -8,9 +8,54 @@ pub fn is_file_locked(filepath: &str) -> bool {
|
|||||||
.expect("Failed to execute command");
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
let results = &String::from_utf8_lossy(&cmd.stdout);
|
let results = &String::from_utf8_lossy(&cmd.stdout);
|
||||||
|
println!("{}", results);
|
||||||
if results.contains(filepath) {
|
if results.contains(filepath) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::fs;
|
||||||
|
use std::mem::drop;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_should_not_be_locked() {
|
||||||
|
setup("test-not-locked.txt");
|
||||||
|
assert!(!is_file_locked("test-not-locked.txt"));
|
||||||
|
teardown("test-not-locked.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_should_be_locked() {
|
||||||
|
setup("test-locked.txt");
|
||||||
|
let path = Path::new("test-locked.txt");
|
||||||
|
match fs::File::open(&path) {
|
||||||
|
Err(e) => panic!("couldn't open {}: {}", "test-locked.txt", e),
|
||||||
|
Ok(_) => assert!(is_file_locked("test-locked.txt")),
|
||||||
|
};
|
||||||
|
teardown("test-locked.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
// get things ready for our tests
|
||||||
|
fn setup(test_file: &str) {
|
||||||
|
let f = match fs::File::create(test_file) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(e) => panic!("{:?}", e),
|
||||||
|
};
|
||||||
|
drop(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean up after running tests
|
||||||
|
fn teardown(test_file: &str) {
|
||||||
|
match fs::remove_file(test_file) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(e) => panic!("{:?}", e),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user