From a7ad8ab28fb686df01183f9230d7a31601476776 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 27 Feb 2021 10:44:52 +0000 Subject: subplotlib: path_exists - check path is a directory Signed-off-by: Daniel Silverstone --- subplotlib/src/steplibrary/files.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/subplotlib/src/steplibrary/files.rs b/subplotlib/src/steplibrary/files.rs index 8ba27cf..169111d 100644 --- a/subplotlib/src/steplibrary/files.rs +++ b/subplotlib/src/steplibrary/files.rs @@ -269,7 +269,12 @@ pub fn remove_directory(context: &Datadir, path: &str) { #[step] pub fn path_exists(context: &Datadir, path: &str) { let full_path = context.canonicalise_filename(path)?; - fs::metadata(full_path)?; + if !fs::metadata(&full_path)?.is_dir() { + throw!(format!( + "{} exists but is not a directory", + full_path.display() + )) + } } #[step] -- cgit v1.2.1 From e1881ecc58e84070e642bb764c721a28356163b9 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 27 Feb 2021 10:48:53 +0000 Subject: subplotlib/tests: Format files.rs Signed-off-by: Daniel Silverstone --- subplotlib/tests/files.rs | 507 ++++++++++++++++++---------------------------- 1 file changed, 199 insertions(+), 308 deletions(-) diff --git a/subplotlib/tests/files.rs b/subplotlib/tests/files.rs index 2f0bc6c..a8fce3d 100644 --- a/subplotlib/tests/files.rs +++ b/subplotlib/tests/files.rs @@ -1,90 +1,70 @@ use subplotlib::prelude::*; - - // -------------------------------- lazy_static! { - static ref SUBPLOT_EMBEDDED_FILES: Vec = vec![ - - SubplotDataFile::new("aGVsbG8udHh0", - "aGVsbG8sIHdvcmxkCg=="), - - ]; + static ref SUBPLOT_EMBEDDED_FILES: Vec = + vec![SubplotDataFile::new("aGVsbG8udHh0", "aGVsbG8sIHdvcmxkCg=="),]; } - - // --------------------------------- // Create on-disk files from embedded files #[test] fn create_on_disk_files_from_embedded_files() { - let mut scenario = Scenario::new(&base64_decode("Q3JlYXRlIG9uLWRpc2sgZmlsZXMgZnJvbSBlbWJlZGRlZCBmaWxlcw==")); - + let mut scenario = Scenario::new(&base64_decode( + "Q3JlYXRlIG9uLWRpc2sgZmlsZXMgZnJvbSBlbWJlZGRlZCBmaWxlcw==", + )); + let step = subplotlib::steplibrary::files::create_from_embedded::Builder::default() - .embedded_file( - - { - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - } - ) - .build(); + .embedded_file({ + use std::path::PathBuf; + // hello.txt + let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); + SUBPLOT_EMBEDDED_FILES + .iter() + .find(|df| df.name() == target_name) + .expect("Unable to find file at runtime") + .clone() + }) + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_exists::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" + &base64_decode("aGVsbG8udHh0"), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_contains::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" + &base64_decode("aGVsbG8udHh0"), ) - ) - .data( - + .data( // "hello, world" - &base64_decode("aGVsbG8sIHdvcmxk" - ) + &base64_decode("aGVsbG8sIHdvcmxk"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_does_not_exist::Builder::default() - .filename( - + .filename( // "other.txt" - &base64_decode("b3RoZXIudHh0" - ) + &base64_decode("b3RoZXIudHh0"), ) - .build(); + .build(); scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::create_from_embedded_with_other_name::Builder::default() - .filename_on_disk( - - // "other.txt" - &base64_decode("b3RoZXIudHh0" - ) - ) - .embedded_file( - - { + + let step = + subplotlib::steplibrary::files::create_from_embedded_with_other_name::Builder::default() + .filename_on_disk( + // "other.txt" + &base64_decode("b3RoZXIudHh0"), + ) + .embedded_file({ use std::path::PathBuf; // hello.txt let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); @@ -93,369 +73,293 @@ fn create_on_disk_files_from_embedded_files() { .find(|df| df.name() == target_name) .expect("Unable to find file at runtime") .clone() - } - ) - .build(); + }) + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_exists::Builder::default() - .filename( - + .filename( // "other.txt" - &base64_decode("b3RoZXIudHh0" - ) + &base64_decode("b3RoZXIudHh0"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_match::Builder::default() - .filename1( - + .filename1( // "hello.txt" - &base64_decode("aGVsbG8udHh0" - ) + &base64_decode("aGVsbG8udHh0"), ) - .filename2( - + .filename2( // "other.txt" - &base64_decode("b3RoZXIudHh0" + &base64_decode("b3RoZXIudHh0"), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::only_these_exist::Builder::default() - .filenames( - + .filenames( // "hello.txt, other.txt" - &base64_decode("aGVsbG8udHh0LCBvdGhlci50eHQ=" + &base64_decode("aGVsbG8udHh0LCBvdGhlci50eHQ="), ) - ) - .build(); + .build(); scenario.add_step(step, None); - scenario.run().unwrap(); } - // --------------------------------- // File metadata #[test] fn file_metadata() { let mut scenario = Scenario::new(&base64_decode("RmlsZSBtZXRhZGF0YQ==")); - + let step = subplotlib::steplibrary::files::create_from_embedded::Builder::default() - .embedded_file( - - { - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - } - ) - .build(); + .embedded_file({ + use std::path::PathBuf; + // hello.txt + let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); + SUBPLOT_EMBEDDED_FILES + .iter() + .find(|df| df.name() == target_name) + .expect("Unable to find file at runtime") + .clone() + }) + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::remember_metadata::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" + &base64_decode("aGVsbG8udHh0"), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::has_remembered_metadata::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" + &base64_decode("aGVsbG8udHh0"), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::create_from_text::Builder::default() - .text( - + .text( // "yo" - &base64_decode("eW8=" + &base64_decode("eW8="), ) - ) - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" - ) + &base64_decode("aGVsbG8udHh0"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::has_different_metadata::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" - ) + &base64_decode("aGVsbG8udHh0"), ) - .build(); + .build(); scenario.add_step(step, None); - scenario.run().unwrap(); } - // --------------------------------- // File modification time #[test] fn file_modification_time() { let mut scenario = Scenario::new(&base64_decode("RmlsZSBtb2RpZmljYXRpb24gdGltZQ==")); - + let step = subplotlib::steplibrary::files::touch_with_timestamp::Builder::default() - .filename( - + .filename( // "foo.dat" - &base64_decode("Zm9vLmRhdA==" + &base64_decode("Zm9vLmRhdA=="), ) - ) - .mtime( - + .mtime( // "1970-01-02 03:04:05" - &base64_decode("MTk3MC0wMS0wMiAwMzowNDowNQ==" - ) + &base64_decode("MTk3MC0wMS0wMiAwMzowNDowNQ=="), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::mtime_is_ancient::Builder::default() - .filename( - + .filename( // "foo.dat" - &base64_decode("Zm9vLmRhdA==" + &base64_decode("Zm9vLmRhdA=="), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::touch::Builder::default() - .filename( - + .filename( // "foo.dat" - &base64_decode("Zm9vLmRhdA==" + &base64_decode("Zm9vLmRhdA=="), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::mtime_is_recent::Builder::default() - .filename( - + .filename( // "foo.dat" - &base64_decode("Zm9vLmRhdA==" + &base64_decode("Zm9vLmRhdA=="), ) - ) - .build(); + .build(); scenario.add_step(step, None); - scenario.run().unwrap(); } - // --------------------------------- // File contents #[test] fn file_contents() { let mut scenario = Scenario::new(&base64_decode("RmlsZSBjb250ZW50cw==")); - + let step = subplotlib::steplibrary::files::create_from_embedded::Builder::default() - .embedded_file( - - { - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - } - ) - .build(); + .embedded_file({ + use std::path::PathBuf; + // hello.txt + let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); + SUBPLOT_EMBEDDED_FILES + .iter() + .find(|df| df.name() == target_name) + .expect("Unable to find file at runtime") + .clone() + }) + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_contains::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" + &base64_decode("aGVsbG8udHh0"), ) - ) - .data( - + .data( // "hello, world" - &base64_decode("aGVsbG8sIHdvcmxk" - ) + &base64_decode("aGVsbG8sIHdvcmxk"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_matches_regex::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" - ) + &base64_decode("aGVsbG8udHh0"), ) - .regex( - + .regex( // "hello, .*" - &base64_decode("aGVsbG8sIC4q" + &base64_decode("aGVsbG8sIC4q"), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::file_matches_regex::Builder::default() - .filename( - + .filename( // "hello.txt" - &base64_decode("aGVsbG8udHh0" + &base64_decode("aGVsbG8udHh0"), ) - ) - .regex( - + .regex( // "hello, .*" - &base64_decode("aGVsbG8sIC4q" - ) + &base64_decode("aGVsbG8sIC4q"), ) - .build(); + .build(); scenario.add_step(step, None); - scenario.run().unwrap(); } - // --------------------------------- // Directories #[test] fn directories() { let mut scenario = Scenario::new(&base64_decode("RGlyZWN0b3JpZXM=")); - + let step = subplotlib::steplibrary::files::make_directory::Builder::default() - .path( - + .path( // "first" - &base64_decode("Zmlyc3Q=" + &base64_decode("Zmlyc3Q="), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_exists::Builder::default() - .path( - + .path( // "first" - &base64_decode("Zmlyc3Q=" - ) + &base64_decode("Zmlyc3Q="), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_is_empty::Builder::default() - .path( - + .path( // "first" - &base64_decode("Zmlyc3Q=" - ) + &base64_decode("Zmlyc3Q="), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_does_not_exist::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" - ) + &base64_decode("c2Vjb25k"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::remove_directory::Builder::default() - .path( - + .path( // "first" - &base64_decode("Zmlyc3Q=" - ) + &base64_decode("Zmlyc3Q="), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_does_not_exist::Builder::default() - .path( - + .path( // "first" - &base64_decode("Zmlyc3Q=" - ) + &base64_decode("Zmlyc3Q="), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::make_directory::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" - ) + &base64_decode("c2Vjb25k"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_exists::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" - ) + &base64_decode("c2Vjb25k"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_is_empty::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" - ) + &base64_decode("c2Vjb25k"), ) - .build(); + .build(); scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::create_from_embedded_with_other_name::Builder::default() - .filename_on_disk( - - // "second/third/hello.txt" - &base64_decode("c2Vjb25kL3RoaXJkL2hlbGxvLnR4dA==" - ) - ) - .embedded_file( - - { + + let step = + subplotlib::steplibrary::files::create_from_embedded_with_other_name::Builder::default() + .filename_on_disk( + // "second/third/hello.txt" + &base64_decode("c2Vjb25kL3RoaXJkL2hlbGxvLnR4dA=="), + ) + .embedded_file({ use std::path::PathBuf; // hello.txt let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); @@ -464,62 +368,49 @@ fn directories() { .find(|df| df.name() == target_name) .expect("Unable to find file at runtime") .clone() - } - ) - .build(); + }) + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_is_not_empty::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" + &base64_decode("c2Vjb25k"), ) - ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_exists::Builder::default() - .path( - + .path( // "second/third" - &base64_decode("c2Vjb25kL3RoaXJk" - ) + &base64_decode("c2Vjb25kL3RoaXJk"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_is_not_empty::Builder::default() - .path( - + .path( // "second/third" - &base64_decode("c2Vjb25kL3RoaXJk" - ) + &base64_decode("c2Vjb25kL3RoaXJk"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::remove_directory::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" - ) + &base64_decode("c2Vjb25k"), ) - .build(); + .build(); scenario.add_step(step, None); - + let step = subplotlib::steplibrary::files::path_does_not_exist::Builder::default() - .path( - + .path( // "second" - &base64_decode("c2Vjb25k" - ) + &base64_decode("c2Vjb25k"), ) - .build(); + .build(); scenario.add_step(step, None); - scenario.run().unwrap(); } - -- cgit v1.2.1