Skip to content

Commit c2a3c98

Browse files
committed
tests: write test artifacts to tmp_path instead of project root
1 parent 3e131e9 commit c2a3c98

3 files changed

Lines changed: 43 additions & 42 deletions

File tree

test/test_archive.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333

3434

3535
def check_writing(
36-
repo: Repository, treeish: str | Tree | Oid | Object, timestamp: int | None = None
36+
repo: Repository,
37+
treeish: str | Tree | Oid | Object,
38+
tmp_path: Path,
39+
timestamp: int | None = None,
3740
) -> None:
38-
archive = tarfile.open('foo.tar', mode='w')
41+
archive_path = tmp_path / 'foo.tar'
42+
archive = tarfile.open(archive_path, mode='w')
3943
repo.write_archive(treeish, archive)
4044

4145
index = Index()
@@ -51,19 +55,17 @@ def check_writing(
5155
assert timestamp == fileinfo.mtime
5256

5357
archive.close()
54-
path = Path('foo.tar')
55-
assert path.is_file()
56-
path.unlink()
58+
assert archive_path.is_file()
5759

5860

59-
def test_write_tree(testrepo: Repository) -> None:
60-
check_writing(testrepo, TREE_HASH)
61-
check_writing(testrepo, Oid(hex=TREE_HASH))
62-
check_writing(testrepo, testrepo[TREE_HASH])
61+
def test_write_tree(testrepo: Repository, tmp_path: Path) -> None:
62+
check_writing(testrepo, TREE_HASH, tmp_path)
63+
check_writing(testrepo, Oid(hex=TREE_HASH), tmp_path)
64+
check_writing(testrepo, testrepo[TREE_HASH], tmp_path)
6365

6466

65-
def test_write_commit(testrepo: Repository) -> None:
67+
def test_write_commit(testrepo: Repository, tmp_path: Path) -> None:
6668
commit_timestamp = testrepo[COMMIT_HASH].committer.time
67-
check_writing(testrepo, COMMIT_HASH, commit_timestamp)
68-
check_writing(testrepo, Oid(hex=COMMIT_HASH), commit_timestamp)
69-
check_writing(testrepo, testrepo[COMMIT_HASH], commit_timestamp)
69+
check_writing(testrepo, COMMIT_HASH, tmp_path, commit_timestamp)
70+
check_writing(testrepo, Oid(hex=COMMIT_HASH), tmp_path, commit_timestamp)
71+
check_writing(testrepo, testrepo[COMMIT_HASH], tmp_path, commit_timestamp)

test/test_config.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@
3232

3333
from . import utils
3434

35-
CONFIG_FILENAME = 'test_config'
35+
36+
@pytest.fixture
37+
def config_path(tmp_path: Path) -> Path:
38+
return tmp_path / 'test_config'
3639

3740

3841
@pytest.fixture
3942
def config(testrepo: Repository) -> Generator[object, None, None]:
4043
yield testrepo.config
41-
try:
42-
Path(CONFIG_FILENAME).unlink()
43-
except OSError:
44-
pass
4544

4645

4746
def test_config(config: Config) -> None:
@@ -64,42 +63,42 @@ def test_system_config() -> None:
6463
pass
6564

6665

67-
def test_new() -> None:
66+
def test_new(config_path: Path) -> None:
6867
# Touch file
69-
open(CONFIG_FILENAME, 'w').close()
68+
config_path.touch()
7069

71-
config_write = Config(CONFIG_FILENAME)
70+
config_write = Config(str(config_path))
7271
assert config_write is not None
7372

7473
config_write['core.bare'] = False
7574
config_write['core.editor'] = 'ed'
7675

77-
config_read = Config(CONFIG_FILENAME)
76+
config_read = Config(str(config_path))
7877
assert 'core.bare' in config_read
7978
assert not config_read.get_bool('core.bare')
8079
assert 'core.editor' in config_read
8180
assert config_read['core.editor'] == 'ed'
8281

8382

84-
def test_add() -> None:
85-
with open(CONFIG_FILENAME, 'w') as new_file:
83+
def test_add(config_path: Path) -> None:
84+
with open(config_path, 'w') as new_file:
8685
new_file.write('[this]\n\tthat = true\n')
8786
new_file.write('[something "other"]\n\there = false')
8887

8988
config = Config()
90-
config.add_file(CONFIG_FILENAME, 0)
89+
config.add_file(config_path, 0)
9190
assert 'this.that' in config
9291
assert config.get_bool('this.that')
9392
assert 'something.other.here' in config
9493
assert not config.get_bool('something.other.here')
9594

9695

97-
def test_add_aspath() -> None:
98-
with open(CONFIG_FILENAME, 'w') as new_file:
96+
def test_add_aspath(config_path: Path) -> None:
97+
with open(config_path, 'w') as new_file:
9998
new_file.write('[this]\n\tthat = true\n')
10099

101100
config = Config()
102-
config.add_file(Path(CONFIG_FILENAME), 0)
101+
config.add_file(config_path, 0)
103102
assert 'this.that' in config
104103

105104

@@ -148,12 +147,12 @@ def test_write(config: Config) -> None:
148147
assert 'core.dummy3' not in config
149148

150149

151-
def test_multivar() -> None:
152-
with open(CONFIG_FILENAME, 'w') as new_file:
150+
def test_multivar(config_path: Path) -> None:
151+
with open(config_path, 'w') as new_file:
153152
new_file.write('[this]\n\tthat = foobar\n\tthat = foobeer\n')
154153

155154
config = Config()
156-
config.add_file(CONFIG_FILENAME, 6)
155+
config.add_file(config_path, 6)
157156
assert 'this.that' in config
158157

159158
assert ['foobar', 'foobeer'] == list(config.get_multivar('this.that'))
@@ -185,27 +184,27 @@ def test_iterator(config: Config) -> None:
185184
assert lst['core.bare']
186185

187186

188-
def test_valueless_key_iteration() -> None:
187+
def test_valueless_key_iteration(config_path: Path) -> None:
189188
# A valueless key (no `= value`) has a NULL value pointer in libgit2.
190189
# Iterating over such entries must not raise a RuntimeError.
191-
with open(CONFIG_FILENAME, 'w') as new_file:
190+
with open(config_path, 'w') as new_file:
192191
new_file.write('[section]\n\tvaluelesskey\n\tnormalkey = somevalue\n')
193192

194193
config = Config()
195-
config.add_file(CONFIG_FILENAME, 6)
194+
config.add_file(config_path, 6)
196195

197196
entries = {entry.name: entry for entry in config}
198197
assert 'section.valuelesskey' in entries
199198
assert 'section.normalkey' in entries
200199

201200

202-
def test_valueless_key_value() -> None:
201+
def test_valueless_key_value(config_path: Path) -> None:
203202
# A valueless key must expose value=None and raw_value=None.
204-
with open(CONFIG_FILENAME, 'w') as new_file:
203+
with open(config_path, 'w') as new_file:
205204
new_file.write('[section]\n\tvaluelesskey\n\tnormalkey = somevalue\n')
206205

207206
config = Config()
208-
config.add_file(CONFIG_FILENAME, 6)
207+
config.add_file(config_path, 6)
209208

210209
entries = {entry.name: entry for entry in config}
211210
assert entries['section.valuelesskey'].raw_value is None

test/test_nonunicode.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
"""Tests for non unicode byte strings"""
2727

28-
import os
2928
import shutil
3029
import sys
30+
from pathlib import Path
3131

3232
import pytest
3333

@@ -45,12 +45,12 @@
4545

4646
@utils.requires_network
4747
@works_in_linux
48-
def test_nonunicode_branchname(testrepo: Repository) -> None:
49-
folderpath = 'temp_repo_nonutf'
50-
if os.path.exists(folderpath):
48+
def test_nonunicode_branchname(testrepo: Repository, tmp_path: Path) -> None:
49+
folderpath = tmp_path / 'temp_repo_nonutf'
50+
if folderpath.exists():
5151
shutil.rmtree(folderpath)
5252
newrepo = pygit2.clone_repository(
53-
path=folderpath, url='https://github.com/pygit2/test_branch_notutf.git'
53+
path=str(folderpath), url='https://github.com/pygit2/test_branch_notutf.git'
5454
)
5555
bstring = b'\xc3master'
5656
assert bstring in [

0 commit comments

Comments
 (0)