Skip to content

Commit 9351a2f

Browse files
authored
Use of pytest fixture tmp_path in all tests (#35)
* add tmp_path * fix path issue * remove cleaning up in clone fixture * add assert to checkout test * small fix * remove -f * remove useless comment * remove useless run_in_tmp_path
1 parent 857a898 commit 9351a2f

File tree

7 files changed

+86
-90
lines changed

7 files changed

+86
-90
lines changed

test/conftest.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@ def git2cpp_path():
1717
return Path(__file__).parent.parent / 'build' / 'git2cpp'
1818

1919
@pytest.fixture
20-
def xtl_clone(git2cpp_path):
20+
def xtl_clone(git2cpp_path, tmp_path, run_in_tmp_path):
2121
url = 'https://github.com/xtensor-stack/xtl.git'
22-
clone_working_dir = 'test/data'
23-
2422
clone_cmd = [git2cpp_path, 'clone', url]
25-
subprocess.run(clone_cmd, capture_output=True, cwd = clone_working_dir, text=True)
26-
27-
yield
28-
29-
cleanup_cmd = ['rm', '-rf', 'xtl']
30-
subprocess.run(cleanup_cmd, capture_output=True, cwd = clone_working_dir, text=True)
23+
subprocess.run(clone_cmd, capture_output=True, cwd = tmp_path, text=True)
3124

3225
@pytest.fixture
3326
def git_config(monkeypatch):

test/test_add.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
import pytest
55

66

7-
working_dir = 'test/data/xtl'
8-
97
@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"])
10-
def test_add(xtl_clone, git2cpp_path, all_flag):
11-
with open("./test/data/xtl/mook_file.txt", "x"):
12-
pass
8+
def test_add(xtl_clone, git2cpp_path, tmp_path, all_flag):
9+
assert (tmp_path / "xtl").exists()
10+
xtl_path = tmp_path / "xtl"
11+
12+
p = xtl_path / "mook_file.txt"
13+
p.write_text('')
1314

14-
with open("./test/data/xtl/mook_file_2.txt", "x"):
15-
pass
15+
p2 = xtl_path / "mook_file_2.txt"
16+
p2.write_text('')
1617

1718
cmd_add = [git2cpp_path, 'add']
1819
if all_flag != "":
1920
cmd_add.append(all_flag)
2021
else:
2122
cmd_add.append("mook_file.txt")
22-
p_add = subprocess.run(cmd_add, cwd=working_dir, text=True)
23+
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
2324
assert p_add.returncode == 0
2425

2526
cmd_status = [git2cpp_path, 'status', "--long"]
26-
p_status = subprocess.run(cmd_status, cwd=working_dir, capture_output=True, text=True)
27+
p_status = subprocess.run(cmd_status, cwd=xtl_path, capture_output=True, text=True)
2728
assert p_status.returncode == 0
2829

2930
assert "Changes to be committed" in p_status.stdout
@@ -32,9 +33,3 @@ def test_add(xtl_clone, git2cpp_path, all_flag):
3233
assert "Untracked files" not in p_status.stdout
3334
else:
3435
assert "Untracked files" in p_status.stdout
35-
36-
os.remove("./test/data/xtl/mook_file.txt")
37-
os.remove("./test/data/xtl/mook_file_2.txt")
38-
39-
# undo the add, to leave the test directory at the end the same as it was at the start
40-
subprocess.run(cmd_add, cwd=working_dir, capture_output=True, text=True)

test/test_branch.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@
44
import pytest
55

66

7-
working_dir = 'test/data/xtl'
7+
def test_branch_list(xtl_clone, git2cpp_path, tmp_path):
8+
assert (tmp_path / "xtl").exists()
9+
xtl_path = tmp_path / "xtl"
810

9-
def test_branch_list(xtl_clone, git2cpp_path):
1011
cmd = [git2cpp_path, 'branch']
11-
p = subprocess.run(cmd, capture_output=True, cwd=working_dir, text=True)
12+
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)
1213
assert p.returncode == 0
1314
assert(p.stdout == '* master\n')
1415

