Skip to content

Commit 57c527c

Browse files
Evedelmuayyad-alsadi
authored andcommitted
add edits from review
Signed-off-by: Sergei Biriukov <svbiriukov@gmail.com>
1 parent d1f5ac9 commit 57c527c

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

podman_compose.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import glob
2525

2626
from threading import Thread
27-
from pathlib import Path
2827

2928
import shlex
3029

@@ -1302,10 +1301,12 @@ def normalize_service_final(service: dict, project_dir: str) -> dict:
13021301
if "build" in service:
13031302
build = service["build"]
13041303
context = build if is_str(build) else build.get("context", ".")
1305-
context = str((Path(project_dir) / context).resolve())
1306-
dockerfile = "Dockerfile"
1307-
if "dockerfile" in service["build"]:
1308-
dockerfile = service["build"]["dockerfile"]
1304+
context = os.path.normpath(os.path.join(project_dir, context))
1305+
dockerfile = (
1306+
"Dockerfile"
1307+
if is_str(build)
1308+
else service["build"].get("dockerfile", "Dockerfile")
1309+
)
13091310
if not is_dict(service["build"]):
13101311
service["build"] = {}
13111312
service["build"]["dockerfile"] = dockerfile

pytests/test_normalize_final_build.py

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import argparse
44
import copy
55
import os
6-
from pathlib import Path
76
import yaml
87
from podman_compose import (
98
normalize_service,
@@ -13,20 +12,20 @@
1312
PodmanCompose,
1413
)
1514

16-
15+
cwd = os.path.abspath(".")
1716
test_cases_simple_normalization = [
1817
({"image": "test-image"}, {"image": "test-image"}),
1918
(
2019
{"build": "."},
2120
{
22-
"build": {"context": str(Path.cwd()), "dockerfile": "Dockerfile"},
21+
"build": {"context": cwd, "dockerfile": "Dockerfile"},
2322
},
2423
),
2524
(
2625
{"build": "../relative"},
2726
{
2827
"build": {
29-
"context": str((Path.cwd() / "../relative").resolve()),
28+
"context": os.path.normpath(os.path.join(cwd, "../relative")),
3029
"dockerfile": "Dockerfile",
3130
},
3231
},
@@ -35,7 +34,7 @@
3534
{"build": "./relative"},
3635
{
3736
"build": {
38-
"context": str((Path.cwd() / "./relative").resolve()),
37+
"context": os.path.normpath(os.path.join(cwd, "./relative")),
3938
"dockerfile": "Dockerfile",
4039
},
4140
},
@@ -57,7 +56,7 @@
5756
},
5857
{
5958
"build": {
60-
"context": str(Path.cwd()),
59+
"context": cwd,
6160
"dockerfile": "Dockerfile",
6261
},
6362
},
@@ -70,7 +69,7 @@
7069
},
7170
{
7271
"build": {
73-
"context": str(Path.cwd()),
72+
"context": cwd,
7473
"dockerfile": "Dockerfile",
7574
},
7675
},
@@ -81,7 +80,7 @@
8180
},
8281
{
8382
"build": {
84-
"context": str((Path.cwd() / "../").resolve()),
83+
"context": os.path.normpath(os.path.join(cwd, "../")),
8584
"dockerfile": "test-dockerfile",
8685
},
8786
},
@@ -92,7 +91,7 @@
9291
},
9392
{
9493
"build": {
95-
"context": str(Path.cwd()),
94+
"context": cwd,
9695
"dockerfile": "./dev/test-dockerfile",
9796
},
9897
},
@@ -123,14 +122,14 @@ def test_pre_merge_normalize_does_not_affect_build_section() -> None:
123122
# [service.build] is normalised after merges
124123
#
125124
def test_normalize_service_final_returns_absolute_path_in_context() -> None:
126-
project_dir = str(Path.cwd().resolve())
125+
project_dir = cwd
127126
for test_input, expected_service in copy.deepcopy(test_cases_simple_normalization):
128127
actual_service = normalize_service_final(test_input, project_dir)
129128
assert expected_service == actual_service
130129

131130

