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

1""" 

2Tests of gtkwave_util.py 

3""" 

4 

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 

9 

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] 

28 

29 

30def test_create_gtkwave_file(apio_runner: ApioRunner): 

31 """Test the create_gtkwave_file() function""" 

32 

33 with apio_runner.in_sandbox() as sb: 

34 

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") 

38 

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) 

44 

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) 

48 

49 # -- Verify that the .vcd file exists. 

50 assert vcd_path.is_file() 

51 

52 # -- Delete the file main_tb.gtkw 

53 assert gtkw_path.is_file() 

54 gtkw_path.unlink() 

55 assert not gtkw_path.exists() 

56 

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() 

60 

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}",') 

66 

67 assert lines == EXPECTED_GTKW_LINES 

68 

69 

70def test_default_signals_creation(apio_runner: ApioRunner): 

71 """Test the automatic .gtkw file creation by 'apio sim'.""" 

72 

73 with apio_runner.in_sandbox() as sb: 

74 

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) 

80 

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() 

86 

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() 

91 

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}",') 

97 

98 assert lines == EXPECTED_GTKW_LINES 

99 

100 

101def test_user_gtkw_file_protection(apio_runner: ApioRunner): 

102 """Test that a user's .gtkw file is not overwritten by apio'.""" 

103 

104 with apio_runner.in_sandbox() as sb: 

105 

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) 

111 

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) 

116 

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() 

121 

122 # -- Read the .gtkw file after the operation. 

123 assert gtkw_path.exists() 

124 text_after = sb.read_file_text(gtkw_path) 

125 

126 # -- Verify that apio didn't change it. 

127 assert text_after == text_before