Coverage for tests/unit_tests/utils/test_util.py: 99%
78 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
1"""
2Tests of util.py
3"""
5import os
6import sys
7from pathlib import Path
8import pytest
9from pytest import raises
10from tests.conftest import ApioRunner
11from apio.common.debug_util import is_debug
12from apio.utils.util import (
13 get_apio_release_info,
14 plurality,
15 list_plurality,
16 pushd,
17 subprocess_call,
18 compute_file_sha256,
19)
21# TODO: Add more tests.
24def test_release_info():
25 """Tests that the release info placeholder is empty. This value
26 is set on the fly by build and publishing github workflows."""
27 version_info = get_apio_release_info()
28 assert isinstance(version_info, str)
29 assert version_info == ""
32def test_pluraliry():
33 """Tests the plurality() function."""
34 # -- Test for ints 1, 2, 3
35 assert plurality(1, "file") == "1 file"
36 assert plurality(2, "file") == "2 files"
37 assert plurality(3, "file") == "3 files"
39 # -- Test for lengths 1, 2, 3.
40 assert plurality(["aa"], "file") == "1 file"
41 assert plurality(["aa", "bb"], "file") == "2 files"
42 assert plurality(["aa", "bb", "cc"], "file") == "3 files"
45def test_list_pluraliry():
46 """Tests the list_plurality() function."""
48 # -- Test for lengths 1, 2, and 3.
49 assert list_plurality(["aa"], "or") == "aa"
50 assert list_plurality(["aa", "bb"], "and") == "aa and bb"
51 assert list_plurality(["aa", "bb", "cc"], "or") == "aa, bb, or cc"
53 # -- An empty list should trhow an assert exception.
54 with pytest.raises(AssertionError):
55 list_plurality([], "or")
58def test_is_debug():
59 """Tests the is_debug() function."""
61 # -- Assuming APIO_DEBUG is not defined.
62 assert not is_debug(1)
63 assert not is_debug(2)
64 assert not is_debug(3)
66 # -- Enter debug mode level 1.
67 os.environ["APIO_DEBUG"] = "1"
68 assert is_debug(1)
69 assert not is_debug(2)
70 assert not is_debug(3)
72 # -- Enter debug mode level 2.
73 os.environ["APIO_DEBUG"] = "2"
74 assert is_debug(1)
75 assert is_debug(2)
76 assert not is_debug(3)
78 # -- Enter debug mode level 3.
79 os.environ["APIO_DEBUG"] = "3"
80 assert is_debug(1)
81 assert is_debug(2)
82 assert is_debug(3)
84 # -- Exit debug mode
85 os.environ.pop("APIO_DEBUG")
86 assert not is_debug(1)
87 assert not is_debug(2)
88 assert not is_debug(3)
91def test_pushd(apio_runner: ApioRunner):
92 """Test the pushd context manager."""
94 with apio_runner.in_sandbox() as sb:
95 # -- Define dir 1.
96 dir1 = sb.proj_dir
97 assert dir1.is_dir()
99 # -- Define dir 2
100 dir2 = dir1 / "dir2"
101 assert dir2.resolve() != dir1.resolve()
102 dir2.mkdir()
104 # -- Change to dir1
105 os.chdir(dir1)
106 assert Path.cwd().resolve() == dir1.resolve()
108 # -- Pushd to dir 2
109 with pushd(dir2):
110 assert Path.cwd().resolve() == dir2.resolve()
112 # -- Back from pushd to dir1
113 assert Path.cwd().resolve() == dir1.resolve()
116def test_subprocess_call(apio_runner: ApioRunner):
117 """Test subprocess_call()."""
119 with apio_runner.in_sandbox():
121 # -- Test a successful subprocess
122 file1 = Path("file1")
123 assert not file1.exists()
124 subprocess_call(
125 [
126 sys.executable,
127 "-c",
128 f'import pathlib; pathlib.Path("{str(file1)}").'
129 'write_text("content1")',
130 ]
131 )
132 assert file1.is_file()
133 assert file1.read_text(encoding="utf-8") == "content1"
135 # -- Test a failing subprocess
136 with raises(SystemExit) as e:
137 subprocess_call(
138 [
139 sys.executable,
140 "-c",
141 "import sys; sys.exit(7)",
142 ]
143 )
144 # -- Apio exits with 1, regardless of the subprocess error status.
145 assert e.value.code == 1
148def test_compute_file_sha256(apio_runner: ApioRunner):
149 """Test compute_file_sha256()."""
151 with apio_runner.in_sandbox() as sb:
153 path = Path("./my-test-file")
155 # -- Test with an empty file.
156 path = Path("./my-test-file1")
157 sb.write_file(path, "")
158 sha256 = compute_file_sha256(path)
159 assert (
160 sha256 == "e3b0c44298fc1c149afbf4c8996fb92427"
161 "ae41e4649b934ca495991b7852b855"
162 )
164 # -- Test with non empty file.
165 path = Path("./my-test-file2")
166 sb.write_file(path, "hello world")
167 sha256 = compute_file_sha256(path)
168 assert (
169 sha256 == "b94d27b9934d3e08a52e52d7da7dabfac"
170 "484efe37a5380ee9088f7ace2efcde9"
171 )