From b54e173646ad38e8005f94db42e1f8b66efb24cd Mon Sep 17 00:00:00 2001 From: Denis Kurilenko Date: Wed, 4 May 2022 09:13:29 +0300 Subject: [PATCH 1/2] Create RFC Simple Tests --- rfc/simplify_tests.md | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 rfc/simplify_tests.md diff --git a/rfc/simplify_tests.md b/rfc/simplify_tests.md new file mode 100644 index 0000000..318760b --- /dev/null +++ b/rfc/simplify_tests.md @@ -0,0 +1,94 @@ +# Simplify tests +## Summary +Using declarative approach for create and check folders content. For declaration description using yaml string data + +### Example YAML data: +```yaml +Test name for folder: "FOLDER" + Test name for file: "FILE" + Another file: + - type: "FILE" + - content: "blah blah blah" + Another folder: "FOLDER" +``` + +## Motivation +I think every to agree with me, that now we have many code noise in tests. +### Example: +```rust + let mut test_file = PathBuf::from(TEST_FOLDER); + test_file.push("it_read_and_write_work"); + test_file.push("test.txt"); + fs_extra::dir::create_all(test_file.parent().unwrap(), true).unwrap(); + let content1 = "test_1"; + let content2 = "test_2"; + write_all(&test_file, &content1).unwrap(); + assert!(test_file.exists()); + let read1 = read_to_string(&test_file).unwrap(); + assert_eq!(content1, read1); + write_all(&test_file, &content2).unwrap(); + let read2 = read_to_string(&test_file).unwrap(); + assert_eq!(content2, read2); +``` + +In new way this code can be like: + +```rust +let t = TestFS::new("it_read_and_write_work", r#" + test.txt: + - type: "FILE" + - content: "test_1""#; +let test_file = t.getPath("test.txt") +let data = read_to_string(&test_file).unwrap(); +assert_eq!("test_1", data); +write_all(&test_file, "test_2").unwrap(); +data = read_to_string(&test_file).unwrap(); +assert_eq!("test_2", data); +``` + +I think we can change it and make tests more readable, and increase speed to write new tests using new approach. + +### Another Example: +```rust +let mut test_file = PathBuf::from(TEST_FOLDER); +test_file.push("it_copy_work"); +let mut test_file_out = test_file.clone(); +test_file.push("test.txt"); +test_file_out.push("out"); +test_file_out.push("test.txt"); +fs_extra::dir::create_all(&test_file.parent().unwrap(), true).unwrap(); +fs_extra::dir::create_all(&test_file_out.parent().unwrap(), true).unwrap(); + +write_all(&test_file, "test_data").unwrap(); +assert!(test_file.exists()); +assert!(!test_file_out.exists()); +let options = CopyOptions::new(); +copy(&test_file, &test_file_out, &options).unwrap(); +assert!(test_file.exists()); +assert!(test_file_out.exists()); +assert_eq!(test_file.file_name(), test_file_out.file_name()); +assert!(files_eq(test_file, test_file_out).unwrap()); +``` +RFC version: +```rust +let t = TestFS::new("it_copy_work", r#" + test.txt: + - type: "FILE" + - content: "test_data" + out: "FOLDER""#; +let options = CopyOptions::new(); +let test_file = t.getPath("test.txt") +let test_file_out = t.getPath("out/test.txt") +copy(&test_file, &test_file_out, &options).unwrap(); +t.check(r#" + test.txt: + - type: "FILE" + - content: "test_data" + out: "FOLDER" + test.txt: + - type: "FILE" + - content: "test_data""#); +``` + + + From 24aef106630895286ec31a6dbf16a81c49f56c0d Mon Sep 17 00:00:00 2001 From: webdesus Date: Thu, 12 May 2022 16:35:37 +0300 Subject: [PATCH 2/2] Fixed little typos --- rfc/simplify_tests.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rfc/simplify_tests.md b/rfc/simplify_tests.md index 318760b..b284b43 100644 --- a/rfc/simplify_tests.md +++ b/rfc/simplify_tests.md @@ -36,9 +36,9 @@ In new way this code can be like: ```rust let t = TestFS::new("it_read_and_write_work", r#" test.txt: - - type: "FILE" - - content: "test_1""#; -let test_file = t.getPath("test.txt") + type: "FILE" + content: "test_1""#; +let test_file = t.get_path("test.txt") let data = read_to_string(&test_file).unwrap(); assert_eq!("test_1", data); write_all(&test_file, "test_2").unwrap(); @@ -73,21 +73,21 @@ RFC version: ```rust let t = TestFS::new("it_copy_work", r#" test.txt: - - type: "FILE" - - content: "test_data" - out: "FOLDER""#; + type: "FILE" + content: "test_data" + out: "FOLDER""#); let options = CopyOptions::new(); -let test_file = t.getPath("test.txt") -let test_file_out = t.getPath("out/test.txt") +let test_file = t.get_path("test.txt") +let test_file_out = t.get_path("out/test.txt") copy(&test_file, &test_file_out, &options).unwrap(); t.check(r#" test.txt: - - type: "FILE" - - content: "test_data" + type: "FILE" + content: "test_data" out: "FOLDER" test.txt: - - type: "FILE" - - content: "test_data""#); + type: "FILE" + content: "test_data""#); ```