1516

16-
def test_branch_create_delete(xtl_clone, git2cpp_path):
17+
def test_branch_create_delete(xtl_clone, git2cpp_path, tmp_path):
18+
assert (tmp_path / "xtl").exists()
19+
xtl_path = tmp_path / "xtl"
20+
1721
create_cmd = [git2cpp_path, 'branch', 'foregone']
18-
p_create = subprocess.run(create_cmd, capture_output=True, cwd=working_dir, text=True)
22+
p_create = subprocess.run(create_cmd, capture_output=True, cwd=xtl_path, text=True)
1923
assert p_create.returncode == 0
24+
2025
list_cmd = [git2cpp_path, 'branch']
21-
p_list = subprocess.run(list_cmd, capture_output=True, cwd=working_dir, text=True)
26+
p_list = subprocess.run(list_cmd, capture_output=True, cwd=xtl_path, text=True)
2227
assert p_list.returncode == 0
2328
assert(p_list.stdout == ' foregone\n* master\n')
2429

2530
del_cmd = [git2cpp_path, 'branch', '-d', 'foregone']
26-
subprocess.run(del_cmd, capture_output=True, cwd=working_dir, text=True)
27-
p_list2 = subprocess.run(list_cmd, capture_output=True, cwd=working_dir, text=True)
31+
p_del = subprocess.run(del_cmd, capture_output=True, cwd=xtl_path, text=True)
32+
assert p_del.returncode == 0
33+
34+
p_list2 = subprocess.run(list_cmd, capture_output=True, cwd=xtl_path, text=True)
2835
assert p_list2.returncode == 0
2936
assert(p_list2.stdout == '* master\n')

test/test_checkout.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,48 @@
44
import pytest
55

66

7-
working_dir = 'test/data/xtl'
7+
def test_checkout(xtl_clone, git2cpp_path, tmp_path):
8+
assert (tmp_path / "xtl").exists()
9+
xtl_path = tmp_path / "xtl"
810

9-
def test_checkout(xtl_clone, git2cpp_path):
1011
create_cmd = [git2cpp_path, 'branch', 'foregone']
11-
p_create = subprocess.run(create_cmd, capture_output=True, cwd=working_dir, text=True)
12+
p_create = subprocess.run(create_cmd, capture_output=True, cwd=xtl_path, text=True)
1213
assert p_create.returncode == 0
1314

1415
checkout_cmd = [git2cpp_path, 'checkout', 'foregone']
15-
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
16+
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=xtl_path, text=True)
1617
assert p_checkout.returncode == 0
1718
assert(p_checkout.stdout == '');
1819

1920
branch_cmd = [git2cpp_path, 'branch']
20-
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=working_dir, text=True)
21+
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=xtl_path, text=True)
2122
assert p_branch.returncode == 0
2223
assert(p_branch.stdout == '* foregone\n master\n')
2324

2425
checkout_cmd[2] = 'master'
25-
p_checkout2 = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
26+
p_checkout2 = subprocess.run(checkout_cmd, capture_output=True, cwd=xtl_path, text=True)
2627
assert p_checkout2.returncode == 0
2728

28-
del_cmd = [git2cpp_path, 'branch', '-d', 'foregone']
29-
p_del = subprocess.run(del_cmd, cwd=working_dir, text=True)
30-
assert p_del.returncode == 0
3129

30+
def test_checkout_b(xtl_clone, git2cpp_path, tmp_path):
31+
assert (tmp_path / "xtl").exists()
32+
xtl_path = tmp_path / "xtl"
3233

33-
def test_checkout_b(xtl_clone, git2cpp_path):
3434
checkout_cmd = [git2cpp_path, 'checkout', '-b', 'foregone']
35-
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
35+
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=xtl_path, text=True)
3636
assert p_checkout.returncode == 0
3737
assert(p_checkout.stdout == '');
3838

