Skip to content

fix: fix path mounting when running in Docker#1888

Merged
asottile merged 1 commit intopre-commit:masterfrom
okainov:docker-fix-2
Apr 29, 2021
Merged

fix: fix path mounting when running in Docker#1888
asottile merged 1 commit intopre-commit:masterfrom
okainov:docker-fix-2

Conversation

@okainov
Copy link
Copy Markdown
Contributor

@okainov okainov commented Apr 21, 2021

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.

@okainov
Copy link
Copy Markdown
Contributor Author

okainov commented Apr 23, 2021

Name                             Stmts   Miss Branch BrPart  Cover   Missing
----------------------------------------------------------------------------
pre_commit/languages/docker.py      65      0     10      1    99%   40->36
----------------------------------------------------------------------------
TOTAL                             7089      0    714      1    99%

100 files skipped due to complete coverage.
Coverage failure: total of 100 is less than fail-under=100

I don't really understand why CI is not happy. All lines are test-covered. what is 40->36?

@asottile
Copy link
Copy Markdown
Member

Name                             Stmts   Miss Branch BrPart  Cover   Missing
----------------------------------------------------------------------------
pre_commit/languages/docker.py      65      0     10      1    99%   40->36
----------------------------------------------------------------------------
TOTAL                             7089      0    714      1    99%

100 files skipped due to complete coverage.
Coverage failure: total of 100 is less than fail-under=100

I don't really understand why CI is not happy. All lines are test-covered. what is 40->36?

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) else: pass

@okainov
Copy link
Copy Markdown
Contributor Author

okainov commented Apr 23, 2021

@asottile fixed, please review and merge if it's fine

assert docker.get_docker_user() == ()


class TestInDocker:
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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?


@mock.patch.object(docker, 'is_in_docker', return_value=True)
def test_in_docker_no_binds_same_path(self, _):
binds_list: List[str] = []
Copy link
Copy Markdown
Member

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 (?)

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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

assert docker.get_docker_user() == ()


class TestInDocker:
Copy link
Copy Markdown
Member

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


@mock.patch.object(docker, 'is_in_docker', return_value=True)
def test_in_docker_no_binds_same_path(self, _):
binds_list: List[str] = []
Copy link
Copy Markdown
Member

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


@pytest.fixture
def mock_file_fixture(self):
return lambda read_data: \
Copy link
Copy Markdown
Member

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 = ...

Copy link
Copy Markdown
Contributor Author

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 asottile merged commit 7f13fa5 into pre-commit:master Apr 29, 2021
@okainov
Copy link
Copy Markdown
Contributor Author

okainov commented Apr 30, 2021

@asottile thanks for merging. Any plans when to expect new release?

@asottile
Copy link
Copy Markdown
Member

no, it's also kinda rude to ask -- the release will happen when it happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Path is not mounted correctly when running Docker hooks from Docker

2 participants