Skip to content

Commit 3e8ed7a

Browse files
committed
better test clean up
This makes it so that you don't need to manually delete a bunch of files on test failure.
1 parent bd75237 commit 3e8ed7a

4 files changed

Lines changed: 64 additions & 35 deletions

File tree

ci/expect_scripts/file.exp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ expect "Testing some File functions..." {
6464
expect "✓ File.exists! returns true for a file that exists" {
6565
expect "✓ File.exists! returns false for a file that does not exist" {
6666

67-
# Cleanup phase
68-
expect "Cleaning up test files..." {
69-
expect "✓ Deleted all files." {
67+
expect "I ran all file function tests." {
68+
69+
expect "Cleaning up test files..." {
70+
expect "✓ Deleted all files." {
71+
72+
# Final completion message
7073

71-
# Final completion message
72-
expect "I ran all file function tests." {
7374
expect eof {
7475
check_exit_and_segfault
7576
}

ci/expect_scripts/path-test.exp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,23 @@ expect "Testing Path functions..." {
9191
expect "✓ Path.exists! returns true for a file that exists" {
9292
expect "✓ Path.exists! returns false for a file that does not exist" {
9393
expect "" {
94-
95-
# Cleanup phase
96-
expect "Cleaning up test files..." {
97-
expect "Files to clean up:" {
98-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_bytes\\.txt" {
99-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_hardlink\\.txt" {
100-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_json\\.json" {
101-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_original\\.txt" {
102-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_rename_new\\.txt" {
103-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_utf8\\.txt" {
104-
expect "" {
105-
expect "Files remaining after cleanup: Bool.false" {
94+
expect "I ran all Path function tests." {
95+
expect "" {
96+
97+
# Cleanup phase
98+
expect "Cleaning up test files..." {
99+
expect "Files to clean up:" {
100+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_bytes\\.txt" {
101+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_hardlink\\.txt" {
102+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_json\\.json" {
103+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_original\\.txt" {
104+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_rename_new\\.txt" {
105+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_utf8\\.txt" {
106106
expect "" {
107-
108-
# Final completion message
109-
expect "I ran all Path function tests." {
107+
expect "Files remaining after cleanup: Bool.false" {
110108
expect eof {
111-
check_exit_and_segfault
109+
check_exit_and_segfault
110+
}
112111
}
113112
}
114113
}
@@ -121,7 +120,6 @@ expect "Testing Path functions..." {
121120
}
122121
}
123122
}
124-
}
125123
}
126124
}
127125
}

tests/file.roc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import json.Json
1212

1313
main! : List Arg => Result {} _
1414
main! = |_args|
15+
when run_tests!({}) is
16+
Ok(_) ->
17+
cleanup_test_files!(FilesNeedToExist)
18+
Err(err) ->
19+
cleanup_test_files!(FilesMaybeExist)?
20+
Err(Exit(1, "Test run failed:\n\t${Inspect.to_str(err)}"))
21+
22+
run_tests! : {} => Result {} _
23+
run_tests! = |{}|
1524
Stdout.line!("Testing some File functions...")?
1625
Stdout.line!("This will create and manipulate test files in the current directory.")?
1726
Stdout.line!("")?
@@ -34,9 +43,6 @@ main! = |_args|
3443
# Test file exists
3544
test_file_exists!({})?
3645
37-
# Clean up test files
38-
cleanup_test_files!({})?
39-
4046
Stdout.line!("\nI ran all file function tests.")
4147
4248
test_basic_file_operations! : {} => Result {} _
@@ -236,8 +242,8 @@ test_file_exists! = |{}|
236242
237243
Ok({})
238244
239-
cleanup_test_files! : {} => Result {} _
240-
cleanup_test_files! = |{}|
245+
cleanup_test_files! : [FilesNeedToExist, FilesMaybeExist] => Result {} _
246+
cleanup_test_files! = |files_requirement|
241247
Stdout.line!("\nCleaning up test files...")?
242248
243249
test_files = [
@@ -250,5 +256,17 @@ cleanup_test_files! = |{}|
250256
"test_rename_new.txt",
251257
]
252258
253-
List.for_each_try!(test_files, |filename| File.delete!(filename)) ? |err| FileDeletionFailed(err)
259+
delete_result = List.for_each_try!(
260+
test_files,
261+
|filename| File.delete!(filename)
262+
)
263+
264+
when files_requirement is
265+
FilesNeedToExist ->
266+
delete_result ? |err| FileDeletionFailed(err)
267+
268+
FilesMaybeExist ->
269+
Ok({})?
270+
254271
Stdout.line!("Deleted all files.")
272+

0 commit comments

Comments
 (0)