Skip to content

Commit 775749b

Browse files
committed
add tmp_path
1 parent 857a898 commit 775749b

File tree

7 files changed

+68
-90
lines changed

7 files changed

+68
-90
lines changed

test/conftest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ 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)
23+
subprocess.run(clone_cmd, capture_output=True, cwd = tmp_path, text=True)
2624

25+
# remove what's following ?
2726
yield
2827

2928
cleanup_cmd = ['rm', '-rf', 'xtl']
30-
subprocess.run(cleanup_cmd, capture_output=True, cwd = clone_working_dir, text=True)
29+
subprocess.run(cleanup_cmd, capture_output=True, cwd = tmp_path, text=True)
3130

3231
@pytest.fixture
3332
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, run_in_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: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@
44
import pytest
55

66

7-
working_dir = 'test/data/xtl'
8-
9-
def test_branch_list(xtl_clone, git2cpp_path):
7+
def test_branch_list(xtl_clone, git2cpp_path, tmp_path, run_in_tmp_path):
108
cmd = [git2cpp_path, 'branch']
11-
p = subprocess.run(cmd, capture_output=True, cwd=working_dir, text=True)
9+
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
1210
assert p.returncode == 0
1311
assert(p.stdout == '* master\n')
1412

1513

16-
def test_branch_create_delete(xtl_clone, git2cpp_path):
14+
def test_branch_create_delete(xtl_clone, git2cpp_path, tmp_path, run_in_tmp_path):
1715
create_cmd = [git2cpp_path, 'branch', 'foregone']
18-
p_create = subprocess.run(create_cmd, capture_output=True, cwd=working_dir, text=True)
16+
p_create = subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, text=True)
1917
assert p_create.returncode == 0
18+
2019
list_cmd = [git2cpp_path, 'branch']
21-
p_list = subprocess.run(list_cmd, capture_output=True, cwd=working_dir, text=True)
20+
p_list = subprocess.run(list_cmd, capture_output=True, cwd=tmp_path, text=True)
2221
assert p_list.returncode == 0
2322
assert(p_list.stdout == ' foregone\n* master\n')
2423

2524
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)
25+
subprocess.run(del_cmd, capture_output=True, cwd=tmp_path, text=True)
26+
p_list2 = subprocess.run(list_cmd, capture_output=True, cwd=tmp_path, text=True)
2827
assert p_list2.returncode == 0
2928
assert(p_list2.stdout == '* master\n')

test/test_checkout.py

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

66

7-
working_dir = 'test/data/xtl'
8-
9-
def test_checkout(xtl_clone, git2cpp_path):
7+
def test_checkout(xtl_clone, git2cpp_path, tmp_path, run_in_tmp_path):
108
create_cmd = [git2cpp_path, 'branch', 'foregone']
11-
p_create = subprocess.run(create_cmd, capture_output=True, cwd=working_dir, text=True)
9+
p_create = subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, text=True)
1210
assert p_create.returncode == 0
1311

1412
checkout_cmd = [git2cpp_path, 'checkout', 'foregone']
15-
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
13+
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=tmp_path, text=True)
1614
assert p_checkout.returncode == 0
1715
assert(p_checkout.stdout == '');
1816

1917
branch_cmd = [git2cpp_path, 'branch']
20-
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=working_dir, text=True)
18+
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path, text=True)
2119
assert p_branch.returncode == 0
2220
assert(p_branch.stdout == '* foregone\n master\n')
2321

2422
checkout_cmd[2] = 'master'
25-
p_checkout2 = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
23+
p_checkout2 = subprocess.run(checkout_cmd, capture_output=True, cwd=tmp_path, text=True)
2624
assert p_checkout2.returncode == 0
2725

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
31-
3226

33-
def test_checkout_b(xtl_clone, git2cpp_path):
27+
def test_checkout_b(xtl_clone, git2cpp_path, tmp_path, run_in_tmp_path):
3428
checkout_cmd = [git2cpp_path, 'checkout', '-b', 'foregone']
35-
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
29+
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=tmp_path, text=True)
3630
assert p_checkout.returncode == 0
3731
assert(p_checkout.stdout == '');
3832

3933
branch_cmd = [git2cpp_path, 'branch']
40-
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=working_dir, text=True)
34+
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path, text=True)
4135
assert p_branch.returncode == 0
4236
assert(p_branch.stdout == '* foregone\n master\n')
4337

