Skip to content

Commit 4d8d52a

Browse files
committed
Find Compose files
1 parent 1ab8146 commit 4d8d52a

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

src/pytest_docker/plugin.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,30 @@ def docker_compose_command() -> str:
149149
return "docker compose"
150150

151151

152+
def _find_compose_file(basedir: Path) -> Optional[Path]:
153+
# Search for well-known filenames, in the order defined by the compose docs.
154+
# https://docs.docker.com/compose/intro/compose-application-model/#the-compose-file
155+
names = [
156+
"compose.yaml",
157+
"compose.yml",
158+
"docker-compose.yaml",
159+
"docker-compose.yml",
160+
]
161+
paths = (basedir / filename for filename in names)
162+
exisiting_paths = (path for path in paths if path.is_file())
163+
164+
return next(exisiting_paths, None)
165+
166+
152167
@pytest.fixture(scope=containers_scope)
153-
def docker_compose_file(pytestconfig: Any) -> Union[List[str], str]:
154-
"""Get an absolute path to the `docker-compose.yml` file. Override this
155-
fixture in your tests if you need a custom location."""
168+
def docker_compose_file(pytestconfig: Config) -> Union[List[str], str]:
169+
"""Get an absolute path to the compose file. Override this fixture in your tests
170+
if you need a custom location."""
171+
172+
tests_dir = Path(pytestconfig.rootpath) / "tests"
173+
fallback_path = tests_dir / "docker-compose.yml"
156174

157-
return os.path.join(str(pytestconfig.rootdir), "tests", "docker-compose.yml")
175+
return str(_find_compose_file(tests_dir) or fallback_path)
158176

159177

160178
@pytest.fixture(scope=containers_scope)

tests/test_fixtures.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
1-
import os.path
1+
import os
2+
from pathlib import Path
23
from typing import List
34

45
import pytest
56
from _pytest.fixtures import FixtureRequest
67
from _pytest.pytester import Pytester
78

8-
HERE = os.path.dirname(os.path.abspath(__file__))
99

10+
@pytest.fixture
11+
def tests_dir(pytester: Pytester) -> Path:
12+
dir = Path(pytester.path) / "tests"
13+
dir.mkdir()
14+
return dir
15+
16+
17+
@pytest.fixture(params=[[]])
18+
def tests_dir_contents(tests_dir: Path, request: FixtureRequest) -> List[Path]:
19+
filenames: List[str] = getattr(request, "param", [])
20+
paths = [tests_dir / filename for filename in filenames]
21+
for path in paths:
22+
path.touch()
23+
24+
return paths
25+
26+
27+
@pytest.mark.parametrize(
28+
("tests_dir_contents", "effective_compose_file"),
29+
[
30+
(
31+
[
32+
"compose.yaml",
33+
"compose.yml",
34+
"docker-compose.yaml",
35+
"docker-compose.yml",
36+
],
37+
"compose.yaml",
38+
),
39+
(["compose.yml", "docker-compose.yaml", "docker-compose.yml"], "compose.yml"),
40+
(["docker-compose.yaml", "docker-compose.yml"], "docker-compose.yaml"),
41+
(["docker-compose.yml"], "docker-compose.yml"),
42+
([], "docker-compose.yml"),
43+
],
44+
indirect=["tests_dir_contents"],
45+
)
46+
@pytest.mark.usefixtures("tests_dir_contents")
47+
def test_docker_compose_file(
48+
pytester: Pytester,
49+
effective_compose_file: str,
50+
tests_dir: Path,
51+
) -> None:
52+
pytester.makepyfile(
53+
f"""
54+
def test_docker_compose_file(docker_compose_file):
55+
assert docker_compose_file == "{tests_dir}/{effective_compose_file}"
56+
"""
57+
)
1058

11-
def test_docker_compose_file(docker_compose_file: str) -> None:
12-
assert docker_compose_file == os.path.join(HERE, "docker-compose.yml")
59+
result = pytester.runpytest()
60+
result.assert_outcomes(passed=1)
1361

1462

1563
def test_docker_compose_project(docker_compose_project_name: str) -> None:

0 commit comments

Comments
 (0)