Skip to content

Commit

Permalink
test: fix mock of tempfile.TemporaryDirectory
Browse files Browse the repository at this point in the history
Since we use this as a context manager we need to go "deeper" to set the return
value on the correct attribute. The previous version fails on Python 3.13 as
Path objects can no longer be used as context managers.
  • Loading branch information
tigarmo committed Jan 30, 2025
1 parent 3666da2 commit c025e17
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions tests/unit/test_oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,52 @@
}


@pytest.fixture()
@pytest.fixture
def mock_run(mocker):
return mocker.patch("rockcraft.oci._process_run")


@pytest.fixture()
@pytest.fixture
def mock_archive_layer(mocker):
return mocker.patch("rockcraft.layers.archive_layer")


@pytest.fixture()
@pytest.fixture
def mock_rmtree(mocker):
return mocker.patch("shutil.rmtree")


@pytest.fixture()
@pytest.fixture
def mock_mkdir(mocker):
return mocker.patch("pathlib.Path.mkdir")


@pytest.fixture()
@pytest.fixture
def mock_mkdtemp(mocker):
return mocker.patch("tempfile.mkdtemp")


@pytest.fixture()
@pytest.fixture
def mock_tmpdir(mocker):
return mocker.patch("tempfile.TemporaryDirectory")


@pytest.fixture()
@pytest.fixture
def mock_inject_variant(mocker):
return mocker.patch("rockcraft.oci._inject_architecture_variant")


@pytest.fixture()
@pytest.fixture
def mock_read_bytes(mocker):
return mocker.patch("pathlib.Path.read_bytes")


@pytest.fixture()
@pytest.fixture
def mock_write_bytes(mocker):
return mocker.patch("pathlib.Path.write_bytes")


@pytest.fixture()
@pytest.fixture
def mock_add_layer(mocker):
return mocker.patch("rockcraft.oci.Image.add_layer")

Expand Down Expand Up @@ -313,7 +313,7 @@ def test_add_new_user(
tmp_path,
):
fake_tmpfs = tmp_path / "mock-tmp"
mock_tmpdir.return_value = fake_tmpfs
mock_tmpdir.return_value.__enter__.return_value = str(fake_tmpfs)

image = oci.Image("a:b", Path("/c"))
image.add_user(
Expand Down Expand Up @@ -457,7 +457,7 @@ def test_append_new_user(
(fake_prime_etc / f".wh.{filename}").touch()

fake_tmp_new_layer = tmp_path / "mock-tmp"
mock_tmpdir.return_value = fake_tmp_new_layer
mock_tmpdir.return_value.__enter__.return_value = str(fake_tmp_new_layer)

image = oci.Image("a:b", Path("/c"))
image.add_user(
Expand Down Expand Up @@ -746,7 +746,7 @@ def test_set_pebble_layer(
mock_define_pebble_layer.return_value = None

fake_tmpfs = tmp_path / "mock-tmp-pebble-layer-path"
mock_tmpdir.return_value = fake_tmpfs
mock_tmpdir.return_value.__enter__.return_value = str(fake_tmpfs)

image.set_pebble_layer(
mock_services,
Expand Down

0 comments on commit c025e17

Please sign in to comment.