Coverage for tests / unit_tests / scons / test_gtkwave_util.py: 100%

55 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-08 02:47 +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 text = sb.read_file(gtkw_path) 

63 lines = text.split("\n") 

64 print(f"Actual {gtkw_path} lines:") 

65 for line in lines: 

66 print(f' "{line}",') 

67 

68 assert lines == EXPECTED_GTKW_LINES 

69 

70 

71def test_default_signals_creation(apio_runner: ApioRunner): 

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

73 

74 with apio_runner.in_sandbox() as sb: 

75 

76 # -- Execute "apio examples fetch alhambra-ii/getting-started" 

77 result = sb.invoke_apio_cmd( 

78 apio, ["examples", "fetch", "alhambra-ii/getting-started"] 

79 ) 

80 sb.assert_result_ok(result) 

81 

82 # -- Delete the file main_tb.gtkw 

83 gtkw_path = Path("main_tb.gtkw") 

84 assert gtkw_path.exists() 

85 gtkw_path.unlink() 

86 assert not gtkw_path.exists() 

87 

88 # -- Execute "apio sim --no-gtkwave main_tb.v" 

89 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave", "main_tb.v"]) 

90 sb.assert_result_ok(result) 

91 assert gtkw_path.exists() 

92 

93 # -- Test the generated .gtkw file. 

94 text = sb.read_file(gtkw_path) 

95 lines = text.split("\n") 

96 print(f"Actual {gtkw_path} lines:") 

97 for line in lines: 

98 print(f' "{line}",') 

99 

100 assert lines == EXPECTED_GTKW_LINES 

101 

102 

103def test_user_gtkw_file_protection(apio_runner: ApioRunner): 

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

105 

106 with apio_runner.in_sandbox() as sb: 

107 

108 # -- Execute "apio examples fetch alhambra-ii/getting-started" 

109 result = sb.invoke_apio_cmd( 

110 apio, ["examples", "fetch", "alhambra-ii/getting-started"] 

111 ) 

112 sb.assert_result_ok(result) 

113 

114 # -- Read the user's .gtkw file 

115 gtkw_path = Path("main_tb.gtkw") 

116 assert gtkw_path.exists() 

117 text_before = sb.read_file(gtkw_path) 

118 

119 # gtkw_path.unlink() 

120 # assert not gtkw_path.exists() 

121 

122 # -- Execute "apio sim --no-gtkwave main_tb.v" 

123 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave", "main_tb.v"]) 

124 sb.assert_result_ok(result) 

125 assert gtkw_path.exists() 

126 

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

128 assert gtkw_path.exists() 

129 text_after = sb.read_file(gtkw_path) 

130 

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

132 assert text_after == text_before