Coverage for tests/unit_tests/utils/test_util.py: 98%

62 statements  

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

1""" 

2Tests of util.py 

3""" 

4 

5import os 

6import sys 

7from pathlib import Path 

8import pytest 

9from pytest import raises 

10from tests.conftest import ApioRunner 

11from apio.utils.util import ( 

12 plurality, 

13 list_plurality, 

14 is_debug, 

15 pushd, 

16 subprocess_call, 

17) 

18 

19# TODO: Add more tests. 

20 

21 

22def test_pluraliry(): 

23 """Tests the plurality() function.""" 

24 # -- Test for ints 1, 2, 3 

25 assert plurality(1, "file") == "1 file" 

26 assert plurality(2, "file") == "2 files" 

27 assert plurality(3, "file") == "3 files" 

28 

29 # -- Test for lengths 1, 2, 3. 

30 assert plurality(["aa"], "file") == "1 file" 

31 assert plurality(["aa", "bb"], "file") == "2 files" 

32 assert plurality(["aa", "bb", "cc"], "file") == "3 files" 

33 

34 

35def test_list_pluraliry(): 

36 """Tests the list_plurality() function.""" 

37 

38 # -- Test for lengths 1, 2, and 3. 

39 assert list_plurality(["aa"], "or") == "aa" 

40 assert list_plurality(["aa", "bb"], "and") == "aa and bb" 

41 assert list_plurality(["aa", "bb", "cc"], "or") == "aa, bb, or cc" 

42 

43 # -- An empty list should trhow an assert exception. 

44 with pytest.raises(AssertionError): 

45 list_plurality([], "or") 

46 

47 

48def test_is_debug(): 

49 """Tests the is_debug() function.""" 

50 

51 # -- Assuming APIO_DEBUG is not defined. 

52 assert not is_debug(1) 

53 assert not is_debug(2) 

54 assert not is_debug(3) 

55 

56 # -- Enter debug mode level 1. 

57 os.environ["APIO_DEBUG"] = "1" 

58 assert is_debug(1) 

59 assert not is_debug(2) 

60 assert not is_debug(3) 

61 

62 # -- Enter debug mode level 2. 

63 os.environ["APIO_DEBUG"] = "2" 

64 assert is_debug(1) 

65 assert is_debug(2) 

66 assert not is_debug(3) 

67 

68 # -- Enter debug mode level 3. 

69 os.environ["APIO_DEBUG"] = "3" 

70 assert is_debug(1) 

71 assert is_debug(2) 

72 assert is_debug(3) 

73 

74 # -- Exit debug mode 

75 os.environ.pop("APIO_DEBUG") 

76 assert not is_debug(1) 

77 assert not is_debug(2) 

78 assert not is_debug(3) 

79 

80 

81def test_pushd(apio_runner: ApioRunner): 

82 """Test the pushd context manager.""" 

83 

84 with apio_runner.in_sandbox() as sb: 

85 # -- Define dir 1. 

86 dir1 = sb.proj_dir 

87 assert dir1.is_dir() 

88 

89 # -- Define dir 2 

90 dir2 = dir1 / "dir2" 

91 assert dir2.resolve() != dir1.resolve() 

92 dir2.mkdir() 

93 

94 # -- Change to dir1 

95 os.chdir(dir1) 

96 assert Path.cwd().resolve() == dir1.resolve() 

97 

98 # -- Pushd to dir 2 

99 with pushd(dir2): 

100 assert Path.cwd().resolve() == dir2.resolve() 

101 

102 # -- Back from pushd to dir1 

103 assert Path.cwd().resolve() == dir1.resolve() 

104 

105 

106def test_subprocess_call(apio_runner: ApioRunner): 

107 """Test subprocess_call().""" 

108 

109 with apio_runner.in_sandbox(): 

110 

111 # -- Test a successful subprocess 

112 file1 = Path("file1") 

113 assert not file1.exists() 

114 subprocess_call( 

115 [ 

116 sys.executable, 

117 "-c", 

118 f'import pathlib; pathlib.Path("{str(file1)}").' 

119 'write_text("content1")', 

120 ] 

121 ) 

122 assert file1.is_file() 

123 assert file1.read_text(encoding="utf-8") == "content1" 

124 

125 # -- Test a failing subprocess 

126 with raises(SystemExit) as e: 

127 subprocess_call( 

128 [ 

129 sys.executable, 

130 "-c", 

131 "import sys; sys.exit(7)", 

132 ] 

133 ) 

134 # -- Apio exits with 1, regardless of the subprocess error status. 

135 assert e.value.code == 1