fix: fix path mounting when running in Docker#1888
fix: fix path mounting when running in Docker#1888asottile merged 1 commit intopre-commit:masterfrom
Conversation
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
| assert docker.get_docker_user() == () | ||
|
|
||
|
|
||
| class TestInDocker: |
There was a problem hiding this comment.
we don't use test classes in this project
There was a problem hiding this comment.
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.
we don't use test classes in this project
There was a problem hiding this comment.
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.
if you inline this you shouldn't need the type signature (?)
There was a problem hiding this comment.
I got warning from CI about it
There was a problem hiding this comment.
I mean if you don't have this as a variable you shouldn't need the annotation
There was a problem hiding this comment.
I want to have consistent tests so they look alike
tests/languages/docker_test.py
Outdated
| assert docker.get_docker_user() == () | ||
|
|
||
|
|
||
| class TestInDocker: |
There was a problem hiding this comment.
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.
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.
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.
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_dockerone, it looks too straightforward and quite pythonic already IMO. But the rest was indeed not really optimal, rewrote completely.