From 77d33766f17f2dbdbb20b990aab6f0e98d51112f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20FARKAS?= Date: Mon, 20 Mar 2023 18:48:48 +0100 Subject: [PATCH] std lib: extend test runner capabilities (#8499) I am implementing a nu plugin, and want to unit test that it works well. # Basic usage (unchanged) ![image](https://user-images.githubusercontent.com/282320/225895139-f1ea0088-22e1-4778-b27e-c1868af48753.png) # Select the folder to run tests within (subfolders included) ![image](https://user-images.githubusercontent.com/282320/225894283-4c6ac739-afde-44ef-bf7e-362d5118e83f.png) # Select module to run tests within ![image](https://user-images.githubusercontent.com/282320/225894438-f39690db-955d-4d66-818f-17a179b00c66.png) # Select test command to run ![image](https://user-images.githubusercontent.com/282320/225894534-edb15b9c-d3ef-4368-9108-2d220ef75dcf.png) # Complex usage ![image](https://user-images.githubusercontent.com/282320/225894827-03f2e937-3984-4b33-b206-c155c723feac.png) --------- Co-authored-by: Mate Farkas --- crates/nu-utils/standard_library/tests.nu | 55 ++++++++++++++++++----- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/crates/nu-utils/standard_library/tests.nu b/crates/nu-utils/standard_library/tests.nu index 4d37a056c..a070a1264 100644 --- a/crates/nu-utils/standard_library/tests.nu +++ b/crates/nu-utils/standard_library/tests.nu @@ -1,20 +1,55 @@ use std.nu * -def main [] { - for test_file in (ls ($env.FILE_PWD | path join "test_*.nu") -f | get name) { +def collect-modules [ + path: path, + module?: string +] { + let tests_path = ($path | default $env.FILE_PWD) + let module_search = ($module | default "test_*") + (ls ($tests_path | path join $"**/($module_search).nu") -f | get name) +} + +def collect-commands [ + test_file: string, + module_name: string, + command?: string +] { + let commands = ( + nu -c $'use ($test_file) *; $nu.scope.commands | select name module_name | to nuon' + | from nuon + | where module_name == $module_name + | where ($it.name | str starts-with "test_") + | get name + ) + if $command == null { + $commands + } else { + $commands | where $it == $command + } +} + +# Test executor +# +# It executes exported "test_*" commands in "test_*" modules +def main [ + --path: path, # Path to look for tests. Default: directory of this file. + --module: string, # Module to run tests. Default: all test modules found. + --command: string, # Test command to run. Default: all test command found in the files. + --list, # Do not run any tests, just list them (dry run) +] { + let dry_run = ($list | default false) + for test_file in (collect-modules $path $module) { let $module_name = ($test_file | path parse).stem - log info $"Run tests in ($module_name) module" - let tests = ( - nu -c $'use ($test_file) *; $nu.scope.commands | select name module_name | to nuon' - | from nuon - | where module_name == $module_name - | where ($it.name | str starts-with "test_") - | get name - ) + log info $"Run tests in ($module_name)" + let tests = (collect-commands $test_file $module_name $command) for test_case in $tests { log debug $"Run test ($module_name) ($test_case)" + if $dry_run { + continue + } + nu -c $'use ($test_file) ($test_case); ($test_case)' } }