-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestRig to common.testing to reduce boilerplate for testing (#130)
* Add TestRig to reduce boilerplate for testing * Refactor tests to use TestRig * Be more forgiving in temp dir clean-up * Fix typo in whoami test * Handle unsupported platforms * Call rig.clean_up with C.atexit * Now calling C.atexit at the right place * Moved TestRig to .c.v file for use of C.atexit()
- Loading branch information
Showing
19 changed files
with
176 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module testing | ||
|
||
import os | ||
|
||
// TestRig contains the relevant scaffolding for tests to avoid boilerplate in | ||
// the individual <util>_test.v files | ||
pub struct TestRig { | ||
pub: | ||
util string | ||
platform_util string | ||
executable_under_test string | ||
temp_dir string | ||
cmd CommandPair | ||
is_supported_platform bool | ||
} | ||
|
||
pub struct TestRigConfig { | ||
pub: | ||
util string | ||
is_supported_platform bool = true | ||
} | ||
|
||
pub fn prepare_rig(config TestRigConfig) TestRig { | ||
platform_util := $if !windows { | ||
config.util | ||
} $else { | ||
'coreutils ${config.util}' | ||
} | ||
exec_under_test := if config.is_supported_platform { | ||
prepare_executable(config.util) | ||
} else { | ||
'' | ||
} | ||
temp_dir := os.join_path(temp_folder, config.util) | ||
os.mkdir(temp_dir) or { panic('Unable to make test directory: ${temp_dir}') } | ||
os.chdir(temp_dir) or { panic('Unable to set working directory: ${temp_dir}') } | ||
rig := TestRig{ | ||
util: config.util | ||
platform_util: platform_util | ||
cmd: new_paired_command(platform_util, exec_under_test) | ||
executable_under_test: exec_under_test | ||
temp_dir: temp_dir | ||
is_supported_platform: config.is_supported_platform | ||
} | ||
C.atexit(rig.clean_up) | ||
return rig | ||
} | ||
|
||
pub fn (rig TestRig) call_for_test(args string) os.Result { | ||
res := os.execute('${rig.executable_under_test} ${args}') | ||
assert res.exit_code == 0 | ||
return res | ||
} | ||
|
||
pub fn (rig TestRig) clean_up() { | ||
if os.is_dir(rig.temp_dir) { | ||
os.rmdir_all(rig.temp_dir) or {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.