Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4a9ff4

Browse files
committed
Simplify docker user fallback implementation and test
1 parent a21a4f4 commit d4a9ff4

2 files changed

Lines changed: 11 additions & 23 deletions

File tree

pre_commit/languages/docker.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
ENVIRONMENT_DIR = 'docker'
1616
PRE_COMMIT_LABEL = 'PRE_COMMIT'
17-
FALLBACK_UID = 1000
18-
FALLBACK_GID = 1000
1917
get_default_version = helpers.basic_get_default_version
2018
healthy = helpers.basic_healthy
2119

@@ -75,25 +73,18 @@ def install_environment(
7573
os.mkdir(directory)
7674

7775

78-
def getuid(): # pragma: windows no cover
76+
def get_docker_user(): # pragma: windows no cover
7977
try:
80-
return os.getuid()
78+
return '{}:{}'.format(os.getuid(), os.getgid())
8179
except AttributeError:
82-
return FALLBACK_UID
83-
84-
85-
def getgid(): # pragma: windows no cover
86-
try:
87-
return os.getgid()
88-
except AttributeError:
89-
return FALLBACK_GID
80+
return '1000:1000'
9081

9182

9283
def docker_cmd(): # pragma: windows no cover
9384
return (
9485
'docker', 'run',
9586
'--rm',
96-
'-u', '{}:{}'.format(getuid(), getgid()),
87+
'-u', get_docker_user(),
9788
# https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from
9889
# The `Z` option tells Docker to label the content with a private
9990
# unshared label. Only the current container can use a private volume.

tests/languages/docker_test.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ def test_docker_is_running_process_error():
1515
assert docker.docker_is_running() is False
1616

1717

18-
def test_docker_fallback_uid():
18+
def test_docker_fallback_user():
1919
def invalid_attribute():
2020
raise AttributeError
21-
with mock.patch('os.getuid', invalid_attribute, create=True):
22-
assert docker.getuid() == docker.FALLBACK_UID
23-
24-
25-
def test_docker_fallback_gid():
26-
def invalid_attribute():
27-
raise AttributeError
28-
with mock.patch('os.getgid', invalid_attribute, create=True):
29-
assert docker.getgid() == docker.FALLBACK_GID
21+
with mock.patch.multiple(
22+
'os', create=True,
23+
getuid=invalid_attribute,
24+
getgid=invalid_attribute,
25+
):
26+
assert docker.get_docker_user() == '1000:1000'

0 commit comments

Comments
 (0)