1- import subprocess
2- from pathlib import Path
3-
41import pytest
52
63
7- @pytest .fixture (scope = "module" )
8- def poetry_path () -> str :
9- result = subprocess .run (["which" , "poetry" ], capture_output = True , text = True )
10- poetry_path = result .stdout .strip ()
11- return poetry_path
12-
13-
14- @pytest .fixture (scope = "module" )
15- def git_path () -> str :
16- result = subprocess .run (["which" , "git" ], capture_output = True , text = True )
17- git_path = result .stdout .strip ()
18- return git_path
19-
20-
21- @pytest .fixture (scope = "module" )
22- def run_command (poetry_path , git_path , new_project ):
23- """
24- Run subprocess command with captured output and a limited environment (env).
25-
26- We restrict the environment as different systems & tools (i.e. PyCharm) include
27- environment variables which may supersede the ones provided here. In such cases,
28- this can lead to a breaking alteration in the PTB poetry environment. Thus,
29- we provide the minimum information needed to execute the pre-commit command.
30- """
31-
32- def _run_command_fixture (command , ** kwargs ):
33- defaults = {
34- "capture_output" : True ,
35- "check" : True ,
36- "cwd" : new_project ,
37- "env" : {"PATH" : f"{ Path (git_path ).parent } :{ Path (poetry_path ).parent } " },
38- "text" : True ,
39-
40- }
41- config = {** defaults , ** kwargs }
42-
43- return subprocess .run (command , ** config )
44-
45- return _run_command_fixture
46-
47-
484@pytest .fixture (scope = "module" )
495def pre_commit (run_command , new_project , poetry_path ):
50- run_command (command = ["git" , "init" ])
51- run_command ([poetry_path , "install" ])
6+ run_command (["git" , "init" ])
527 run_command ([poetry_path , "run" , "--" , "pre-commit" , "install" ])
538
549
@@ -61,15 +16,15 @@ def _command(poetry_path: str, stage: str) -> list[str]:
6116
6217 def test_stage_pre_commit (self , pre_commit , poetry_path , run_command ):
6318 command = self ._command (poetry_path , "pre-commit" )
64- output = run_command (command = command , check = False )
19+ output = run_command (command , check = False )
6520
6621 assert "Failed" not in output .stdout
6722 assert "Passed" in output .stdout
6823 assert output .returncode == 0
6924
7025 def test_stage_pre_push (self , pre_commit , poetry_path , run_command ):
7126 command = self ._command (poetry_path , "pre-push" )
72- output = run_command (command = command , check = False )
27+ output = run_command (command , check = False )
7328
7429 assert "Failed" not in output .stdout
7530 assert "Passed" in output .stdout
0 commit comments