Skip to content

Commit dd471c8

Browse files
committed
Fix dockerfile definition if directory name ends with ".git"
After changes in 92f0a85, the dockerfile parameter is igored if the (local) work directory's name ends in `.git`. This commit fixes the regression and adds more tests. Signed-off-by: Monika Kairaityte <monika@kibit.lt>
1 parent 1113c83 commit dd471c8

File tree

5 files changed

+161
-24
lines changed

5 files changed

+161
-24
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed regression of dockerfile definition if working directory name ends with ".git".

podman_compose.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ def normalize_service_final(service: dict[str, Any], project_dir: str) -> dict[s
17991799
build = service["build"]
18001800
context = build if isinstance(build, str) else build.get("context", ".")
18011801

1802-
if not is_path_git_url(context):
1802+
if not is_context_git_url(context):
18031803
context = os.path.normpath(os.path.join(project_dir, context))
18041804
if not isinstance(service["build"], dict):
18051805
service["build"] = {}
@@ -2788,9 +2788,18 @@ async def compose_push(compose: PodmanCompose, args: argparse.Namespace) -> None
27882788
await compose.podman.run([], "push", [cnt["image"]])
27892789

27902790

2791-
def is_path_git_url(path: str) -> bool:
2791+
def is_context_git_url(path: str) -> bool:
27922792
r = urllib.parse.urlparse(path)
2793-
return r.scheme == 'git' or r.path.endswith('.git')
2793+
if r.scheme in ('git', 'http', 'https', 'ssh', 'file', 'rsync'):
2794+
return True
2795+
# URL contains a ":" character, a hint of a valid URL
2796+
if r.scheme != "" and r.netloc == "" and r.path != "":
2797+
return True
2798+
if r.scheme == "": # tweak path URL to get username from url parser
2799+
r = urllib.parse.urlparse("ssh://" + path)
2800+
if r.username is not None and r.username != "":
2801+
return True
2802+
return False
27942803

27952804

27962805
def adjust_build_ssh_key_paths(compose: PodmanCompose, agent_or_key: str) -> str:
@@ -2834,8 +2843,8 @@ def cleanup_temp_dockfile() -> None:
28342843
cleanup_callbacks.append(cleanup_temp_dockfile)
28352844

28362845
build_args = []
2837-
2838-
if not is_path_git_url(ctx):
2846+
# if givent context was not recognized as git url, try joining paths to get a file locally
2847+
if not is_context_git_url(ctx):
28392848
custom_dockerfile_given = False
28402849
if dockerfile:
28412850
dockerfile = os.path.join(ctx, dockerfile)

tests/unit/test_container_to_build_args.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,6 @@ def test_context_invalid_git_url_git_is_not_prefix(self):
210210
with self.assertRaises(OSError):
211211
container_to_build_args(c, cnt, args, lambda path: False)
212212

213-
def test_context_invalid_git_url_git_is_not_suffix(self):
214-
c = create_compose_mock()
215-
216-
cnt = get_minimal_container()
217-
cnt['build']['context'] = "https://github.com/test_repo.git/not_suffix"
218-
args = get_minimal_args()
219-
220-
with self.assertRaises(OSError):
221-
container_to_build_args(c, cnt, args, lambda path: False)
222-
223213
def test_build_ssh_absolute_path(self):
224214
c = create_compose_mock()
225215

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from podman_compose import is_context_git_url
8+
9+
10+
class TestIsContextGitUrl(unittest.TestCase):
11+
@parameterized.expand([
12+
("with_url_fragment", "http://host.xz/path/to/repo.git#fragment", True),
13+
("suffix_and_prefix", "git://host.xz/path/to/repo.git", True),
14+
("empty_url_path", "http://#fragment", True),
15+
("no_prefix", "http://host.xz/path/to/repo", True),
16+
("wrong_prefix_git", "gitt://host.xz/path/to/repo", False),
17+
("wrong_prefix_http", "htt://host.xz/path/to/repo.git", False),
18+
("user_path_ending_with_git", "path/to/workdir.git", False),
19+
("", "/path/to/workdir.git", False),
20+
("", "/path/to:workdir.git", False),
21+
("", "/path/to@workdir.git", False),
22+
("", "~/path/to/workdir.git", False),
23+
# many of possible ways git url can look like
24+
("", "http://example.com/my-project.git", True),
25+
("", "file:///absolute/path/to/my-project.git", True),
26+
("", "ssh:user@example.com:my-project", True),
27+
("", "git@github.com:user/project.git", True),
28+
("", "https://github.com/user/project.git", True),
29+
("", "http://github.com/user/project.git", True),
30+
("", "git@192.168.101.127:user/project.git", True),
31+
("", "https://192.168.101.127/user/project.git", True),
32+
("", "http://192.168.101.127/user/project.git", True),
33+
("", "ssh://user@host.xz:port/path/to/repo.git/", True),
34+
("", "ssh://user@host.xz/path/to/repo.git/", True),
35+
("", "ssh://host.xz:port/path/to/repo.git/", True),
36+
("", "ssh://host.xz/path/to/repo.git/", True),
37+
("", "ssh://user@host.xz/path/to/repo.git/", True),
38+
("", "ssh://host.xz/path/to/repo.git/", True),
39+
("", "ssh://user@host.xz/~user/path/to/repo.git/", True),
40+
("", "ssh://host.xz/~user/path/to/repo.git/", True),
41+
("", "ssh://user@host.xz/~/path/to/repo.git", True),
42+
("", "ssh://host.xz/~/path/to/repo.git", True),
43+
("", "git://host.xz/path/to/repo.git/", True),
44+
("", "git://host.xz/~user/path/to/repo.git/", True),
45+
("", "http://host.xz/path/to/repo.git/", True),
46+
("", "https://host.xz/path/to/repo.git/", True),
47+
("", "git@custom-gitlab:my-group/myrepo.git", True),
48+
("", "ssh://user@host.xz:port/path/to/repo.git/", True),
49+
("", "ssh://user@host.xz/path/to/repo.git/", True),
50+
("", "ssh://host.xz:port/path/to/repo.git/", True),
51+
("", "ssh://host.xz/path/to/repo.git/", True),
52+
("", "ssh://user@host.xz/path/to/repo.git/", True),
53+
("", "ssh://host.xz/path/to/repo.git/", True),
54+
("", "ssh://user@host.xz/~user/path/to/repo.git/", True),
55+
("", "ssh://host.xz/~user/path/to/repo.git/", True),
56+
("", "ssh://user@host.xz/~/path/to/repo.git", True),
57+
("", "ssh://host.xz/~/path/to/repo.git", True),
58+
("", "git://host.xz/path/to/repo.git/", True),
59+
("", "git://host.xz/~user/path/to/repo.git/", True),
60+
("", "http://host.xz/path/to/repo.git/", True),
61+
("", "https://host.xz/path/to/repo.git/", True),
62+
("", "ssh:user@example.com:my-project", True),
63+
("", "user@host.xz:/path/to/repo.git/", True),
64+
("", "host.xz:/path/to/repo.git/", True),
65+
("", "host:path/to/repo.git", True),
66+
("", "host:path/to/repo", True),
67+
("", "user@host.xz:~user/path/to/repo.git/", True),
68+
("", "host.xz:~user/path/to/repo.git/", True),
69+
("", "user@host.xz:path/to/repo.git", True),
70+
("", "user@path/to/repo", True),
71+
("", "host.xz:path/to/repo.git", True),
72+
("", "rsync://host.xz/path/to/repo.git/", True),
73+
("", "file:///path/to/repo.git/", True),
74+
("", "file://~/path/to/repo.git/", True),
75+
("", "github.com:containers/podman-compose.git", True),
76+
("", "github:containers/podman-compose.git", True),
77+
("", "https://github.com/test_repo.git/git_not_suffix", True),
78+
])
79+
def test_is_path_git_url(self, test_name: str, path: str, result: bool) -> None:
80+
self.assertEqual(is_context_git_url(path), result)

tests/unit/test_is_path_git_url.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,76 @@
44

55
from parameterized import parameterized
66

7-
from podman_compose import is_path_git_url
7+
from podman_compose import is_context_git_url
88

99

1010
class TestIsPathGitUrl(unittest.TestCase):
1111
@parameterized.expand([
12-
("prefix_git", "git://host.xz/path/to/repo", True),
13-
("prefix_almost_git", "gitt://host.xz/path/to/repo", False),
14-
("prefix_wrong", "http://host.xz/path/to/repo", False),
15-
("suffix_git", "http://host.xz/path/to/repo.git", True),
16-
("suffix_wrong", "http://host.xz/path/to/repo", False),
17-
("suffix_with_url_fragment", "http://host.xz/path/to/repo.git#fragment", True),
12+
("with_url_fragment", "http://host.xz/path/to/repo.git#fragment", True),
1813
("suffix_and_prefix", "git://host.xz/path/to/repo.git", True),
19-
("empty_url_path", "http://#fragment", False),
14+
("empty_url_path", "http://#fragment", True),
15+
("no_prefix", "http://host.xz/path/to/repo", True),
16+
("wrong_prefix_git", "gitt://host.xz/path/to/repo", False),
17+
("wrong_prefix_http", "htt://host.xz/path/to/repo.git", False),
18+
("user_path_ending_with_git", "path/to/workdir.git", False),
19+
("", "/path/to/workdir.git", False),
20+
("", "/path/to:workdir.git", False),
21+
("", "/path/to@workdir.git", False),
22+
("", "~/path/to/workdir.git", False),
23+
# many of possible ways git url can look like
24+
("", "http://example.com/my-project.git", True),
25+
("", "file:///absolute/path/to/my-project.git", True),
26+
("", "ssh:user@example.com:my-project", True),
27+
("", "git@github.com:user/project.git", True),
28+
("", "https://github.com/user/project.git", True),
29+
("", "http://github.com/user/project.git", True),
30+
("", "git@192.168.101.127:user/project.git", True),
31+
("", "https://192.168.101.127/user/project.git", True),
32+
("", "http://192.168.101.127/user/project.git", True),
33+
("", "ssh://user@host.xz:port/path/to/repo.git/", True),
34+
("", "ssh://user@host.xz/path/to/repo.git/", True),
35+
("", "ssh://host.xz:port/path/to/repo.git/", True),
36+
("", "ssh://host.xz/path/to/repo.git/", True),
37+
("", "ssh://user@host.xz/path/to/repo.git/", True),
38+
("", "ssh://host.xz/path/to/repo.git/", True),
39+
("", "ssh://user@host.xz/~user/path/to/repo.git/", True),
40+
("", "ssh://host.xz/~user/path/to/repo.git/", True),
41+
("", "ssh://user@host.xz/~/path/to/repo.git", True),
42+
("", "ssh://host.xz/~/path/to/repo.git", True),
43+
("", "git://host.xz/path/to/repo.git/", True),
44+
("", "git://host.xz/~user/path/to/repo.git/", True),
45+
("", "http://host.xz/path/to/repo.git/", True),
46+
("", "https://host.xz/path/to/repo.git/", True),
47+
("", "git@custom-gitlab:my-group/myrepo.git", True),
48+
("", "ssh://user@host.xz:port/path/to/repo.git/", True),
49+
("", "ssh://user@host.xz/path/to/repo.git/", True),
50+
("", "ssh://host.xz:port/path/to/repo.git/", True),
51+
("", "ssh://host.xz/path/to/repo.git/", True),
52+
("", "ssh://user@host.xz/path/to/repo.git/", True),
53+
("", "ssh://host.xz/path/to/repo.git/", True),
54+
("", "ssh://user@host.xz/~user/path/to/repo.git/", True),
55+
("", "ssh://host.xz/~user/path/to/repo.git/", True),
56+
("", "ssh://user@host.xz/~/path/to/repo.git", True),
57+
("", "ssh://host.xz/~/path/to/repo.git", True),
58+
("", "git://host.xz/path/to/repo.git/", True),
59+
("", "git://host.xz/~user/path/to/repo.git/", True),
60+
("", "http://host.xz/path/to/repo.git/", True),
61+
("", "https://host.xz/path/to/repo.git/", True),
62+
("", "ssh:user@example.com:my-project", True),
63+
("", "user@host.xz:/path/to/repo.git/", True),
64+
("", "host.xz:/path/to/repo.git/", True),
65+
("", "host:path/to/repo.git", True),
66+
("", "host:path/to/repo", True),
67+
("", "user@host.xz:~user/path/to/repo.git/", True),
68+
("", "host.xz:~user/path/to/repo.git/", True),
69+
("", "user@host.xz:path/to/repo.git", True),
70+
("", "user@path/to/repo", True),
71+
("", "host.xz:path/to/repo.git", True),
72+
("", "rsync://host.xz/path/to/repo.git/", True),
73+
("", "file:///path/to/repo.git/", True),
74+
("", "file://~/path/to/repo.git/", True),
75+
("", "github.com:containers/podman-compose.git", True),
76+
("", "github:containers/podman-compose.git", True),
2077
])
2178
def test_is_path_git_url(self, test_name: str, path: str, result: bool) -> None:
22-
self.assertEqual(is_path_git_url(path), result)
79+
self.assertEqual(is_context_git_url(path), result)

0 commit comments

Comments
 (0)