Coverage for tests / unit_tests / utils / test_util.py: 98%
66 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +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.utils.util import (
12 get_apio_release_info,
13 plurality,
14 list_plurality,
15 is_debug,
16 pushd,
17 subprocess_call,
18)
20# TODO: Add more tests.
23def test_release_info():
24 """Tests that the release info placeholder is empty. This value
25 is set on the fly by build and publishing github workflows."""
26 version_info = get_apio_release_info()
27 assert isinstance(version_info, str)
28 assert version_info == ""
31def test_pluraliry():
32 """Tests the plurality() function."""
33 # -- Test for ints 1, 2, 3
34 assert plurality(1, "file") == "1 file"
35 assert plurality(2, "file") == "2 files"
36 assert plurality(3, "file") == "3 files"
38 # -- Test for lengths 1, 2, 3.
39 assert plurality(["aa"], "file") == "1 file"
40 assert plurality(["aa", "bb"], "file") == "2 files"
41 assert plurality(["aa", "bb", "cc"], "file") == "3 files"
44def test_list_pluraliry():
45 """Tests the list_plurality() function."""
47 # -- Test for lengths 1, 2, and 3.
48 assert list_plurality(["aa"], "or") == "aa"
49 assert list_plurality(["aa", "bb"], "and") == "aa and bb"
50 assert list_plurality(["aa", "bb", "cc"], "or") == "aa, bb, or cc"
52 # -- An empty list should trhow an assert exception.
53 with pytest.raises(AssertionError):
54 list_plurality([], "or")
57def test_is_debug():
58 """Tests the is_debug() function."""
60 # -- Assuming APIO_DEBUG is not defined.
61 assert not is_debug(1)
62 assert not is_debug(2)
63 assert not is_debug(3)
65 # -- Enter debug mode level 1.
66 os.environ["APIO_DEBUG"] = "1"
67 assert is_debug(1)
68 assert not is_debug(2)
69 assert not is_debug(3)
71 # -- Enter debug mode level 2.
72 os.environ["APIO_DEBUG"] = "2"
73 assert is_debug(1)
74 assert is_debug(2)
75 assert not is_debug(3)
77 # -- Enter debug mode level 3.
78 os.environ["APIO_DEBUG"] = "3"
79 assert is_debug(1)
80 assert is_debug(2)
81 assert is_debug(3)
83 # -- Exit debug mode
84 os.environ.pop("APIO_DEBUG")
85 assert not is_debug(1)
86 assert not is_debug(2)
87 assert not is_debug(3)
90def test_pushd(apio_runner: ApioRunner):
91 """Test the pushd context manager."""
93 with apio_runner.in_sandbox() as sb:
94 # -- Define dir 1.
95 dir1 = sb.proj_dir
96 assert dir1.is_dir()
98 # -- Define dir 2
99 dir2 = dir1 / "dir2"
100 assert dir2.resolve() != dir1.resolve()
101 dir2.mkdir()
103 # -- Change to dir1
104 os.chdir(dir1)
105 assert Path.cwd().resolve() == dir1.resolve()
107 # -- Pushd to dir 2
108 with pushd(dir2):
109 assert Path.cwd().resolve() == dir2.resolve()
111 # -- Back from pushd to dir1
112 assert Path.cwd().resolve() == dir1.resolve()
115def test_subprocess_call(apio_runner: ApioRunner):
116 """Test subprocess_call()."""
118 with apio_runner.in_sandbox():
120 # -- Test a successful subprocess
121 file1 = Path("file1")
122 assert not file1.exists()
123 subprocess_call(
124 [
125 sys.executable,
126 "-c",
127 f'import pathlib; pathlib.Path("{str(file1)}").'
128 'write_text("content1")',
129 ]
130 )
131 assert file1.is_file()
132 assert file1.read_text(encoding="utf-8") == "content1"
134 # -- Test a failing subprocess
135 with raises(SystemExit) as e:
136 subprocess_call(
137 [
138 sys.executable,
139 "-c",
140 "import sys; sys.exit(7)",
141 ]
142 )
143 # -- Apio exits with 1, regardless of the subprocess error status.
144 assert e.value.code == 1