Skip to content

Commit

Permalink
test: use stderr.golden instead of stderr.golden.py
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed May 21, 2024
1 parent 9e1ff13 commit 5a66d3b
Show file tree
Hide file tree
Showing 755 changed files with 3,334 additions and 7,927 deletions.
9 changes: 1 addition & 8 deletions kclvm/evaluator/src/calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,7 @@ impl<'ctx> Evaluator<'ctx> {
/// Insert an entry including key and value into the dict, and merge the original entry.
#[inline]
pub(crate) fn dict_insert_merge_value(&self, dict: &mut ValueRef, key: &str, value: &ValueRef) {
self.dict_merge_key_value_pair(
dict,
key,
value,
ConfigEntryOperationKind::Union,
-1,
false,
);
self.dict_merge_key_value_pair(dict, key, value, ConfigEntryOperationKind::Union, -1, true);
}

/// Set dict key with the value. When the dict is a schema and resolve schema validations.
Expand Down
9 changes: 6 additions & 3 deletions kclvm/parser/src/file_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ impl FileGraph {
.rev()
.map(|n| self.graph[n].clone())
.collect::<Vec<_>>()),
Err(_) => {
Err(err) => {
// toposort function in the `petgraph` library doesn't return the cycle itself,
// so we need to use Tarjan's algorithm to find one instead
let strongly_connected_components = petgraph::algo::tarjan_scc(&self.graph);

// a strongly connected component is a cycle if it has more than one node
// let's just return the first one we find
let cycle = strongly_connected_components
let cycle = match strongly_connected_components
.into_iter()
.find(|component| component.len() > 1)
.unwrap();
{
Some(vars) => vars,
None => vec![err.node_id()],
};
Err(cycle
.iter()
.map(|n| self.graph[*n].clone())
Expand Down
74 changes: 44 additions & 30 deletions kclvm/tests/integration/grammar/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
TEST_FILE = "main.k"
STDOUT_GOLDEN = "stdout.golden"
STDERR_GOLDEN = "stderr.golden"
STDOUT_GOLDEN_PY = "stdout.golden.py"
STDERR_GOLDEN_PY = "stderr.golden.py"
SETTINGS_FILE = "settings.yaml"
TEST_PATH = "test/grammar"

Expand Down Expand Up @@ -106,6 +104,16 @@ def read_settings_file(settings_file_name):
test_dirs = find_test_dirs(str(test_path), "")


def remove_ansi_escape_sequences(text):
ansi_escape_pattern = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
return ansi_escape_pattern.sub('', text)


def remove_extra_empty_lines(text):
lines = [line for line in text.splitlines() if line.strip()]
return '\n'.join(lines)


@pytest.mark.parametrize("test_dir", test_dirs)
def test_grammar(test_dir):
print("Testing {}".format(test_dir))
Expand All @@ -123,31 +131,37 @@ def test_grammar(test_dir):
stdout, stderr = process.communicate()
print("STDOUT:\n{}".format(stdout.decode()))
print("STDERR:\n{}".format(stderr.decode()))
RETURN_CODE = 0
KCLVM_OUTPUT = 1
GOLDEN_FILE = 2
GOLDEN_FILE_SCRIPT = 3
settings = {
"stdout": (None, stdout, STDOUT_GOLDEN, STDOUT_GOLDEN_PY),
}
for _, setting in settings.items():
# Attempt to generate a golden stdout.
golden_file_result = generate_golden_file(
os.path.join(test_dir, setting[GOLDEN_FILE_SCRIPT])
)
if golden_file_result:
compare_results(setting[KCLVM_OUTPUT], golden_file_result)
else:
# Attempt to use existing golden stdout.
try:
with open(
os.path.join(test_dir, setting[GOLDEN_FILE]), "r"
) as golden_file:
compare_results_with_lines(setting[KCLVM_OUTPUT], golden_file)
if setting[RETURN_CODE] is not None:
assert process.returncode == setting[RETURN_CODE]
except OSError:
# Ignore when a golden file does not exist.
pass
except Exception:
raise
# Attempt to use existing golden stdout.
try:
with open(
os.path.join(test_dir, STDOUT_GOLDEN), "r"
) as golden_file:
compare_results_with_lines(stdout, golden_file)
assert process.returncode == 0
except OSError:
# Ignore when a golden file does not exist.
pass
except Exception:
raise

# Attempt to compare existing golden stdout.
try:
with open(
os.path.join(test_dir, STDOUT_GOLDEN), "r"
) as golden_file:
compare_results_with_lines(stdout, golden_file)
assert process.returncode == 0
except OSError:
# Ignore when a golden file does not exist.
pass
except Exception:
raise

stderr_file = pathlib.Path(test_dir).joinpath(STDERR_GOLDEN)
cwd = os.path.abspath(test_dir)
if stderr_file.exists():
golden = remove_extra_empty_lines(remove_ansi_escape_sequences(stderr_file.read_text()))
stderr = remove_extra_empty_lines(remove_ansi_escape_sequences(stderr.decode()))
golden = golden.replace("${CWD}", cwd)
assert golden in stderr
assert process.returncode > 0
6 changes: 6 additions & 0 deletions test/grammar/assert/invalid/fail_0/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E3M38]: EvaluationError
--> ${CWD}/main.k:1:1
|
1 | assert False
|
|
15 changes: 0 additions & 15 deletions test/grammar/assert/invalid/fail_0/stderr.golden.py

This file was deleted.

6 changes: 6 additions & 0 deletions test/grammar/assert/invalid/fail_1/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E3M38]: EvaluationError
--> ${CWD}/main.k:1:1
|
1 | assert 1 == 2, '1 is not equal to 2'
| 1 is not equal to 2
|
22 changes: 0 additions & 22 deletions test/grammar/assert/invalid/fail_1/stderr.golden.py

This file was deleted.

6 changes: 6 additions & 0 deletions test/grammar/assert/invalid/fail_2/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E3M38]: EvaluationError
--> ${CWD}/main.k:2:1
|
2 | assert _x == "bad case", "x should be 'good case'"
| x should be 'good case'
|
22 changes: 0 additions & 22 deletions test/grammar/assert/invalid/fail_2/stderr.golden.py

This file was deleted.

6 changes: 6 additions & 0 deletions test/grammar/assert/invalid/fail_3/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E3M38]: EvaluationError
--> ${CWD}/main.k:3:1
|
3 | assert _x == "bad case" if _x, "x should be 'good case'"
| x should be 'good case'
|
22 changes: 0 additions & 22 deletions test/grammar/assert/invalid/fail_3/stderr.golden.py