132131
def test_normalize_returns_absolute_path_in_context() -> None:
133-
project_dir = str(Path.cwd().resolve())
132+
project_dir = cwd
134133
for test_input, expected_result in copy.deepcopy(test_cases_simple_normalization):
135134
compose_test = {"services": {"test-service": test_input}}
136135
compose_expected = {"services": {"test-service": expected_result}}
@@ -166,19 +165,19 @@ def test__parse_compose_file_when_single_compose() -> None:
166165
(
167166
{},
168167
{"build": "."},
169-
{"build": {"context": str(Path.cwd()), "dockerfile": "Dockerfile"}},
168+
{"build": {"context": cwd, "dockerfile": "Dockerfile"}},
170169
),
171170
(
172171
{"build": "."},
173172
{},
174-
{"build": {"context": str(Path.cwd()), "dockerfile": "Dockerfile"}},
173+
{"build": {"context": cwd, "dockerfile": "Dockerfile"}},
175174
),
176175
(
177176
{"build": "/workspace/absolute"},
178177
{"build": "./relative"},
179178
{
180179
"build": {
181-
"context": str((Path.cwd() / "./relative").resolve()),
180+
"context": os.path.normpath(os.path.join(cwd, "./relative")),
182181
"dockerfile": "Dockerfile",
183182
}
184183
},
@@ -196,22 +195,22 @@ def test__parse_compose_file_when_single_compose() -> None:
196195
(
197196
{"build": {"dockerfile": "test-dockerfile"}},
198197
{},
199-
{"build": {"context": str(Path.cwd()), "dockerfile": "test-dockerfile"}},
198+
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}},
200199
),
201200
(
202201
{},
203202
{"build": {"dockerfile": "test-dockerfile"}},
204-
{"build": {"context": str(Path.cwd()), "dockerfile": "test-dockerfile"}},
203+
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}},
205204
),
206205
(
207206
{},
208207
{"build": {"dockerfile": "test-dockerfile"}},
209-
{"build": {"context": str(Path.cwd()), "dockerfile": "test-dockerfile"}},
208+
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}},
210209
),
211210
(
212211
{"build": {"dockerfile": "test-dockerfile-1"}},
213212
{"build": {"dockerfile": "test-dockerfile-2"}},
214-
{"build": {"context": str(Path.cwd()), "dockerfile": "test-dockerfile-2"}},
213+
{"build": {"context": cwd, "dockerfile": "test-dockerfile-2"}},
215214
),
216215
(
217216
{"build": "/workspace/absolute"},
@@ -223,11 +222,44 @@ def test__parse_compose_file_when_single_compose() -> None:
223222
{"build": "/workspace/absolute"},
224223
{"build": {"context": "/workspace/absolute", "dockerfile": "test-dockerfile"}},
225224
),
225+
(
226+
{"build": {"dockerfile": "./test-dockerfile-1"}},
227+
{"build": {"dockerfile": "./test-dockerfile-2", "args": ["ENV1=1"]}},
228+
{
229+
"build": {
230+
"context": cwd,
231+
"dockerfile": "./test-dockerfile-2",
232+
"args": ["ENV1=1"],
233+
}
234+
},
235+
),
236+
(
237+
{"build": {"dockerfile": "./test-dockerfile-1", "args": ["ENV1=1"]}},
238+
{"build": {"dockerfile": "./test-dockerfile-2"}},
239+
{
240+
"build": {
241+
"context": cwd,
242+
"dockerfile": "./test-dockerfile-2",
243+
"args": ["ENV1=1"],
244+
}
245+
},
246+
),
247+
(
248+
{"build": {"dockerfile": "./test-dockerfile-1", "args": ["ENV1=1"]}},
249+
{"build": {"dockerfile": "./test-dockerfile-2", "args": ["ENV2=2"]}},
250+
{
251+
"build": {
252+
"context": cwd,
253+
"dockerfile": "./test-dockerfile-2",
254+
"args": ["ENV1=1", "ENV2=2"],
255+
}
256+
},
257+
),
226258
]
227259

228260

229261
#
230-
# running full parse over merged and extended compose files
262+
# running full parse over merged
231263
#
232264
def test__parse_compose_file_when_multiple_composes() -> None:
233265
for test_input, test_override, expected_result in copy.deepcopy(
@@ -274,5 +306,5 @@ def dump_yaml(compose: dict, name: str) -> None:
274306
def test_clean_test_yamls() -> None:
275307
test_files = ["test-compose-1.yaml", "test-compose-2.yaml", "test-compose.yaml"]
276308
for file in test_files:
277-
if Path(file).exists():
309+
if os.path.exists(file):
278310
os.remove(file)

0 commit comments

Comments
 (0)