Coverage for tests/unit_tests/scons/test_gtkwave_util.py: 100%
53 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""
10Tests of gtkwave_util.py
11"""
13from pathlib import Path
14from tests.conftest import ApioRunner
15from apio.commands.apio import apio_top_cli as apio
16from apio.scons.gtkwave_util import create_gtkwave_file
18# Expected default gtkw file lines for the examples we use below.
19EXPECTED_GTKW_LINES = [
20 "[*] GTKWave display configuration for 'apio sim main_tb.v'",
21 "[*] THIS FILE WAS GENERATED AUTOMATICALLY BY APIO.",
22 "[*] DO NOT EDIT IT MANUALLY!",
23 "[*] To customize this file, run 'apio sim main_tb.v'",
24 "[*] and save the file from GTKWave.",
25 "",
26 "[*] GTKWave Analyzer v3.4.0 (w)1999-2022 BSI",
27 "",
28 "[*]",
29 "main_tb.assertion_err",
30 "main_tb.clk_num",
31 "main_tb.i",
32 "main_tb.leds[1:0]",
33 "main_tb.N",
34 "main_tb.sys_clk",
35 "",
36]
39def test_create_gtkwave_file(apio_runner: ApioRunner):
40 """Test the create_gtkwave_file() function"""
42 with apio_runner.in_sandbox() as sb:
44 # -- Create relative paths to the .gtkw and .vcd files
45 gtkw_path = Path("main_tb.gtkw")
46 vcd_path = Path("_build/blink-slow/main_tb.vcd")
48 # -- Execute "apio examples fetch alhambra-ii/getting-started"
49 result = sb.invoke_apio_cmd(
50 apio, ["examples", "fetch", "alhambra-ii/getting-started"]
51 )
52 sb.assert_result_ok(result)
54 # -- Execute "apio test main_tb.v" to create the .vcd file
55 result = sb.invoke_apio_cmd(apio, ["test", "main_tb.v"])
56 sb.assert_result_ok(result)
58 # -- Verify that the .vcd file exists.
59 assert vcd_path.is_file()
61 # -- Delete the file main_tb.gtkw
62 assert gtkw_path.is_file()
63 gtkw_path.unlink()
64 assert not gtkw_path.exists()
66 # -- Create the default .gtkw file
67 create_gtkwave_file("main_tb.v", str(vcd_path), str(gtkw_path))
68 assert gtkw_path.is_file()
70 # -- Test the generated .gtkw file.
71 lines = sb.read_file_lines(gtkw_path)
72 print(f"Actual {gtkw_path} lines:")
73 for line in lines:
74 print(f' "{line}",')
76 assert lines == EXPECTED_GTKW_LINES
79def test_default_signals_creation(apio_runner: ApioRunner):
80 """Test the automatic .gtkw file creation by 'apio sim'."""
82 with apio_runner.in_sandbox() as sb:
84 # -- Execute "apio examples fetch alhambra-ii/getting-started"
85 result = sb.invoke_apio_cmd(
86 apio, ["examples", "fetch", "alhambra-ii/getting-started"]
87 )
88 sb.assert_result_ok(result)
90 # -- Delete the file main_tb.gtkw
91 gtkw_path = Path("main_tb.gtkw")
92 assert gtkw_path.exists()
93 gtkw_path.unlink()
94 assert not gtkw_path.exists()
96 # -- Execute "apio sim --no-gtkwave main_tb.v"
97 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave", "main_tb.v"])
98 sb.assert_result_ok(result)
99 assert gtkw_path.exists()
101 # -- Test the generated .gtkw file.
102 lines = sb.read_file_lines(gtkw_path)
103 print(f"Actual {gtkw_path} lines:")
104 for line in lines:
105 print(f' "{line}",')
107 assert lines == EXPECTED_GTKW_LINES
110def test_user_gtkw_file_protection(apio_runner: ApioRunner):
111 """Test that a user's .gtkw file is not overwritten by apio'."""
113 with apio_runner.in_sandbox() as sb:
115 # -- Execute "apio examples fetch alhambra-ii/getting-started"
116 result = sb.invoke_apio_cmd(
117 apio, ["examples", "fetch", "alhambra-ii/getting-started"]
118 )
119 sb.assert_result_ok(result)
121 # -- Read the user's .gtkw file
122 gtkw_path = Path("main_tb.gtkw")
123 assert gtkw_path.exists()
124 text_before = sb.read_file_text(gtkw_path)
126 # -- Execute "apio sim --no-gtkwave main_tb.v"
127 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave", "main_tb.v"])
128 sb.assert_result_ok(result)
129 assert gtkw_path.exists()
131 # -- Read the .gtkw file after the operation.
132 assert gtkw_path.exists()
133 text_after = sb.read_file_text(gtkw_path)
135 # -- Verify that apio didn't change it.
136 assert text_after == text_before