Skip to content

Commit f6bdc98

Browse files
committed
Migrate new_cookie fixture to conftest.py
1 parent c375378 commit f6bdc98

3 files changed

Lines changed: 60 additions & 83 deletions

File tree

tests/conftest.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
import json
12
import os
3+
import sys
24
from pathlib import Path
35
from tempfile import TemporaryDirectory
46
from typing import Iterator
57
from unittest.mock import patch
68

79
import pytest
810

11+
from cookie_python.new import main as new_cookie_main
12+
13+
AUTHOR_NAME = "Ness"
14+
AUTHOR_EMAIL = "ness@onett.example"
15+
PROJECT_NAME = "unit-test-1"
16+
17+
18+
@pytest.fixture
19+
def project_environment() -> Iterator[None]:
20+
add_values = dict(
21+
GIT_AUTHOR_NAME=AUTHOR_NAME,
22+
GIT_AUTHOR_EMAIL=AUTHOR_EMAIL,
23+
GIT_COMMITTER_NAME=AUTHOR_NAME,
24+
GIT_COMMITTER_EMAIL=AUTHOR_EMAIL,
25+
GITHUB_API_TOKEN="unittest_token",
26+
)
27+
with patch.dict(os.environ, add_values):
28+
yield
29+
930

1031
@pytest.fixture(scope="session", autouse=True)
1132
def subprocess_environment() -> Iterator[None]:
@@ -36,3 +57,33 @@ def opt_update_expected_outputs(request: pytest.FixtureRequest) -> bool:
3657
def temp_dir() -> Iterator[Path]:
3758
with TemporaryDirectory(prefix="cookie-python.unittest.") as td:
3859
yield Path(td)
60+
61+
62+
@pytest.fixture(params=["@"])
63+
def new_cookie(
64+
request: pytest.FixtureRequest, project_environment: None, temp_dir: Path
65+
) -> Iterator[Path]:
66+
testargs = [
67+
"new-cookie",
68+
"--local",
69+
str(temp_dir),
70+
"--",
71+
"-d",
72+
"-y",
73+
"--extra-context",
74+
json.dumps(
75+
{
76+
"author_email": AUTHOR_EMAIL,
77+
"author_name": AUTHOR_NAME,
78+
"github_user": "ness.unittest.example",
79+
"project_description": "Unit test project",
80+
"project_name": PROJECT_NAME,
81+
"enable_container_publish": "yes",
82+
}
83+
),
84+
"-c",
85+
request.param,
86+
]
87+
with patch.object(sys, "argv", testargs):
88+
new_cookie_main()
89+
yield temp_dir / PROJECT_NAME

tests/test_manage_cookie.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import json
2-
import os
31
import subprocess
42
import sys
53
from pathlib import Path
@@ -11,53 +9,8 @@
119
import pytest
1210

1311
from cookie_python.manage.main import main as manage_cookie_main
14-
from cookie_python.new import main as new_cookie_main
1512

16-
AUTHOR_NAME = "Ness"
17-
AUTHOR_EMAIL = "ness@onett.example"
18-
PROJECT_NAME = "unit-test-1"
19-
20-
21-
@pytest.fixture
22-
def environ() -> Iterator[None]:
23-
add_values = dict(
24-
GIT_AUTHOR_NAME=AUTHOR_NAME,
25-
GIT_AUTHOR_EMAIL=AUTHOR_EMAIL,
26-
GIT_COMMITTER_NAME=AUTHOR_NAME,
27-
GIT_COMMITTER_EMAIL=AUTHOR_EMAIL,
28-
GITHUB_API_TOKEN="unittest_token",
29-
)
30-
with patch.dict(os.environ, add_values):
31-
yield
32-
33-
34-
@pytest.fixture(params=["@"])
35-
def new_cookie(
36-
request: pytest.FixtureRequest, environ: None, temp_dir: Path
37-
) -> Iterator[Path]:
38-
testargs = [
39-
"new-cookie",
40-
"--local",
41-
str(temp_dir),
42-
"--",
43-
"-d",
44-
"-y",
45-
"--extra-context",
46-
json.dumps(
47-
{
48-
"author_email": AUTHOR_EMAIL,
49-
"author_name": AUTHOR_NAME,
50-
"github_user": "ness.unittest.example",
51-
"project_description": "Unit test project",
52-
"project_name": PROJECT_NAME,
53-
}
54-
),
55-
"-c",
56-
request.param,
57-
]
58-
with patch.object(sys, "argv", testargs):
59-
new_cookie_main()
60-
yield temp_dir / PROJECT_NAME
13+
from .conftest import AUTHOR_NAME, PROJECT_NAME
6114

6215

6316
@pytest.fixture(autouse=True)

tests/test_new_cookie.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1-
import json
21
import os
32
import subprocess
4-
import sys
53
from pathlib import Path
6-
from unittest import mock
74

8-
from cookie_python.new import main
95

10-
11-
def test_new_cookie_create(temp_dir: Path) -> None:
12-
testargs = [
13-
"new-cookie",
14-
"--local",
15-
str(temp_dir),
16-
"--",
17-
"-d",
18-
"-y",
19-
"--extra-context",
20-
json.dumps(
21-
{
22-
"author_email": "ness@onett.example",
23-
"author_name": "Ness",
24-
"github_user": "ness.unittest.example",
25-
"project_description": "Unit test project",
26-
"project_name": "unit-test-1",
27-
"enable_container_publish": "yes",
28-
}
29-
),
30-
]
31-
with mock.patch.object(sys, "argv", testargs):
32-
main()
33-
project_dir = temp_dir / "unit-test-1"
34-
assert os.path.isdir(project_dir)
6+
def test_new_cookie_create(new_cookie: Path, temp_dir: Path) -> None:
7+
assert os.path.isdir(new_cookie)
358
assert not (
369
subprocess.check_output(
37-
["git", "status", "--porcelain=v1"], cwd=project_dir
10+
["git", "status", "--porcelain=v1"], cwd=new_cookie
3811
)
3912
.decode("utf-8")
4013
.strip()
4114
), "Untracked files present in template-rendered project"
4215

4316
# Verify cruft is up to date
4417
subprocess.check_call(
45-
["poetry", "run", "pip", "install", "toml"], cwd=project_dir
18+
["poetry", "run", "pip", "install", "toml"], cwd=new_cookie
4619
)
4720
subprocess.check_call(
48-
["poetry", "run", "cruft", "diff", "--exit-code"], cwd=project_dir
21+
["poetry", "run", "cruft", "diff", "--exit-code"], cwd=new_cookie
4922
)
5023

5124
# Install rendered project
52-
subprocess.check_call(["poetry", "sync"], cwd=project_dir)
25+
subprocess.check_call(["poetry", "sync"], cwd=new_cookie)
5326
# Run rendered project's tests
54-
subprocess.check_call(["poetry", "run", "poe", "test"], cwd=project_dir)
27+
subprocess.check_call(["poetry", "run", "poe", "test"], cwd=new_cookie)
5528
# Build container for rendered project
5629
subprocess.check_call(
57-
["docker", "build", ".", "--no-cache"], cwd=project_dir
30+
["docker", "build", ".", "--no-cache"], cwd=new_cookie
5831
)

0 commit comments

Comments
 (0)