Skip to content

Commit 0be9442

Browse files
authored
Merge pull request #44 from Chisanan232/develop
🔬🧪🧬 [Refactor] Automating getting test module paths.
2 parents 8b0fb79 + 2569db7 commit 0be9442

18 files changed

+232
-41
lines changed

.github/workflows/prepare_test_items.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Workflow input parameters:
77
# * shell_path: The file path of shell script which gets all the test items.
88
# * shell_arg: The arguments of the shell script which gets all the test items.
9+
# * use_customized_shell: Don't use the shell script template project prepares, run customized shell script.
910
#
1011
# Workflow running output:
1112
# Yes, it has running result output. The output is the paths of all test items.
@@ -19,12 +20,18 @@ on:
1920
inputs:
2021
shell_path:
2122
description: "The file path of shell script which gets all the test items."
22-
required: true
23+
required: false
2324
type: string
25+
default: './scripts/ci/get-all-tests.sh'
2426
shell_arg:
2527
description: "The arguments of the shell script which gets all the test items."
2628
required: true
2729
type: string
30+
use_customized_shell:
31+
description: "If it's true, it wouldn't download the shell script and use the value of argument *shell_path* directly as shell script path to run."
32+
type: boolean
33+
required: false
34+
default: false
2835
outputs:
2936
all_test_items:
3037
description: "The output string about all test items it needs to run."
@@ -40,6 +47,11 @@ jobs:
4047
matrix: ${{ steps.set-matrix.outputs.all_test_items }}
4148
steps:
4249
- uses: actions/checkout@v3
50+
51+
- name: Download shell script for getting path of all test modules
52+
if: ${{ inputs.use_customized_shell != true }}
53+
run: curl https://raw.githubusercontent.com/Chisanan232/GitHub-Action_Reusable_Workflows-Python/develop/scripts/ci/get-all-tests.sh --output ${{ inputs.shell_path }}
54+
4355
- id: set-matrix
4456
run: |
4557
sudo apt-get install jq

.github/workflows/test_pyproject_ci_multi-tests_by_poetry.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ jobs:
3232
# name: Prepare all unit test items
3333
uses: ./.github/workflows/prepare_test_items.yaml
3434
with:
35-
shell_path: scripts/ci/test/get-unit-test-paths.sh
36-
shell_arg: unix
35+
shell_arg: test/unit_test/
3736

3837

3938
prep-testbed_integration-test:
@@ -42,6 +41,7 @@ jobs:
4241
with:
4342
shell_path: scripts/ci/test/get-integration-test-paths.sh
4443
shell_arg: unix
44+
use_customized_shell: true
4545

4646

4747
run_unit-test:

.github/workflows/test_python_project_ci_multi-tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ jobs:
3434
# name: Prepare all unit test items
3535
uses: ./.github/workflows/prepare_test_items.yaml
3636
with:
37-
shell_path: scripts/ci/test/get-unit-test-paths.sh
38-
shell_arg: unix
37+
shell_arg: test/unit_test/
3938

4039

4140
prep-testbed_integration-test:
@@ -44,6 +43,7 @@ jobs:
4443
with:
4544
shell_path: scripts/ci/test/get-integration-test-paths.sh
4645
shell_arg: unix
46+
use_customized_shell: true
4747

4848

4949
run_unit-test:

.github/workflows/test_python_project_ci_one-test.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ jobs:
3333
# name: Prepare all unit test items
3434
uses: ./.github/workflows/prepare_test_items.yaml
3535
with:
36-
shell_path: scripts/ci/test/get-unit-test-paths.sh
37-
shell_arg: unix
36+
shell_arg: test/unit_test/
3837

3938

4039
run_unit-test:

