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 <Mate.Farkas@oneidentity.com>
This commit is contained in:
Máté FARKAS 2023-03-20 18:48:48 +01:00 committed by GitHub
parent b0be6c3013
commit 77d33766f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)'
}
}