diff --git a/cibuildwheel/docker_container.py b/cibuildwheel/docker_container.py index ecf9c52f..8c3a608b 100644 --- a/cibuildwheel/docker_container.py +++ b/cibuildwheel/docker_container.py @@ -27,12 +27,14 @@ class DockerContainer: bash_stdin: IO[bytes] bash_stdout: IO[bytes] - def __init__(self, docker_image: str, simulate_32_bit=False): + def __init__(self, docker_image: str, simulate_32_bit: bool = False, cwd: Optional[Union[str, PathLike]] = None): self.docker_image = docker_image self.simulate_32_bit = simulate_32_bit + self.cwd = cwd def __enter__(self) -> 'DockerContainer': self.name = f'cibuildwheel-{uuid.uuid4()}' + cwd_args = ['-w', str(self.cwd)] if self.cwd else [] shell_args = ['linux32', '/bin/bash'] if self.simulate_32_bit else ['/bin/bash'] subprocess.run( [ @@ -41,6 +43,7 @@ class DockerContainer: '--name', self.name, '-i', '-v', '/:/host', # ignored on CircleCI + *cwd_args, self.docker_image, *shell_args ], diff --git a/unit_test/docker_container_test.py b/unit_test/docker_container_test.py index a3b6f06d..22771ed2 100644 --- a/unit_test/docker_container_test.py +++ b/unit_test/docker_container_test.py @@ -41,6 +41,13 @@ def test_environment(): assert container.call(['sh', '-c', 'echo $TEST_VAR'], env={'TEST_VAR': '1'}, capture_output=True) == '1\n' +@pytest.mark.docker +def test_cwd(): + with DockerContainer(DEFAULT_IMAGE, cwd='/cibuildwheel/working_directory') as container: + assert container.call(['pwd'], capture_output=True) == '/cibuildwheel/working_directory\n' + assert container.call(['pwd'], capture_output=True, cwd='/opt') == '/opt\n' + + @pytest.mark.docker def test_container_removed(): with DockerContainer(DEFAULT_IMAGE) as container: