Skip to content

Commit 2b97dbf

Browse files
authored
Feat(kclvm-test): add konfig test for kclvm on unix. (#415)
Feat(kclvm-test): add konfig test for kclvm on unix. issue #379.
1 parent 04a9258 commit 2b97dbf

File tree

6 files changed

+152
-1
lines changed

6 files changed

+152
-1
lines changed

.github/workflows/macos_test.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ jobs:
5454
working-directory: ./kclvm
5555
run: export PATH=$PATH:$PWD/../_build/dist/Darwin/kclvm/bin:/usr/local/opt/llvm@12/bin && make install-rustc-wasm && make && make codecov-lcov
5656
shell: bash
57+
- name: Rust konfig test
58+
working-directory: ./kclvm
59+
run: export PATH=$PATH:$PWD/../_build/dist/Darwin/kclvm/bin:/usr/local/opt/llvm@12/bin && make install-rustc-wasm && make && make test-konfig
60+
shell: bash

.github/workflows/ubuntu_test.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ jobs:
4545
working-directory: ./kclvm
4646
run: export PATH=$PATH:$PWD/../_build/dist/ubuntu/kclvm/bin && make install-rustc-wasm && make && make codecov-lcov
4747
shell: bash
48+
- name: Rust konfig test
49+
working-directory: ./kclvm
50+
run: export PATH=$PATH:$PWD/../_build/dist/ubuntu/kclvm/bin && make install-rustc-wasm && make && make test-konfig
51+
shell: bash
4852
- name: Coveralls upload
4953
uses: coverallsapp/github-action@master
5054
with:

internal/scripts/test.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ help_message=$(cat <<-END
2323
trigger unit test
2424
test_grammar
2525
trigger grammar test
26+
test_konfig
27+
trigger konfig test
2628
all
2729
trigger all tests
2830
END
@@ -44,14 +46,18 @@ done
4446

4547
if [ "$action" == "" ]; then
4648
PS3='Please select the test scope: '
47-
options=("test_grammar")
49+
options=("test_grammar", "test_konfig")
4850
select action in "${options[@]}"
4951
do
5052
case $action in
5153
"test_grammar")
5254
$topdir/internal/scripts/test_grammar.sh
5355
break
5456
;;
57+
"test_konfig")
58+
$topdir/internal/scripts/test_konfig.sh
59+
break
60+
;;
5561
*) echo "Invalid action $REPLY:$action"
5662
exit 1
5763
break

internal/scripts/test_konfig.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
# Environment
4+
if [ -f "/etc/os-release" ]; then
5+
source /etc/os-release
6+
os=$ID
7+
else
8+
os=$(uname)
9+
fi
10+
topdir=$(realpath $(dirname $0)/../../)
11+
kclvm_source_dir="$topdir"
12+
13+
echo PATH=$PATH:$kclvm_source_dir/_build/dist/$os/kclvm/bin >> ~/.bash_profile
14+
source ~/.bash_profile
15+
16+
export PATH=$PATH:$topdir/_build/dist/$os/kclvm/bin
17+
18+
# Grammar test
19+
cd $kclvm_source_dir/test/integration
20+
python3 -m pip install --upgrade pip
21+
python3 -m pip install -U kclvm
22+
python3 -m pip install pytest pytest-xdist
23+
python3 -m pytest -v -n 10

kclvm/makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ test-runtime:
7070
test-grammar:
7171
cd tests/integration/grammar && kclvm -m pytest -v -n 5
7272

