66
77import pytest
88
9- #region Global Variables
9+
10+ # region Global Variables
1011
1112ROOT = Path (__file__ ).resolve ().parent .parent
12- SCRIPT = ROOT / ' tools.sh'
13- VENV_TEST = ROOT / ' .venv_test'
14- PACKAGE_NAME = ' stat-log-db'
13+ SCRIPT = ROOT / " tools.sh"
14+ VENV_TEST = ROOT / " .venv_test"
15+ PACKAGE_NAME = " stat-log-db"
1516
1617GITHUB_ACTIONS = os .getenv ("GITHUB_ACTIONS" ) == "true"
1718
18- #endregion
19+ # endregion
20+
1921
22+ # region testing tools
2023
21- #region testing tools
2224
2325def _ensure_test_venv ():
2426 """Ensure the test virtual environment is created."""
2527 if not VENV_TEST .exists ():
26- subprocess .run ([sys .executable , '-m' , 'venv' , str (VENV_TEST )], check = True )
28+ subprocess .run ([sys .executable , "-m" , "venv" , str (VENV_TEST )], check = True )
29+
2730
2831def _venv_python ():
2932 """Return path to the virtual environment's python interpreter."""
30- return VENV_TEST / ('Scripts' if os .name == 'nt' else 'bin' ) / ('python.exe' if os .name == 'nt' else 'python' )
33+ return VENV_TEST / ("Scripts" if os .name == "nt" else "bin" ) / ("python.exe" if os .name == "nt" else "python" )
34+
3135
3236def is_installed (package : str ) -> bool :
3337 """
@@ -36,26 +40,28 @@ def is_installed(package: str) -> bool:
3640 """
3741 _ensure_test_venv ()
3842 python_executable = _venv_python ()
39- result = subprocess .run ([str (python_executable ), '-m' , ' pip' , ' show' , package ], capture_output = True , text = True )
43+ result = subprocess .run ([str (python_executable ), "-m" , " pip" , " show" , package ], capture_output = True , text = True )
4044 return result .returncode == 0
4145
46+
4247def run_tools (args , use_test_venv = False ):
4348 """Run tools.sh returning (code, stdout+stderr)."""
4449 env = os .environ .copy ()
4550 if use_test_venv :
4651 _ensure_test_venv ()
47- scripts_dir = VENV_TEST / (' Scripts' if os .name == 'nt' else ' bin' )
48- env [' PATH' ] = str (scripts_dir ) + os .pathsep + env .get (' PATH' , '' )
49- env [' VIRTUAL_ENV' ] = str (VENV_TEST )
50- env [' PYTHONHOME' ] = '' # ensure venv python resolution
51- bash = r' C:\Program Files\Git\bin\bash.exe' if os .name == 'nt' else ' bash' # TODO: indicate to the user that they need git bash
52+ scripts_dir = VENV_TEST / (" Scripts" if os .name == "nt" else " bin" )
53+ env [" PATH" ] = str (scripts_dir ) + os .pathsep + env .get (" PATH" , "" )
54+ env [" VIRTUAL_ENV" ] = str (VENV_TEST )
55+ env [" PYTHONHOME" ] = "" # ensure venv python resolution
56+ bash = r" C:\Program Files\Git\bin\bash.exe" if os .name == "nt" else " bash" # TODO: indicate to the user that they need git bash
5257 proc = subprocess .run ([bash , str (SCRIPT ), * args ], capture_output = True , text = True , cwd = ROOT , env = env )
5358 return proc .returncode , proc .stdout + proc .stderr
5459
55- #endregion
60+
61+ # endregion
5662
5763
58- @pytest .fixture () # scope="module"
64+ @pytest .fixture () # scope="module"
5965def test_venv ():
6066 """
6167 Provision an isolated virtual environment used for install/uninstall tests.
@@ -69,13 +75,13 @@ def test_venv():
6975
7076
7177def test_help ():
72- code , out = run_tools (['-h' ])
78+ code , out = run_tools (["-h" ])
7379 assert code == 0
7480 # Read README.md
75- readme_path = ROOT / ' README.md'
81+ readme_path = ROOT / " README.md"
7682 assert readme_path .exists (), f"README not found at { readme_path } "
7783 readme_content = None
78- with open (readme_path , 'r' , encoding = ' utf-8' ) as f :
84+ with open (readme_path , "r" , encoding = " utf-8" ) as f :
7985 readme_content = f .read ().strip ()
8086 assert not (readme_content is None ), "Unable to read README"
8187 # Compare README content with help output
@@ -84,66 +90,74 @@ def test_help():
8490 except AssertionError :
8591 assert out .strip () == readme_content .strip (), "Help output does not match README content (leading & trailing whitespace stripped)"
8692
93+
8794@pytest .mark .skipif (GITHUB_ACTIONS , reason = "Skipping test on GitHub Actions" )
8895def test_install_dev (test_venv ):
89- code , out = run_tools ([' -id' ], use_test_venv = True )
96+ code , out = run_tools ([" -id" ], use_test_venv = True )
9097 assert code == 0
91- assert 'Installing' in out
92- assert 'dev' in out
93- assert is_installed (PACKAGE_NAME ), 'Package should be installed after dev install'
98+ assert "Installing" in out
99+ assert "dev" in out
100+ assert is_installed (PACKAGE_NAME ), "Package should be installed after dev install"
101+
94102
95103@pytest .mark .skipif (GITHUB_ACTIONS , reason = "Skipping test on GitHub Actions" )
96104def test_install_normal (test_venv ):
97- code , out = run_tools ([' -in' ], use_test_venv = True )
105+ code , out = run_tools ([" -in" ], use_test_venv = True )
98106 assert code == 0
99- assert 'Installing' in out
100- assert 'dev' not in out
101- assert is_installed (PACKAGE_NAME ), 'Package should be installed after normal install'
107+ assert "Installing" in out
108+ assert "dev" not in out
109+ assert is_installed (PACKAGE_NAME ), "Package should be installed after normal install"
110+
102111
103112def test_install_invalid_arg (test_venv ):
104- code , out = run_tools ([' -ix' ], use_test_venv = True )
113+ code , out = run_tools ([" -ix" ], use_test_venv = True )
105114 assert code == 1
106- assert ('Unsupported argument' in out ) or ('Invalid install mode' in out )
107- assert not is_installed (PACKAGE_NAME ), 'Package should not be installed after invalid install argument'
115+ assert ("Unsupported argument" in out ) or ("Invalid install mode" in out )
116+ assert not is_installed (PACKAGE_NAME ), "Package should not be installed after invalid install argument"
117+
108118
109119@pytest .mark .skipif (GITHUB_ACTIONS , reason = "Skipping test on GitHub Actions" )
110120def test_uninstall (test_venv ):
111121 # Ensure something installed first (dev mode)
112- icode , iout = run_tools ([' -id' ], use_test_venv = True )
122+ icode , iout = run_tools ([" -id" ], use_test_venv = True )
113123 assert icode == 0
114- assert is_installed (PACKAGE_NAME ), ' Package should be installed (before uninstall)'
115- ucode , uout = run_tools (['-u' ], use_test_venv = True )
124+ assert is_installed (PACKAGE_NAME ), " Package should be installed (before uninstall)"
125+ ucode , uout = run_tools (["-u" ], use_test_venv = True )
116126 assert ucode == 0
117- assert 'Uninstalling' in uout
118- assert 'Uninstall complete' in uout
119- assert not is_installed (PACKAGE_NAME ), 'Package should not be installed after uninstall'
127+ assert "Uninstalling" in uout
128+ assert "Uninstall complete" in uout
129+ assert not is_installed (PACKAGE_NAME ), "Package should not be installed after uninstall"
130+
120131
121132@pytest .mark .skipif (GITHUB_ACTIONS , reason = "Skipping test on GitHub Actions" )
122133def test_install_and_clean_multi_flag (test_venv ):
123- code , out = run_tools ([' -id' , '-c' ], use_test_venv = True )
134+ code , out = run_tools ([" -id" , "-c" ], use_test_venv = True )
124135 assert code == 0
125- assert is_installed (PACKAGE_NAME ), 'Package should be installed'
126- assert 'Installing' in out
127- assert 'Cleaning up workspace' in out
128- assert 'Cleanup complete' in out
129- assert is_installed (PACKAGE_NAME ), 'Cleanup should not remove installed package'
136+ assert is_installed (PACKAGE_NAME ), "Package should be installed"
137+ assert "Installing" in out
138+ assert "Cleaning up workspace" in out
139+ assert "Cleanup complete" in out
140+ assert is_installed (PACKAGE_NAME ), "Cleanup should not remove installed package"
141+
130142
131143def test_test_no_arg ():
132- code , out = run_tools (['-t' ])
144+ code , out = run_tools (["-t" ])
133145 assert code == 1
134146 try :
135- assert out == ' Option -t requires an argument'
147+ assert out == " Option -t requires an argument"
136148 except AssertionError :
137- assert out .strip () == 'Option -t requires an argument'
149+ assert out .strip () == "Option -t requires an argument"
150+
138151
139152def test_test_invalid_arg ():
140- code , out = run_tools ([' -tx' ])
153+ code , out = run_tools ([" -tx" ])
141154 assert code == 1
142- assert ('Unsupported argument' in out ) or ('Invalid test mode' in out )
155+ assert ("Unsupported argument" in out ) or ("Invalid test mode" in out )
156+
143157
144158@pytest .mark .skipif (GITHUB_ACTIONS , reason = "Skipping test on GitHub Actions" )
145159def test_clean ():
146- code , out = run_tools (['-c' ])
160+ code , out = run_tools (["-c" ])
147161 assert code == 0
148- assert ' Cleaning up workspace' in out
149- assert ' Cleanup complete' in out
162+ assert " Cleaning up workspace" in out
163+ assert " Cleanup complete" in out
0 commit comments