11from __future__ import annotations
22
3- import csv
4- import json
53import logging
64import sys
7- from pathlib import Path
85from typing import TYPE_CHECKING
96
107import pytest
11- from pyk .kdist import kdist
12- from pyk .kore .prelude import int_dv
13- from pyk .kore .syntax import App
14- from pyk .kore .tools import PrintOutput , kore_print
158
16- from kevm_pyk .interpreter import interpret
17-
18- from ..utils import REPO_ROOT
9+ from ..utils import REPO_ROOT , _skipped_tests , _test
1910
2011if TYPE_CHECKING :
12+ from pathlib import Path
2113 from typing import Final
2214
23- from pyk .kore .syntax import Pattern
24-
2515
2616_LOGGER : Final = logging .getLogger (__name__ )
2717
3424SLOW_TESTS_FILE : Final = REPO_ROOT / 'tests/slow.llvm'
3525
3626
37- def _test (gst_file : Path , * , schedule : str , mode : str , usegas : bool , save_failing : bool ) -> None :
38- skipped_gst_tests = SKIPPED_TESTS .get (gst_file , [])
39- if '*' in skipped_gst_tests :
40- pytest .skip ()
41-
42- failing_tests : list [str ] = []
43- gst_file_relative_path : Final [str ] = str (gst_file .relative_to (TEST_DIR ))
44- chainid = 0 if gst_file_relative_path in TEST_FILES_WITH_CID_0 else 1
45-
46- with gst_file .open () as f :
47- gst_data = json .load (f )
48-
49- for test_name , test in gst_data .items ():
50- _LOGGER .info (f'Running test: { gst_file } - { test_name } ' )
51- if test_name in skipped_gst_tests :
52- continue
53- res = interpret ({test_name : test }, schedule , mode , chainid , usegas , check = False )
54-
55- try :
56- _assert_exit_code_zero (res )
57- except AssertionError :
58- if not save_failing :
59- raise
60- failing_tests .append (test_name )
61-
62- if not failing_tests :
63- return
64- if save_failing :
65- with FAILING_TESTS_FILE .open ('a' , newline = '' ) as ff :
66- writer = csv .writer (ff )
67- if len (failing_tests ) == len (gst_data ):
68- writer .writerow ([gst_file_relative_path , '*' ])
69- else :
70- for test_name in sorted (failing_tests ):
71- writer .writerow ([gst_file_relative_path , test_name ])
72- raise AssertionError (f'Found failing tests in GST file { gst_file_relative_path } : { failing_tests } ' )
73-
74-
75- def _assert_exit_code_zero (pattern : Pattern ) -> None :
76- assert type (pattern ) is App
77- kevm_cell = pattern .args [0 ]
78- assert type (kevm_cell ) is App
79- exit_code_cell = kevm_cell .args [1 ]
80- assert type (exit_code_cell ) is App
81-
82- exit_code = exit_code_cell .args [0 ]
83- if exit_code == int_dv (0 ):
84- return
85-
86- pretty = kore_print (pattern , definition_dir = kdist .get ('evm-semantics.llvm' ), output = PrintOutput .PRETTY )
87- assert pretty == GOLDEN
88-
89-
90- def _skipped_tests () -> dict [Path , list [str ]]:
91- slow_tests = read_csv_file (SLOW_TESTS_FILE )
92- failing_tests = read_csv_file (FAILING_TESTS_FILE )
93- skipped : dict [Path , list [str ]] = {}
94- for test_file , test in slow_tests + failing_tests :
95- test_file = TEST_DIR / test_file
96- skipped .setdefault (test_file , []).append (test )
97- return skipped
98-
27+ SKIPPED_TESTS : Final = _skipped_tests (TEST_DIR , SLOW_TESTS_FILE , FAILING_TESTS_FILE )
9928
100- def read_csv_file (csv_file : Path ) -> tuple [tuple [Path , str ], ...]:
101- with csv_file .open (newline = '' ) as file :
102- reader = csv .reader (file )
103- return tuple ((Path (row [0 ]), row [1 ]) for row in reader )
10429
30+ def compute_chain_id (gst_file : str ) -> int :
31+ return 0 if gst_file in TEST_FILES_WITH_CID_0 else 1
10532
106- SKIPPED_TESTS : Final = _skipped_tests ()
10733
10834VM_TEST_DIR : Final = TEST_DIR / 'BlockchainTests/GeneralStateTests/VMTests'
10935VM_TESTS : Final = tuple (VM_TEST_DIR .glob ('*/*.json' ))
@@ -116,7 +42,17 @@ def read_csv_file(csv_file: Path) -> tuple[tuple[Path, str], ...]:
11642 ids = [str (test_file .relative_to (VM_TEST_DIR )) for test_file in VM_TESTS ],
11743)
11844def test_vm (test_file : Path , save_failing : bool ) -> None :
119- _test (test_file , schedule = 'DEFAULT' , mode = 'VMTESTS' , usegas = True , save_failing = save_failing )
45+ _test (
46+ test_file ,
47+ schedule = 'DEFAULT' ,
48+ mode = 'VMTESTS' ,
49+ usegas = True ,
50+ save_failing = save_failing ,
51+ compute_chain_id = compute_chain_id ,
52+ skipped_tests = SKIPPED_TESTS ,
53+ test_dir = TEST_DIR ,
54+ failing_tests_file = FAILING_TESTS_FILE ,
55+ )
12056
12157
12258@pytest .mark .skip (reason = 'failing / slow VM tests' )
@@ -126,7 +62,17 @@ def test_vm(test_file: Path, save_failing: bool) -> None:
12662 ids = [str (test_file .relative_to (VM_TEST_DIR )) for test_file in SKIPPED_VM_TESTS ],
12763)
12864def test_rest_vm (test_file : Path , save_failing : bool ) -> None :
129- _test (test_file , schedule = 'DEFAULT' , mode = 'VMTESTS' , usegas = True , save_failing = save_failing )
65+ _test (
66+ test_file ,
67+ schedule = 'DEFAULT' ,
68+ mode = 'VMTESTS' ,
69+ usegas = True ,
70+ save_failing = save_failing ,
71+ compute_chain_id = compute_chain_id ,
72+ skipped_tests = SKIPPED_TESTS ,
73+ test_dir = TEST_DIR ,
74+ failing_tests_file = FAILING_TESTS_FILE ,
75+ )
13076
13177
13278ALL_TEST_DIR : Final = TEST_DIR / 'BlockchainTests/GeneralStateTests'
@@ -141,7 +87,17 @@ def test_rest_vm(test_file: Path, save_failing: bool) -> None:
14187 ids = [str (test_file .relative_to (ALL_TEST_DIR )) for test_file in BCHAIN_TESTS ],
14288)
14389def test_bchain (test_file : Path , save_failing : bool ) -> None :
144- _test (test_file , schedule = 'CANCUN' , mode = 'NORMAL' , usegas = True , save_failing = save_failing )
90+ _test (
91+ test_file ,
92+ schedule = 'CANCUN' ,
93+ mode = 'NORMAL' ,
94+ usegas = True ,
95+ save_failing = save_failing ,
96+ compute_chain_id = compute_chain_id ,
97+ skipped_tests = SKIPPED_TESTS ,
98+ test_dir = TEST_DIR ,
99+ failing_tests_file = FAILING_TESTS_FILE ,
100+ )
145101
146102
147103@pytest .mark .skip (reason = 'failing / slow blockchain tests' )
@@ -151,4 +107,14 @@ def test_bchain(test_file: Path, save_failing: bool) -> None:
151107 ids = [str (test_file .relative_to (ALL_TEST_DIR )) for test_file in SKIPPED_BCHAIN_TESTS ],
152108)
153109def test_rest_bchain (test_file : Path , save_failing : bool ) -> None :
154- _test (test_file , schedule = 'CANCUN' , mode = 'NORMAL' , usegas = True , save_failing = save_failing )
110+ _test (
111+ test_file ,
112+ schedule = 'CANCUN' ,
113+ mode = 'NORMAL' ,
114+ usegas = True ,
115+ save_failing = save_failing ,
116+ compute_chain_id = compute_chain_id ,
117+ skipped_tests = SKIPPED_TESTS ,
118+ test_dir = TEST_DIR ,
119+ failing_tests_file = FAILING_TESTS_FILE ,
120+ )
0 commit comments