This file was deleted.

2 changes: 1 addition & 1 deletion test/grammar/builtins/file/append/file_append.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sample content
sample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample contentsample content
7 changes: 6 additions & 1 deletion test/grammar/builtins/file/append/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
append() requires 'filepath' argument
error[E3M38]: EvaluationError
--> ${CWD}/main.k:5:1
|
5 | file.append("", "sample content without path")
| Failed to open or create file '': No such file or directory (os error 2)
|
7 changes: 6 additions & 1 deletion test/grammar/builtins/file/cp/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
cp: failed to copy 'source.txt' to 'destination.txt': No such file or directory
error[E3M38]: EvaluationError
--> ${CWD}/main.k:3:1
|
3 | file.cp("source.txt", "destination.txt")
| Failed to copy from 'source.txt' to 'destination.txt': No such file or directory (os error 2)
|
7 changes: 6 additions & 1 deletion test/grammar/builtins/file/delete/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
delete: failed to delete 'test_dir': No such file or directory
error[E3M38]: EvaluationError
--> ${CWD}/main.k:3:1
|
3 | file.delete("test_dir")
| failed to delete 'test_dir': No such file or directory (os error 2)
|
1 change: 0 additions & 1 deletion test/grammar/builtins/file/delete/stdout.golden

This file was deleted.

1 change: 1 addition & 0 deletions test/grammar/builtins/file/mkdir/main.k
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import file

file.mkdir("test_dir")
a = 1
2 changes: 0 additions & 2 deletions test/grammar/builtins/file/mkdir/stderr.golden

This file was deleted.

2 changes: 1 addition & 1 deletion test/grammar/builtins/file/mkdir/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@

a: 1
7 changes: 6 additions & 1 deletion test/grammar/builtins/file/mv/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
mv: failed to move 'source.txt' to 'destination.txt': No such file or directory
error[E3M38]: EvaluationError
--> ${CWD}/main.k:3:1
|
3 | file.mv("source.txt", "destination.txt")
| Failed to move 'source.txt' to 'destination.txt': No such file or directory (os error 2)
|
Empty file.
7 changes: 6 additions & 1 deletion test/grammar/builtins/file/size/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
size: failed to get size of 'source_file.txt': No such file or directory
error[E3M38]: EvaluationError
--> ${CWD}/main.k:3:1
|
3 | file.size("source_file.txt")
| failed to get size of 'source_file.txt': No such file or directory (os error 2)
|
3 changes: 2 additions & 1 deletion test/grammar/builtins/file/write/main.k
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import file

file.write("test_file.txt", "Hello, world!")
file.write("test_file.txt", "Hello, world!")
a = 1
1 change: 1 addition & 0 deletions test/grammar/builtins/file/write/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a: 1
6 changes: 6 additions & 0 deletions test/grammar/builtins/operator/operator_fail_0/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E2G22]: TypeError
--> ${CWD}/main.k:2:5
|
2 | b = 1 + None
| ^ unsupported operand type(s) for +: 'int(1)' and 'NoneType'
|
21 changes: 0 additions & 21 deletions test/grammar/builtins/operator/operator_fail_0/stderr.golden.py

This file was deleted.

6 changes: 6 additions & 0 deletions test/grammar/builtins/operator/operator_fail_1/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E2G22]: TypeError
--> ${CWD}/main.k:2:5
|
2 | b = None + 1
| ^ unsupported operand type(s) for +: 'NoneType' and 'int(1)'
|
22 changes: 0 additions & 22 deletions test/grammar/builtins/operator/operator_fail_1/stderr.golden.py

This file was deleted.

6 changes: 6 additions & 0 deletions test/grammar/builtins/operator/operator_fail_2/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E2G22]: TypeError
--> ${CWD}/main.k:2:5
|
2 | b = 1 + a
| ^ unsupported operand type(s) for +: 'int(1)' and '[int | {str:str}]'
|
Loading

0 comments on commit 5a66d3b

Please sign in to comment.