Skip to content

Commit d3b33c2

Browse files
committed
Moved default flag values out as module-wide variables; tried mocking return value of 'Path.exists()'
1 parent d2a0905 commit d3b33c2

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

tests/cli/test_build_images.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
import grp
22
import os
33
import sys
4-
from pathlib import Path
54
from unittest.mock import call, patch
65

76
import pytest
87

98
from murfey.cli.build_images import run
109

10+
images = [f"test_image_{n}" for n in range(3)]
11+
12+
# Set defaults of the various flags
13+
def_tags = ["latest"]
14+
def_src = "/home/runner/work/python-murfey/python-murfey"
15+
def_dst = "localhost"
16+
def_uid = os.getuid()
17+
def_gid = os.getgid()
18+
def_gname = grp.getgrgid(os.getgid()).gr_name if hasattr(grp, "getgrgid") else "nogroup"
19+
def_dry_run = False
20+
21+
1122
test_run_params_matrix: tuple[
1223
tuple[list[str], list[str], str, str, str, str, str, bool]
1324
] = (
1425
# Images | Tags | Source | Destination | User ID | Group ID | Group Name | Dry Run
1526
# Default settings
16-
([f"test_image_{n}" for n in range(3)], [], "", "", "", "", "", False),
27+
(images, [], "", "", "", "", "", False),
1728
)
1829

1930

2031
@pytest.mark.parametrize("build_params", test_run_params_matrix)
32+
@patch("murfey.cli.build_images.Path.exists")
2133
@patch("murfey.cli.build_images.run_subprocess")
2234
@patch("murfey.cli.build_images.cleanup")
2335
@patch("murfey.cli.build_images.push_images")
@@ -29,6 +41,7 @@ def test_run(
2941
mock_push,
3042
mock_clean,
3143
mock_subprocess,
44+
mock_exists,
3245
build_params: tuple[list[str], list[str], str, str, str, str, str, bool],
3346
):
3447
"""
@@ -39,17 +52,6 @@ def test_run(
3952
# Unpack build params
4053
images, tags, src, dst, uid, gid, gname, dry_run = build_params
4154

42-
# Set defaults of the various flags
43-
def_tags = ["latest"]
44-
def_src = "/home/runner/work/python-murfey/python-murfey"
45-
def_dst = "localhost"
46-
def_uid = os.getuid()
47-
def_gid = os.getgid()
48-
def_gname = (
49-
grp.getgrgid(os.getgid()).gr_name if hasattr(grp, "getgrgid") else "nogroup"
50-
)
51-
def_dry_run = False
52-
5355
# Set up the command based on what these values are
5456
build_cmd = [
5557
"murfey.build_images",
@@ -84,10 +86,8 @@ def test_run(
8486
for image in images
8587
]
8688

87-
# Create Dockerfiles at the location it expects
88-
for image in images:
89-
dockerfile = Path(src if src else def_src) / "Dockerfiles" / image
90-
dockerfile.touch(exist_ok=True)
89+
# Mock the check for the existence of the Dockerfiles
90+
mock_exists.return_value = True
9191

9292
# Run the function with the command
9393
run()

0 commit comments

Comments
 (0)