3939
branch_cmd = [git2cpp_path, 'branch']
40-
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=working_dir, text=True)
40+
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=xtl_path, text=True)
4141
assert p_branch.returncode == 0
4242
assert(p_branch.stdout == '* foregone\n master\n')
4343

4444
checkout_cmd.remove('-b')
4545
checkout_cmd[2] = 'master'
46-
p_checkout2 = subprocess.run(checkout_cmd, cwd=working_dir, text=True)
46+
p_checkout2 = subprocess.run(checkout_cmd, cwd=xtl_path, text=True)
4747
assert p_checkout2.returncode == 0
4848

49-
del_cmd = [git2cpp_path, 'branch', '-d', 'foregone']
50-
p_del = subprocess.run(del_cmd, cwd=working_dir, text=True)
51-
assert p_del.returncode == 0
49+
p_branch2 = subprocess.run(branch_cmd, capture_output=True, cwd=xtl_path, text=True)
50+
assert p_branch2.returncode == 0
51+
assert(p_branch2.stdout == ' foregone\n* master\n')

test/test_clone.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
import pytest
55

66

7-
def test_clone(git2cpp_path):
7+
def test_clone(git2cpp_path, tmp_path, run_in_tmp_path):
88
url = 'https://github.com/xtensor-stack/xtl.git'
9-
working_dir = 'test/data'
109

1110
clone_cmd = [git2cpp_path, 'clone', url]
12-
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd = working_dir, text=True)
11+
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd = tmp_path, text=True)
1312
assert p_clone.returncode == 0
1413

15-
assert os.path.exists(working_dir + '/xtl')
16-
assert os.path.exists(working_dir + '/xtl/include')
17-
18-
cleanup_cmd = ['rm', '-rf', 'xtl']
19-
p_cleanup = subprocess.run(cleanup_cmd, capture_output=True, cwd = working_dir, text=True)
20-
assert p_cleanup.returncode == 0
14+
assert os.path.exists(os.path.join(tmp_path, 'xtl'))
15+
assert os.path.exists(os.path.join(tmp_path, 'xtl/include'))

test/test_commit.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@
44
import pytest
55

66

7-
working_dir = 'test/data/xtl'
8-
97
@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"])
10-
def test_commit(xtl_clone, git_config, git2cpp_path, monkeypatch, all_flag):
11-
with open("./test/data/xtl/mook_file.txt", "x"):
12-
pass
8+
def test_commit(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, all_flag):
9+
assert (tmp_path / "xtl").exists()
10+
xtl_path = tmp_path / "xtl"
11+
12+
p = xtl_path / "mook_file.txt"
13+
p.write_text('')
1314

1415
cmd_add = [git2cpp_path, 'add', "mook_file.txt"]
15-
p_add = subprocess.run(cmd_add, cwd=working_dir, text=True)
16+
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
1617
assert p_add.returncode == 0
1718

1819
cmd_status = [git2cpp_path, 'status', "--long"]
19-
p_status = subprocess.run(cmd_status, capture_output=True, cwd=working_dir, text=True)
20+
p_status = subprocess.run(cmd_status, capture_output=True, cwd=xtl_path, text=True)
2021
assert p_status.returncode == 0
22+
2123
assert "Changes to be committed" in p_status.stdout
2224
assert "new file" in p_status.stdout
2325

2426
cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"]
25-
p_commit = subprocess.run(cmd_commit, cwd=working_dir, text=True)
27+
p_commit = subprocess.run(cmd_commit, cwd=xtl_path, text=True)
2628
assert p_commit.returncode == 0
2729

2830
cmd_status_2 = [git2cpp_path, 'status', "--long"]
29-
p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd=working_dir, text=True)
31+
p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd=xtl_path, text=True)
3032
assert p_status_2.returncode == 0
3133
assert "mook_file" not in p_status_2.stdout

test/test_status.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55
import pytest
66

77