4438
checkout_cmd.remove('-b')
4539
checkout_cmd[2] = 'master'
46-
p_checkout2 = subprocess.run(checkout_cmd, cwd=working_dir, text=True)
40+
p_checkout2 = subprocess.run(checkout_cmd, cwd=tmp_path, text=True)
4741
assert p_checkout2.returncode == 0
48-
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

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: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +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_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, run_in_tmp_path, tmp_path_factory, monkeypatch, all_flag):
9+
f = tmp_path / "xtl/mook_file.txt"
10+
f.write_text('')
1311

1412
cmd_add = [git2cpp_path, 'add', "mook_file.txt"]
15-
p_add = subprocess.run(cmd_add, cwd=working_dir, text=True)
13+
p_add = subprocess.run(cmd_add, cwd=tmp_path, text=True)
1614
assert p_add.returncode == 0
1715

1816
cmd_status = [git2cpp_path, 'status', "--long"]
19-
p_status = subprocess.run(cmd_status, capture_output=True, cwd=working_dir, text=True)
17+
p_status = subprocess.run(cmd_status, capture_output=True, cwd=tmp_path, text=True)
2018
assert p_status.returncode == 0
19+
2120
assert "Changes to be committed" in p_status.stdout
2221
assert "new file" in p_status.stdout
2322

2423
cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"]
25-
p_commit = subprocess.run(cmd_commit, cwd=working_dir, text=True)
24+
p_commit = subprocess.run(cmd_commit, cwd=tmp_path, text=True)
2625
assert p_commit.returncode == 0
2726

2827
cmd_status_2 = [git2cpp_path, 'status', "--long"]
29-
p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd=working_dir, text=True)
28+
p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd=tmp_path, text=True)
3029
assert p_status_2.returncode == 0
3130
assert "mook_file" not in p_status_2.stdout

test/test_status.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
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, run_in_tmp_path, tmp_path_factory, short_flag, long_flag):
11+
f = tmp_path / "xtl/mook_file.txt" # Untracked files
12+
f.write_text('')
1513

16-
with open("./test/data/xtl/CMakeLists.txt", "a") as f: # Changes not staged for commit / modified
17-
f.write("blablabla")
14+
fw = tmp_path / "xtl/CMakeLists.txt" # Changes not staged for commit / modified
15+
fw.write("blablabla")
1816

19-
os.remove("./test/data/xtl/README.md") # Changes not staged for commit / deleted
17+
os.remove(tmp_path / "xtl/README.md") # Changes not staged for commit / deleted
2018

2119
cmd = [git2cpp_path, 'status']
2220
if short_flag != "":
2321
cmd.append(short_flag)
2422
if long_flag != "":
2523
cmd.append(long_flag)
26-
p = subprocess.run(cmd, capture_output=True, cwd=working_dir, text=True)
27-
assert p.returncode == 0
24+
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
2825

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

4239
@pytest.mark.parametrize("short_flag", ["", "-s", "--short"])
4340
@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
41+
def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, run_in_tmp_path, short_flag, long_flag):
42+
assert (tmp_path / "xtl").exists()
43+
xtl_path = tmp_path / "xtl"
44+
45+
p = xtl_path / "mook_file.txt" # Changes to be committed / new file
46+
p.write_text('')
4747

48-
os.remove("./test/data/xtl/README.md") # Changes to be committed / deleted
48+
os.remove(xtl_path / "README.md") # Changes to be committed / deleted
4949

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

5454
cmd_status = [git2cpp_path, 'status']
5555
if short_flag != "":
5656
cmd_status.append(short_flag)
5757
if long_flag != "":
5858
cmd_status.append(long_flag)
59-
p = subprocess.run(cmd_status, capture_output=True, cwd=working_dir, text=True)
59+
p_status = subprocess.run(cmd_status, capture_output=True, cwd=xtl_path, text=True)
60+
assert p_status.returncode == 0
6061

6162
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
63+
assert "Changes to be committed" in p_status.stdout
64+
assert "Changes not staged for commit" not in p_status.stdout
65+
assert "Untracked files" not in p_status.stdout
66+
assert "new file" in p_status.stdout
67+
assert "deleted" in p_status.stdout
6768

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

0 commit comments

Comments
 (0)