Coverage for tests/unit_tests/scons/test_gtkwave_util.py: 100%
53 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
1"""
2Tests of gtkwave_util.py
3"""
5from pathlib import Path
6from tests.conftest import ApioRunner
7from apio.commands.apio import apio_top_cli as apio
8from apio.scons.gtkwave_util import create_gtkwave_file
10# Expected default gtkw file lines for the examples we use below.
11EXPECTED_GTKW_LINES = [
12 "[*] GTKWave display configuration for 'apio sim main_tb.v'",
13 "[*] THIS FILE WAS GENERATED AUTOMATICALLY BY APIO.",
14 "[*] DO NOT EDIT IT MANUALLY!",
15 "[*] To customize this file, run 'apio sim main_tb.v'",
16 "[*] and save the file from GTKWave.",
17 "",
18 "[*] GTKWave Analyzer v3.4.0 (w)1999-2022 BSI",
19 "",
20 "[*]",
21 "testbench.CLK",
22 "testbench.expected_led",
23 "testbench.i[31:0]",
24 "testbench.LED1",
25 "testbench.LED2",
26 "",
27]
30def test_create_gtkwave_file(apio_runner: ApioRunner):
31 """Test the create_gtkwave_file() function"""
33 with apio_runner.in_sandbox() as sb:
35 # -- Create relative paths to the .gtkw and .vcd files
36 gtkw_path = Path("main_tb.gtkw")
37 vcd_path = Path("_build/blink-slow/main_tb.vcd")
39 # -- Execute "apio examples fetch alhambra-ii/getting-started"
40 result = sb.invoke_apio_cmd(
41 apio, ["examples", "fetch", "alhambra-ii/getting-started"]
42 )
43 sb.assert_result_ok(result)
45 # -- Execute "apio test main_tb.v" to create the .vcd file
46 result = sb.invoke_apio_cmd(apio, ["test", "main_tb.v"])
47 sb.assert_result_ok(result)
49 # -- Verify that the .vcd file exists.
50 assert vcd_path.is_file()
52 # -- Delete the file main_tb.gtkw
53 assert gtkw_path.is_file()
54 gtkw_path.unlink()
55 assert not gtkw_path.exists()
57 # -- Create the default .gtkw file
58 create_gtkwave_file("main_tb.v", str(vcd_path), str(gtkw_path))
59 assert gtkw_path.is_file()
61 # -- Test the generated .gtkw file.
62 lines = sb.read_file_lines(gtkw_path)
63 print(f"Actual {gtkw_path} lines:")
64 for line in lines:
65 print(f' "{line}",')
67 assert lines == EXPECTED_GTKW_LINES
70def test_default_signals_creation(apio_runner: ApioRunner):
71 """Test the automatic .gtkw file creation by 'apio sim'."""
73 with apio_runner.in_sandbox() as sb:
75 # -- Execute "apio examples fetch alhambra-ii/getting-started"
76 result = sb.invoke_apio_cmd(
77 apio, ["examples", "fetch", "alhambra-ii/getting-started"]
78 )
79 sb.assert_result_ok(result)
81 # -- Delete the file main_tb.gtkw
82 gtkw_path = Path("main_tb.gtkw")
83 assert gtkw_path.exists()
84 gtkw_path.unlink()
85 assert not gtkw_path.exists()
87 # -- Execute "apio sim --no-gtkwave main_tb.v"
88 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave", "main_tb.v"])
89 sb.assert_result_ok(result)
90 assert gtkw_path.exists()
92 # -- Test the generated .gtkw file.
93 lines = sb.read_file_lines(gtkw_path)
94 print(f"Actual {gtkw_path} lines:")
95 for line in lines:
96 print(f' "{line}",')
98 assert lines == EXPECTED_GTKW_LINES
101def test_user_gtkw_file_protection(apio_runner: ApioRunner):
102 """Test that a user's .gtkw file is not overwritten by apio'."""
104 with apio_runner.in_sandbox() as sb:
106 # -- Execute "apio examples fetch alhambra-ii/getting-started"
107 result = sb.invoke_apio_cmd(
108 apio, ["examples", "fetch", "alhambra-ii/getting-started"]
109 )
110 sb.assert_result_ok(result)
112 # -- Read the user's .gtkw file
113 gtkw_path = Path("main_tb.gtkw")
114 assert gtkw_path.exists()
115 text_before = sb.read_file_text(gtkw_path)
117 # -- Execute "apio sim --no-gtkwave main_tb.v"
118 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave", "main_tb.v"])
119 sb.assert_result_ok(result)
120 assert gtkw_path.exists()
122 # -- Read the .gtkw file after the operation.
123 assert gtkw_path.exists()
124 text_after = sb.read_file_text(gtkw_path)
126 # -- Verify that apio didn't change it.
127 assert text_after == text_before