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

51 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +0000

1"""Test for the "apio clean" command.""" 

2 

3import os 

4from pathlib import Path 

5from tests.conftest import ApioRunner 

6from apio.commands.apio import apio_top_cli as apio 

7 

8 

9def test_clean(apio_runner: ApioRunner): 

10 """Tests the 'apio clean' with two envs. It should delete _build.""" 

11 

12 with apio_runner.in_sandbox() as sb: 

13 

14 sb.write_apio_ini( 

15 { 

16 "[env:env1]": { 

17 "board": "alhambra-ii", 

18 "top-module": "main", 

19 }, 

20 "[env:env2]": { 

21 "board": "alhambra-ii", 

22 "top-module": "main", 

23 }, 

24 } 

25 ) 

26 

27 sb.write_file("_build/env1/hardware.out", "dummy text") 

28 sb.write_file("_build/env2/hardware.out", "dummy text") 

29 

30 assert Path("_build/env1/hardware.out").exists() 

31 assert Path("_build/env2/hardware.out").exists() 

32 

33 result = sb.invoke_apio_cmd(apio, ["clean"]) 

34 

35 sb.assert_result_ok(result) 

36 assert "Removed _build" in result.output 

37 assert not Path("_build").exists() 

38 

39 

40def test_clean_from_remote_dir(apio_runner: ApioRunner): 

41 """Tests the 'apio clean' with two envs. It should delete _build.""" 

42 

43 with apio_runner.in_sandbox() as sb: 

44 

45 # -- Cache directories values. 

46 proj_dir: Path = sb.proj_dir 

47 remote_dir: Path = sb.sandbox_dir 

48 

49 sb.write_apio_ini( 

50 { 

51 "[env:env1]": { 

52 "board": "alhambra-ii", 

53 "top-module": "main", 

54 }, 

55 "[env:env2]": { 

56 "board": "alhambra-ii", 

57 "top-module": "main", 

58 }, 

59 } 

60 ) 

61 

62 sb.write_file("_build/env1/hardware.out", "dummy text") 

63 sb.write_file("_build/env2/hardware.out", "dummy text") 

64 

65 assert Path("_build/env1/hardware.out").exists() 

66 assert Path("_build/env2/hardware.out").exists() 

67 

68 # -- Run the clean command from a remote dir. 

69 os.chdir(remote_dir) 

70 assert not Path("apio.ini").exists() 

71 result = sb.invoke_apio_cmd( 

72 apio, ["clean", "--project-dir", str(proj_dir)] 

73 ) 

74 sb.assert_result_ok(result) 

75 assert "Removed _build" in result.output 

76 os.chdir(proj_dir) 

77 assert not Path("_build").exists() 

78 

79 

80def test_clean_no_build(apio_runner: ApioRunner): 

81 """Tests the 'apio clean' command with nothing to delete.""" 

82 

83 with apio_runner.in_sandbox() as sb: 

84 

85 sb.write_default_apio_ini() 

86 

87 assert not Path("_build").exists() 

88 

89 result = sb.invoke_apio_cmd(apio, ["clean"]) 

90 sb.assert_result_ok(result) 

91 assert "Removed" not in result.output 

92 assert "Already clean" in result.output 

93 

94 

95def test_clean_legacy_files(apio_runner: ApioRunner): 

96 """Tests that 'apio clean' deletes also legacy files that may have been 

97 left in the project dir by apio version before 1.x.x. 

98 """ 

99 

100 with apio_runner.in_sandbox() as sb: 

101 

102 legacy_files = [ 

103 "hardware.asc", 

104 "hardware.bin", 

105 "hardware.dot", 

106 "hardware.json", 

107 "hardware.pnr", 

108 "hardware.svg", 

109 "main_tb.vcd", 

110 ] 

111 

112 sb.write_default_apio_ini() 

113 

114 for legacy_file in legacy_files: 

115 sb.write_file(legacy_file, "dummy text") 

116 assert Path(legacy_file).exists() 

117 

118 result = sb.invoke_apio_cmd(apio, ["clean"]) 

119 sb.assert_result_ok(result) 

120 

121 for legacy_file in legacy_files: 

122 assert f"Removed {legacy_file}" in result.output 

123 assert not Path(legacy_file).exists()