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

39 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 clean" command.""" 

10 

11import os 

12from pathlib import Path 

13from tests.conftest import ApioRunner 

14from apio.commands.apio import apio_top_cli as apio 

15 

16 

17def test_clean(apio_runner: ApioRunner): 

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

19 

20 with apio_runner.in_sandbox() as sb: 

21 

22 sb.write_apio_ini( 

23 { 

24 "[env:env1]": { 

25 "board": "alhambra-ii", 

26 "top-module": "main", 

27 }, 

28 "[env:env2]": { 

29 "board": "alhambra-ii", 

30 "top-module": "main", 

31 }, 

32 } 

33 ) 

34 

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

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

37 

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

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

40 

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

42 

43 sb.assert_result_ok(result) 

44 assert "Removed _build" in result.output 

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

46 

47 

48def test_clean_from_remote_dir(apio_runner: ApioRunner): 

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

50 

51 with apio_runner.in_sandbox() as sb: 

52 

53 # -- Cache directories values. 

54 proj_dir: Path = sb.proj_dir 

55 remote_dir: Path = sb.sandbox_dir 

56 

57 sb.write_apio_ini( 

58 { 

59 "[env:env1]": { 

60 "board": "alhambra-ii", 

61 "top-module": "main", 

62 }, 

63 "[env:env2]": { 

64 "board": "alhambra-ii", 

65 "top-module": "main", 

66 }, 

67 } 

68 ) 

69 

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

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

72 

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

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

75 

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

77 os.chdir(remote_dir) 

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

79 result = sb.invoke_apio_cmd( 

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

81 ) 

82 sb.assert_result_ok(result) 

83 assert "Removed _build" in result.output 

84 os.chdir(proj_dir) 

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

86 

87 

88def test_clean_no_build(apio_runner: ApioRunner): 

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

90 

91 with apio_runner.in_sandbox() as sb: 

92 

93 sb.write_default_apio_ini() 

94 

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

96 

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

98 sb.assert_result_ok(result) 

99 assert "Removed" not in result.output 

100 assert "Already clean" in result.output