New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: fix path mounting when running in Docker #1888
Conversation
1791cbf
to
a3ff253
Compare
I don't really understand why CI is not happy. All lines are test-covered. what is |
that's branch coverage -- it means that once it enters the loop you've only tested the two positive cases inside and not the (implicit) |
@asottile fixed, please review and merge if it's fine |
tests/languages/docker_test.py
Outdated
@@ -12,3 +16,75 @@ def invalid_attribute(): | |||
getgid=invalid_attribute, | |||
): | |||
assert docker.get_docker_user() == () | |||
|
|||
|
|||
class TestInDocker: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't use test classes in this project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I do want to group tests for a given function together. Test classes are the best suited for this. Having bunch of files like test_modulename_functionname
is a bit too much IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't use test classes in this project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please be more constructive. I would like to have tests grouped together by their test target. Test classes is the built-in way to do so and in comparison with bare functions it improves both readability of the code an readability of reports. They don't introduce any extra tremendous boilerplate, so what's the problem?
tests/languages/docker_test.py
Outdated
|
||
@mock.patch.object(docker, 'is_in_docker', return_value=True) | ||
def test_in_docker_no_binds_same_path(self, _): | ||
binds_list: List[str] = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you inline this you shouldn't need the type signature (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got warning from CI about it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if you don't have this as a variable you shouldn't need the annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to have consistent tests so they look alike
tests/languages/docker_test.py
Outdated
@@ -12,3 +16,75 @@ def invalid_attribute(): | |||
getgid=invalid_attribute, | |||
): | |||
assert docker.get_docker_user() == () | |||
|
|||
|
|||
class TestInDocker: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't use test classes in this project
tests/languages/docker_test.py
Outdated
|
||
@mock.patch.object(docker, 'is_in_docker', return_value=True) | ||
def test_in_docker_no_binds_same_path(self, _): | ||
binds_list: List[str] = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if you don't have this as a variable you shouldn't need the annotation
tests/languages/docker_test.py
Outdated
|
||
@pytest.fixture | ||
def mock_file_fixture(self): | ||
return lambda read_data: \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no backslashes, again
you probably want this to be a yield fixture:
@pytest.fixture
def mocked_read():
with mock.patch.object(builtins, 'open', new_callable=mock_open) as mck:
yield mck
and then in your test, utilize mocked_read.read_data = ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you probably want this to be a yield fixture:
Not really, yield
one doesn't really work, it doesn't allow to set read_data
afterwards. Seems like it can be set only in constructor, so I keep it like it is.
Currently pre-commit mounts the current directory to /src and uses current directory name as mount base. However this does not work when pre-commit is run inside the container on some mounted path already, because mount points are relative to the host, not to the container. Fixes pre-commit#1387
@asottile thanks for merging. Any plans when to expect new release? |
no, it's also kinda rude to ask -- the release will happen when it happens |
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.
Fixes #1387
Second try. Sorry, no way I can not copy-paste
in_docker
one, it looks too straightforward and quite pythonic already IMO. But the rest was indeed not really optimal, rewrote completely.