1
+ import json
1
2
import os
3
+ import subprocess
4
+ import sys
5
+ from pathlib import Path
2
6
from tempfile import TemporaryDirectory
3
- from typing import Iterator
7
+ from collections .abc import Iterator
8
+ from unittest .mock import patch
4
9
5
10
import pytest
6
11
12
+ from cookie_python .new import main as new_cookie_main
13
+
14
+ AUTHOR_NAME = "Ness"
15
+ AUTHOR_EMAIL = "ness@onett.example"
16
+ PROJECT_NAME = "unit-test-1"
17
+
18
+
19
+ @pytest .fixture
20
+ def project_environment () -> Iterator [None ]:
21
+ add_values = dict (
22
+ GIT_AUTHOR_NAME = AUTHOR_NAME ,
23
+ GIT_AUTHOR_EMAIL = AUTHOR_EMAIL ,
24
+ GIT_COMMITTER_NAME = AUTHOR_NAME ,
25
+ GIT_COMMITTER_EMAIL = AUTHOR_EMAIL ,
26
+ GITHUB_API_TOKEN = "unittest_token" ,
27
+ )
28
+ with patch .dict (os .environ , add_values ):
29
+ yield
30
+
31
+
32
+ @pytest .fixture (scope = "session" , autouse = True )
33
+ def subprocess_environment () -> Iterator [None ]:
34
+ with patch .dict (os .environ , {}) as patched_env :
35
+ if "VIRTUAL_ENV" in patched_env :
36
+ del patched_env ["VIRTUAL_ENV" ]
37
+ yield
38
+
7
39
8
40
def pytest_addoption (parser : pytest .Parser ) -> None :
9
41
group = parser .getgroup ("cookie" , "python-cookie test helpers" )
@@ -23,6 +55,39 @@ def opt_update_expected_outputs(request: pytest.FixtureRequest) -> bool:
23
55
24
56
25
57
@pytest .fixture
26
- def temp_dir () -> Iterator [str ]:
58
+ def temp_dir () -> Iterator [Path ]:
27
59
with TemporaryDirectory (prefix = "cookie-python.unittest." ) as td :
28
- yield os .path .join (td )
60
+ yield Path (td )
61
+
62
+
63
+ @pytest .fixture (params = ["@" ])
64
+ def new_cookie (
65
+ request : pytest .FixtureRequest , project_environment : None , temp_dir : Path
66
+ ) -> Iterator [Path ]:
67
+ testargs = [
68
+ "new-cookie" ,
69
+ "--local" ,
70
+ str (temp_dir ),
71
+ "--" ,
72
+ "-d" ,
73
+ "-y" ,
74
+ "--extra-context" ,
75
+ json .dumps (
76
+ {
77
+ "author_email" : AUTHOR_EMAIL ,
78
+ "author_name" : AUTHOR_NAME ,
79
+ "github_user" : "ness.unittest.example" ,
80
+ "project_description" : "Unit test project" ,
81
+ "project_name" : PROJECT_NAME ,
82
+ "enable_container_publish" : "yes" ,
83
+ }
84
+ ),
85
+ "-c" ,
86
+ request .param ,
87
+ ]
88
+ with patch .object (sys , "argv" , testargs ):
89
+ new_cookie_main ()
90
+ project_dir = temp_dir / PROJECT_NAME
91
+ yield project_dir
92
+ if (project_dir / "pyproject.toml" ).is_file ():
93
+ subprocess .run (["poetry" , "env" , "remove" , "--all" ], cwd = project_dir )
0 commit comments