scripts/ci/get-all-tests.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
base_directory=$1
6+
if [ "$base_directory" == "" ];
7+
then
8+
base_directory="test/"
9+
fi
10+
11+
runtime_os=$2
12+
if [ "$runtime_os" == "" ];
13+
then
14+
runtime_os="unix"
15+
fi
16+
17+
declare -a all_test_subpkgs=( "$base_directory" )
18+
19+
get_all_test_subpackage() {
20+
# Get all test directories (python subpackage)
21+
# Note: use dept-first search algorithm
22+
declare index=0
23+
if [ "$1" ];
24+
then
25+
index=$1
26+
fi
27+
28+
declare test_subpkg="${all_test_subpkgs[$index]}"
29+
if [ "$test_subpkg" != "" ];
30+
then
31+
# Still has test subpackage won't scan
32+
declare test_path="$test_subpkg*/"
33+
declare -a test_subpkg_array=( $(ls -d "$test_path" | grep -v '__pycache__') )
34+
35+
if [ ${#test_subpkg_array[@]} != 0 ];
36+
then
37+
# No any directory under this path, try to get the test modules
38+
all_test_subpkgs+=( "${test_subpkg_array[@]}" )
39+
# shellcheck disable=SC2004
40+
get_all_test_subpackage $(( $index + 1 ))
41+
else
42+
# Has some directories under this path, keep searching
43+
if [ ${#all_test_subpkgs[@]} != "$index" ];
44+
then
45+
# shellcheck disable=SC2004
46+
get_all_test_subpackage $(( $index + 1 ))
47+
fi
48+
fi
49+
fi
50+
}
51+
52+
declare all_tests
53+
54+
get_all_test_modules_under_subpkg() {
55+
# Get all test modules with one specific subpackage (directory has __init__.py file)
56+
declare -a testpatharray=( $(ls -F "$1" | grep -v '/$' | grep -v '__init__.py' | grep -v 'test_config.py' | grep -v -E '^_[a-z_]{1,64}.py' | grep -v '__pycache__'))
57+
58+
declare -a alltestpaths
59+
for test_module_path in "${testpatharray[@]}";
60+
do
61+
alltestpaths+=("$1$test_module_path")
62+
done
63+
64+
# shellcheck disable=SC2124
65+
# shellcheck disable=SC2178
66+
all_tests+="${alltestpaths[@]} "
67+
}
68+
69+
get_all_test_modules() {
70+
# Get all test modules under these test subpackages
71+
for test_subpkg in "${all_test_subpkgs[@]}";
72+
do
73+
get_all_test_modules_under_subpkg "$test_subpkg"
74+
done
75+
}
76+
77+
# Get all test module paths
78+
get_all_test_subpackage
79+
get_all_test_modules
80+
81+
# Process data as list type value
82+
dest=( "${all_tests[@]}" )
83+
84+
85+
# Output the final result about all test modules
86+
if echo "$runtime_os" | grep -q "windows";
87+
then
88+
printf "${dest[@]}" | jq -R .
89+
elif echo "$runtime_os" | grep -q "unix";
90+
then
91+
printf '%s\n' "${dest[@]}" | jq -R . | jq -cs .
92+
else
93+
printf 'error' | jq -R .
94+
fi

scripts/ci/test/get-unit-test-paths.sh

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/unit_test/subpkg_1/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test_gh_workflow.sample
2+
import logging
3+
import pytest
4+
5+
6+
@pytest.fixture(scope="function")
7+
def get_hello_python() -> str:
8+
return test_gh_workflow.sample.hello_python()
9+
10+
11+
def test_sample(get_hello_python: str) -> None:
12+
logging.info("Start Unit test.")
13+
assert get_hello_python == "Hello Python", "The return value should be 'Hello Python'."
14+
logging.info("This is Unit test done.")
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test_gh_workflow.sample
2+
import logging
3+
import pytest
4+
5+
6+
@pytest.fixture(scope="function")
7+
def get_hello_python() -> str:
8+
return test_gh_workflow.sample.hello_python()
9+
10+
11+
def test_sample(get_hello_python: str) -> None:
12+
logging.info("Start Unit test.")
13+
assert get_hello_python == "Hello Python", "The return value should be 'Hello Python'."
14+
logging.info("This is Unit test done.")
15+

test/unit_test/subpkg_2/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)