73+
# E2E konfig tests.
74+
test-konfig:
75+
cd tests/integration/konfig && kclvm -m pytest -v -n 5
76+
7377
# Parser fuzz.
7478
fuzz-parser:
7579
cd tests && cargo fuzz run fuzz_parser
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""
2+
this testing framework is developed based on pytest.
3+
see quick start of pytest: https://docs.pytest.org/en/latest/example/simple.html
4+
5+
"""
6+
import os
7+
import subprocess
8+
from pathlib import Path
9+
10+
import pytest
11+
from ruamel.yaml import YAML
12+
from collections.abc import Mapping, Sequence
13+
14+
TEST_FILE = "kcl.yaml"
15+
CI_TEST_DIR = "ci-test"
16+
STDOUT_GOLDEN = "stdout.golden.yaml"
17+
SETTINGS_FILE = "settings.yaml"
18+
19+
ROOT_STR = "test/integration/konfig"
20+
ROOT = str(Path(__file__).parent.parent.parent.parent.parent.joinpath(ROOT_STR))
21+
22+
yaml = YAML(typ="unsafe", pure=True)
23+
24+
25+
def find_test_dirs():
26+
result = []
27+
root_dirs = [ROOT]
28+
for root_dir in root_dirs:
29+
for root, _, files in os.walk(root_dir):
30+
for name in files:
31+
if name == TEST_FILE:
32+
result.append(root)
33+
return result
34+
35+
36+
def compare_results(result, golden_result):
37+
# Convert result and golden_result string to string lines with line ending stripped, then compare.
38+
39+
assert compare_unordered_yaml_objects(list(yaml.load_all(result)), list(yaml.load_all(golden_result)))
40+
41+
# Comparing the contents of two YAML objects for equality in an unordered manner
42+
def compare_unordered_yaml_objects(result, golden_result):
43+
if isinstance(result, Mapping) and isinstance(golden_result, Mapping):
44+
if result.keys() != golden_result.keys():
45+
return False
46+
for key in result.keys():
47+
if not compare_unordered_yaml_objects(result[key], golden_result[key]):
48+
return False
49+
50+
return True
51+
elif isinstance(result, Sequence) and isinstance(golden_result, Sequence):
52+
if len(result) != len(golden_result):
53+
return False
54+
for item in result:
55+
if item not in golden_result:
56+
return False
57+
for item in golden_result:
58+
if item not in result:
59+
return False
60+
return True
61+
else:
62+
return result == golden_result
63+
64+
def has_settings_file(directory):
65+
settings_file = directory / SETTINGS_FILE
66+
return settings_file.is_file()
67+
68+
69+
print("##### K Language Grammar Test Suite #####")
70+
test_dirs = find_test_dirs()
71+
pwd = str(Path(__file__).parent.parent.parent.parent)
72+
os.environ["PYTHONPATH"] = pwd
73+
74+
75+
@pytest.mark.parametrize("test_dir", test_dirs)
76+
def test_konfigs(test_dir):
77+
print(f"Testing {test_dir}")
78+
test_dir = Path(test_dir)
79+
kcl_file_name = test_dir / TEST_FILE
80+
ci_test_dir = test_dir / CI_TEST_DIR
81+
if not ci_test_dir.is_dir():
82+
# Skip invalid test cases
83+
return
84+
golden_file = ci_test_dir / STDOUT_GOLDEN
85+
if not golden_file.is_file():
86+
# Skip invalid test cases
87+
return
88+
kcl_command = ["kcl"]
89+
if has_settings_file(ci_test_dir):
90+
kcl_command.append("-Y")
91+
kcl_command.append(f"{CI_TEST_DIR}/{SETTINGS_FILE}")
92+
kcl_command.append(f"kcl.yaml")
93+
else:
94+
kcl_command.append(f"{TEST_FILE}")
95+
kcl_command.append("--target")
96+
kcl_command.append("native")
97+
process = subprocess.run(
98+
kcl_command, capture_output=True, cwd=test_dir, env=dict(os.environ)
99+
)
100+
stdout, stderr = process.stdout, process.stderr
101+
print(f"STDOUT:\n{stdout.decode()}")
102+
assert (
103+
process.returncode == 0 and len(stderr) == 0
104+
), f"Error executing file {kcl_file_name}.\nexit code = {process.returncode}\nstderr = {stderr}"
105+
if process.returncode == 0 and len(stderr) == 0:
106+
try:
107+
with open(golden_file, "r") as golden:
108+
compare_results(stdout.decode(), golden)
109+
except FileNotFoundError:
110+
raise Exception(f"Error reading expected result from file {golden_file}")

0 commit comments

Comments
 (0)