From fabc0a3f45302e11048fb77208406d732455c519 Mon Sep 17 00:00:00 2001 From: Yethal <26117918+Yethal@users.noreply.github.com> Date: Sat, 8 Jul 2023 11:27:56 +0200 Subject: [PATCH] test-runner: add configurable threading (#9628) # Description Max amount of threads used by the test runner can now be configured via the `--threads` flag # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-std/std/testing.nu | 40 ++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/nu-std/std/testing.nu b/crates/nu-std/std/testing.nu index 26fe17c55e..16b7f41007 100644 --- a/crates/nu-std/std/testing.nu +++ b/crates/nu-std/std/testing.nu @@ -1,5 +1,8 @@ use log.nu +def "nu-complete threads" [] { + seq 1 (sys|get cpu|length) +} # Here we store the map of annotations internal names and the annotation actually used during test creation # The reason we do that is to allow annotations to be easily renamed without modifying rest of the code @@ -202,6 +205,7 @@ export def ($test_function_name) [] { # * result - test execution result def run-tests-for-module [ module: record + threads: int ] { let global_context = if not ($module.before-all|is-empty) { log info $"Running before-all for module ($module.name)" @@ -255,7 +259,7 @@ def run-tests-for-module [ '' } } - | par-each {|test| + | par-each --threads $threads {|test| log info $"Running ($test.test) in module ($module.name)" log debug $"Global context is ($global_context)" @@ -298,14 +302,28 @@ def run-tests-for-module [ # * after-each - function to run after every test case. Receives the context record just like the test cases # * after-all - function to run after all test cases have been executed. Receives the global context record export def run-tests [ - --path: path, # Path to look for tests. Default: current directory. - --module: string, # Test module to run. Default: all test modules found. - --test: string, # Pattern to use to include tests. Default: all tests found in the files. - --exclude: string, # Pattern to use to exclude tests. Default: no tests are excluded - --exclude-module: string, # Pattern to use to exclude test modules. Default: No modules are excluded - --list, # list the selected tests without running them. + --path: path, # Path to look for tests. Default: current directory. + --module: string, # Test module to run. Default: all test modules found. + --test: string, # Pattern to use to include tests. Default: all tests found in the files. + --exclude: string, # Pattern to use to exclude tests. Default: no tests are excluded + --exclude-module: string, # Pattern to use to exclude test modules. Default: No modules are excluded + --list, # list the selected tests without running them. + --threads: int@"nu-complete threads", # Amount of threads to use for parallel execution. Default: All threads are utilized ] { + let available_threads = (sys | get cpu | length) + + # Can't use pattern matching here due to https://github.com/nushell/nushell/issues/9198 + let threads = (if $threads == null { + $available_threads + } else if $threads < 1 { + 1 + } else if $threads <= $available_threads { + $threads + } else { + $available_threads + }) + let module_search_pattern = ('**' | path join ({ stem: ($module | default "*") extension: nu @@ -336,7 +354,7 @@ export def run-tests [ let modules = ( ls ($path | path join $module_search_pattern) - | par-each {|row| {file: $row.name name: ($row.name | path parse | get stem)}} + | par-each --threads $threads {|row| {file: $row.name name: ($row.name | path parse | get stem)}} | insert commands {|module| get-annotated $module.file } @@ -366,8 +384,8 @@ export def run-tests [ let results = ( $modules - | par-each {|module| - run-tests-for-module $module + | par-each --threads $threads {|module| + run-tests-for-module $module $threads } | flatten ) @@ -375,7 +393,7 @@ export def run-tests [ let text = ([ $"(ansi purple)some tests did not pass (char lparen)see complete errors below(char rparen):(ansi reset)" "" - ($results | par-each {|test| ($test | show-pretty-test 4)} | str join "\n") + ($results | par-each --threads $threads {|test| ($test | show-pretty-test 4)} | str join "\n") "" ] | str join "\n")