8-
working_dir = 'test/data/xtl'
9-
108
@pytest.mark.parametrize("short_flag", ["", "-s", "--short"])
119
@pytest.mark.parametrize("long_flag", ["", "--long"])
12-
def test_status_new_file(xtl_clone, git2cpp_path, short_flag, long_flag):
13-
with open("./test/data/xtl/mook_file.txt", "x"): # Untracked files
14-
pass
10+
def test_status_new_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_flag):
11+
assert (tmp_path / "xtl").exists()
12+
xtl_path = tmp_path / "xtl"
13+
14+
p = xtl_path / "mook_file.txt" # Untracked files
15+
p.write_text('')
1516

16-
with open("./test/data/xtl/CMakeLists.txt", "a") as f: # Changes not staged for commit / modified
17-
f.write("blablabla")
17+
pw = xtl_path / "CMakeLists.txt" # Changes not staged for commit / modified
18+
pw.write_text("blablabla")
1819

19-
os.remove("./test/data/xtl/README.md") # Changes not staged for commit / deleted
20+
os.remove(xtl_path / "README.md") # Changes not staged for commit / deleted
2021

2122
cmd = [git2cpp_path, 'status']
2223
if short_flag != "":
2324
cmd.append(short_flag)
2425
if long_flag != "":
2526
cmd.append(long_flag)
26-
p = subprocess.run(cmd, capture_output=True, cwd=working_dir, text=True)
27-
assert p.returncode == 0
27+
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)
2828

2929
if (long_flag == "--long") or ((long_flag == "") & (short_flag == "")):
3030
assert "On branch master" in p.stdout
@@ -41,30 +41,34 @@ def test_status_new_file(xtl_clone, git2cpp_path, short_flag, long_flag):
4141

4242
@pytest.mark.parametrize("short_flag", ["", "-s", "--short"])
4343
@pytest.mark.parametrize("long_flag", ["", "--long"])
44-
def test_status_add_file(xtl_clone, git2cpp_path, short_flag, long_flag):
45-
with open("./test/data/xtl/mook_file.txt", "x"): # Changes to be committed / new file
46-
pass
44+
def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_flag):
45+
assert (tmp_path / "xtl").exists()
46+
xtl_path = tmp_path / "xtl"
4747

48-
os.remove("./test/data/xtl/README.md") # Changes to be committed / deleted
48+
p = xtl_path / "mook_file.txt" # Changes to be committed / new file
49+
p.write_text('')
50+
51+
os.remove(xtl_path / "README.md") # Changes to be committed / deleted
4952

5053
cmd_add = [git2cpp_path, 'add', "--all"]
51-
p = subprocess.run(cmd_add, cwd=working_dir, text=True)
52-
assert p.returncode == 0
54+
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
55+
assert p_add.returncode == 0
5356

5457
cmd_status = [git2cpp_path, 'status']
5558
if short_flag != "":
5659
cmd_status.append(short_flag)
5760
if long_flag != "":
5861
cmd_status.append(long_flag)
59-
p = subprocess.run(cmd_status, capture_output=True, cwd=working_dir, text=True)
62+
p_status = subprocess.run(cmd_status, capture_output=True, cwd=xtl_path, text=True)
63+
assert p_status.returncode == 0
6064

6165
if (long_flag == "--long") or ((long_flag == "") & (short_flag == "")):
62-
assert "Changes to be committed" in p.stdout
63-
assert "Changes not staged for commit" not in p.stdout
64-
assert "Untracked files" not in p.stdout
65-
assert "new file" in p.stdout
66-
assert "deleted" in p.stdout
66+
assert "Changes to be committed" in p_status.stdout
67+
assert "Changes not staged for commit" not in p_status.stdout
68+
assert "Untracked files" not in p_status.stdout
69+
assert "new file" in p_status.stdout
70+
assert "deleted" in p_status.stdout
6771

6872
elif short_flag in ["-s", "--short"]:
69-
assert "A " in p.stdout
70-
assert "D " in p.stdout
73+
assert "A " in p_status.stdout
74+
assert "D " in p_status.stdout

0 commit comments

Comments
 (0)