From ad5ae32cc404b3f4a2915ceb094db9827b754652 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Fri, 13 May 2016 22:06:38 +0200 Subject: [PATCH] testsuite/key: Use pytest built-in fixture --- borg/testsuite/key.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/borg/testsuite/key.py b/borg/testsuite/key.py index 360b88a0c..8a1642a1d 100644 --- a/borg/testsuite/key.py +++ b/borg/testsuite/key.py @@ -38,15 +38,10 @@ class MockArgs: """)) keyfile2_id = unhexlify('c3fbf14bc001ebcc3cd86e696c13482ed071740927cd7cbe1b01b4bfcee49314') - @pytest.fixture(autouse=True) - def keys_dir(self, request, monkeypatch): - tmp_keys_dir = tempfile.mkdtemp() - - def finalize(): - shutil.rmtree(tmp_keys_dir) - - request.addfinalizer(finalize) - monkeypatch.setenv('BORG_KEYS_DIR', tmp_keys_dir) + @pytest.fixture + def keys_dir(self, request, monkeypatch, tmpdir): + monkeypatch.setenv('BORG_KEYS_DIR', tmpdir) + return tmpdir class MockRepository: class _Location: @@ -62,7 +57,7 @@ def test_plaintext(self): assert hexlify(key.id_hash(chunk.data)) == b'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' assert chunk == key.decrypt(key.id_hash(chunk.data), key.encrypt(chunk)) - def test_keyfile(self, monkeypatch): + def test_keyfile(self, monkeypatch, keys_dir): monkeypatch.setenv('BORG_PASSPHRASE', 'test') key = KeyfileKey.create(self.MockRepository(), self.MockArgs()) assert bytes_to_long(key.enc_cipher.iv, 8) == 0 @@ -97,8 +92,8 @@ def test_keyfile_kfenv(self, tmpdir, monkeypatch): with pytest.raises(FileNotFoundError): KeyfileKey.detect(self.MockRepository(), chunk_cdata) - def test_keyfile2(self, monkeypatch): - with open(os.path.join(os.environ['BORG_KEYS_DIR'], 'keyfile'), 'w') as fd: + def test_keyfile2(self, monkeypatch, keys_dir): + with keys_dir.join('keyfile').open('w') as fd: fd.write(self.keyfile2_key_file) monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase') key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata) @@ -144,8 +139,8 @@ def _corrupt_byte(self, key, data, offset): with pytest.raises(IntegrityError): key.decrypt("", data) - def test_decrypt_integrity(self, monkeypatch): - with open(os.path.join(os.environ['BORG_KEYS_DIR'], 'keyfile'), 'w') as fd: + def test_decrypt_integrity(self, monkeypatch, keys_dir): + with keys_dir.join('keyfile').open('w') as fd: fd.write(self.keyfile2_key_file) monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase') key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata)