diff --git a/src/nu-git-manager-sugar/extra.nu b/src/nu-git-manager-sugar/extra.nu index eb4e9290..57bc1596 100644 --- a/src/nu-git-manager-sugar/extra.nu +++ b/src/nu-git-manager-sugar/extra.nu @@ -49,3 +49,31 @@ export def "gm report" []: nothing -> table gm for-each { git status } +# +# get the number of files tracked by each repo: list +# > gm for-each { git lf | lines | length } +# +# get the number of status lines of each repo: table +# > gm for-each { |r| { +# repo: $r, +# status: (git status --short | lines | length) +# } } +export def "gm for-each" [code: closure]: nothing -> any { + let root = gm status | get root.path + + gm list | each {|repo| + cd ($root | path join $repo) + do $code $repo + } +} diff --git a/tests/common/main.nu b/tests/common/main.nu new file mode 100644 index 00000000..37e28d32 --- /dev/null +++ b/tests/common/main.nu @@ -0,0 +1,16 @@ +export def run-with-env [code: closure, --prepare-cache] { + let TEST_ENV_BASE = get-random-test-dir + + let TEST_ENV = { + GIT_REPOS_HOME: ($TEST_ENV_BASE | path join "repos"), + GIT_REPOS_CACHE: ($TEST_ENV_BASE | path join "repos.cache"), + } + + if $prepare_cache { + with-env $TEST_ENV { gm update-cache } + } + + with-env $TEST_ENV $code + + rm --recursive --force --verbose $TEST_ENV_BASE +} diff --git a/tests/gm.nu b/tests/gm.nu index 0155c9a4..ee893729 100644 --- a/tests/gm.nu +++ b/tests/gm.nu @@ -6,23 +6,7 @@ use ../src/nu-git-manager/ * use common/setup.nu [get-random-test-dir] use common/import.nu ["assert imports"] - -def run-with-env [code: closure, --prepare-cache] { - let TEST_ENV_BASE = get-random-test-dir - - let TEST_ENV = { - GIT_REPOS_HOME: ($TEST_ENV_BASE | path join "repos"), - GIT_REPOS_CACHE: ($TEST_ENV_BASE | path join "repos.cache"), - } - - if $prepare_cache { - with-env $TEST_ENV { gm update-cache } - } - - with-env $TEST_ENV $code - - rm --recursive --force --verbose $TEST_ENV_BASE -} +use common/main.nu [run-with-env] export def error-with-empty-store [] { run-with-env { diff --git a/tests/sugar/mod.nu b/tests/sugar/mod.nu index 45e559c1..07ec6666 100644 --- a/tests/sugar/mod.nu +++ b/tests/sugar/mod.nu @@ -1,6 +1,9 @@ use std assert +use ../../src/nu-git-manager-sugar extra ["gm for-each"] + use ../common/import.nu ["assert imports"] +use ../common/main.nu [run-with-env] const MODULE = "nu-git-manager-sugar" @@ -10,7 +13,7 @@ module imports { } export def extra [] { - assert imports $MODULE "extra" [ "gm report" ] + assert imports $MODULE "extra" [ "gm for-each", "gm report" ] } export def git [] { @@ -45,3 +48,13 @@ export use imports def report [] { exit 1 } + +export def for-each [] { + use ../../src/nu-git-manager ["gm clone", "gm list", "gm status"] + + run-with-env --prepare-cache { + gm clone https://github.com/amtoine/nu-git-manager --depth 1 + + assert equal (gm for-each { pwd }) (gm list) + } +}