Coverage for tests/unit_tests/commands/test_apio_sim.py: 100%

53 statements  

« 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 

8 

9"""Test for the "apio sim" command""" 

10 

11from pathlib import Path 

12from tests.conftest import ApioRunner 

13from apio.commands.apio import apio_top_cli as apio 

14 

15 

16def test_sim(apio_runner: ApioRunner): 

17 """Test apio sim without apio.ini. Should fail.""" 

18 

19 with apio_runner.in_sandbox() as sb: 

20 

21 # -- apio sim 

22 result = sb.invoke_apio_cmd(apio, ["sim"]) 

23 assert result.exit_code != 0, result.output 

24 assert "Missing project file apio.ini" in result.output 

25 

26 

27def test_sim_with_env_arg_error(apio_runner: ApioRunner): 

28 """Tests the command with an invalid --env value. This error message 

29 confirms that the --env arg was propagated to the apio.ini loading 

30 logic.""" 

31 

32 with apio_runner.in_sandbox() as sb: 

33 

34 # -- Run "apio sim --env no-such-env" 

35 sb.write_apio_ini({"[env:default]": {"top-module": "main"}}) 

36 result = sb.invoke_apio_cmd(apio, ["sim", "--env", "no-such-env"]) 

37 assert result.exit_code == 1, result.output 

38 assert ( 

39 "Error: Env 'no-such-env' not found in apio.ini" in result.output 

40 ) 

41 

42 

43def test_sim_with_no_user_gtkw_file(apio_runner: ApioRunner): 

44 """Verify that 'apio sim' creates a default .gtkw file.""" 

45 

46 with apio_runner.in_sandbox() as sb: 

47 

48 # -- Get example project. 

49 result = sb.invoke_apio_cmd( 

50 apio, ["examples", "fetch", "alhambra-ii/ledon"] 

51 ) 

52 sb.assert_result_ok(result) 

53 

54 # -- Verify it has a testbench gtkw file. 

55 gtkw_path = Path("main_tb.gtkw") 

56 assert gtkw_path.exists(), gtkw_path 

57 

58 # -- Delete teh gtkw file. 

59 gtkw_path.unlink() 

60 assert not gtkw_path.exists(), gtkw_path 

61 

62 # -- Run headless sim, it should create the gtkw file. 

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

64 sb.assert_result_ok(result) 

65 assert gtkw_path.exists(), gtkw_path 

66 

67 # -- Verify the file. 

68 content1 = gtkw_path.read_text(encoding="utf-8") 

69 timestamp1 = gtkw_path.stat().st_mtime 

70 assert "THIS FILE WAS GENERATED AUTOMATICALLY BY APIO" in content1 

71 assert "main_tb.sys_clk" in content1 

72 assert "leds[7:0]" in content1 

73 

74 # -- Run headless sim again, it should overwrite the gtkw file. 

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

76 sb.assert_result_ok(result) 

77 assert gtkw_path.exists(), gtkw_path 

78 

79 # -- Verify the file. 

80 content2 = gtkw_path.read_text(encoding="utf-8") 

81 timestamp2 = gtkw_path.stat().st_mtime 

82 assert content1 == content2 

83 assert timestamp1 != timestamp2 

84 

85 

86def test_sim_with_user_gtkw_file(apio_runner: ApioRunner): 

87 """Verify that 'apio sim' doesn't overwrite user saved .gtkw file.""" 

88 

89 with apio_runner.in_sandbox() as sb: 

90 

91 # -- Get example project. 

92 result = sb.invoke_apio_cmd( 

93 apio, ["examples", "fetch", "alhambra-ii/ledon"] 

94 ) 

95 sb.assert_result_ok(result) 

96 

97 # -- Verify it has a testbench gtkw file. 

98 gtkw_path = Path("main_tb.gtkw") 

99 assert gtkw_path.exists(), gtkw_path 

100 

101 # -- Verify the file. 

102 content1 = gtkw_path.read_text(encoding="utf-8") 

103 timestamp1 = gtkw_path.stat().st_mtime 

104 assert "THIS FILE WAS GENERATED AUTOMATICALLY BY APIO" not in content1 

105 

106 # -- Run headless sim, it should create the gtkw file. 

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

108 sb.assert_result_ok(result) 

109 assert gtkw_path.exists(), gtkw_path 

110 

111 # -- Verify that the file was not changed file. 

112 content2 = gtkw_path.read_text(encoding="utf-8") 

113 timestamp2 = gtkw_path.stat().st_mtime 

114 assert content1 == content2 

115 assert timestamp1 == timestamp2