Skip to content

Use of pytest fixture tmp_path in all tests #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ def git2cpp_path():
return Path(__file__).parent.parent / 'build' / 'git2cpp'

@pytest.fixture
def xtl_clone(git2cpp_path):
def xtl_clone(git2cpp_path, tmp_path, run_in_tmp_path):
url = 'https://github.com/xtensor-stack/xtl.git'
clone_working_dir = 'test/data'

clone_cmd = [git2cpp_path, 'clone', url]
subprocess.run(clone_cmd, capture_output=True, cwd = clone_working_dir, text=True)

yield

cleanup_cmd = ['rm', '-rf', 'xtl']
subprocess.run(cleanup_cmd, capture_output=True, cwd = clone_working_dir, text=True)
subprocess.run(clone_cmd, capture_output=True, cwd = tmp_path, text=True)

@pytest.fixture
def git_config(monkeypatch):
Expand Down
25 changes: 10 additions & 15 deletions test/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
import pytest


working_dir = 'test/data/xtl'

@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"])
def test_add(xtl_clone, git2cpp_path, all_flag):
with open("./test/data/xtl/mook_file.txt", "x"):
pass
def test_add(xtl_clone, git2cpp_path, tmp_path, all_flag):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

p = xtl_path / "mook_file.txt"
p.write_text('')

with open("./test/data/xtl/mook_file_2.txt", "x"):
pass
p2 = xtl_path / "mook_file_2.txt"
p2.write_text('')

cmd_add = [git2cpp_path, 'add']
if all_flag != "":
cmd_add.append(all_flag)
else:
cmd_add.append("mook_file.txt")
p_add = subprocess.run(cmd_add, cwd=working_dir, text=True)
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
assert p_add.returncode == 0

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

assert "Changes to be committed" in p_status.stdout
Expand All @@ -32,9 +33,3 @@ def test_add(xtl_clone, git2cpp_path, all_flag):
assert "Untracked files" not in p_status.stdout
else:
assert "Untracked files" in p_status.stdout

os.remove("./test/data/xtl/mook_file.txt")
os.remove("./test/data/xtl/mook_file_2.txt")

# undo the add, to leave the test directory at the end the same as it was at the start
subprocess.run(cmd_add, cwd=working_dir, capture_output=True, text=True)
23 changes: 15 additions & 8 deletions test/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@
import pytest


working_dir = 'test/data/xtl'
def test_branch_list(xtl_clone, git2cpp_path, tmp_path):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

def test_branch_list(xtl_clone, git2cpp_path):
cmd = [git2cpp_path, 'branch']
p = subprocess.run(cmd, capture_output=True, cwd=working_dir, text=True)
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)
assert p.returncode == 0
assert(p.stdout == '* master\n')


def test_branch_create_delete(xtl_clone, git2cpp_path):
def test_branch_create_delete(xtl_clone, git2cpp_path, tmp_path):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

create_cmd = [git2cpp_path, 'branch', 'foregone']
p_create = subprocess.run(create_cmd, capture_output=True, cwd=working_dir, text=True)
p_create = subprocess.run(create_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_create.returncode == 0

list_cmd = [git2cpp_path, 'branch']
p_list = subprocess.run(list_cmd, capture_output=True, cwd=working_dir, text=True)
p_list = subprocess.run(list_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_list.returncode == 0
assert(p_list.stdout == ' foregone\n* master\n')

del_cmd = [git2cpp_path, 'branch', '-d', 'foregone']
subprocess.run(del_cmd, capture_output=True, cwd=working_dir, text=True)
p_list2 = subprocess.run(list_cmd, capture_output=True, cwd=working_dir, text=True)
p_del = subprocess.run(del_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_del.returncode == 0

p_list2 = subprocess.run(list_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_list2.returncode == 0
assert(p_list2.stdout == '* master\n')
32 changes: 16 additions & 16 deletions test/test_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
import pytest


working_dir = 'test/data/xtl'
def test_checkout(xtl_clone, git2cpp_path, tmp_path):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

def test_checkout(xtl_clone, git2cpp_path):
create_cmd = [git2cpp_path, 'branch', 'foregone']
p_create = subprocess.run(create_cmd, capture_output=True, cwd=working_dir, text=True)
p_create = subprocess.run(create_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_create.returncode == 0

checkout_cmd = [git2cpp_path, 'checkout', 'foregone']
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_checkout.returncode == 0
assert(p_checkout.stdout == '');

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

checkout_cmd[2] = 'master'
p_checkout2 = subprocess.run(checkout_cmd, capture_output=True, cwd=working_dir, text=True)
p_checkout2 = subprocess.run(checkout_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_checkout2.returncode == 0

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

def test_checkout_b(xtl_clone, git2cpp_path, tmp_path):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

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

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

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

del_cmd = [git2cpp_path, 'branch', '-d', 'foregone']
p_del = subprocess.run(del_cmd, cwd=working_dir, text=True)
assert p_del.returncode == 0
p_branch2 = subprocess.run(branch_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_branch2.returncode == 0
assert(p_branch2.stdout == ' foregone\n* master\n')
13 changes: 4 additions & 9 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
import pytest


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

clone_cmd = [git2cpp_path, 'clone', url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd = working_dir, text=True)
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd = tmp_path, text=True)
assert p_clone.returncode == 0

assert os.path.exists(working_dir + '/xtl')
assert os.path.exists(working_dir + '/xtl/include')

cleanup_cmd = ['rm', '-rf', 'xtl']
p_cleanup = subprocess.run(cleanup_cmd, capture_output=True, cwd = working_dir, text=True)
assert p_cleanup.returncode == 0
assert os.path.exists(os.path.join(tmp_path, 'xtl'))
assert os.path.exists(os.path.join(tmp_path, 'xtl/include'))
20 changes: 11 additions & 9 deletions test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@
import pytest


working_dir = 'test/data/xtl'

@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"])
def test_commit(xtl_clone, git_config, git2cpp_path, monkeypatch, all_flag):
with open("./test/data/xtl/mook_file.txt", "x"):
pass
def test_commit(xtl_clone, git_config, git2cpp_path, tmp_path, monkeypatch, all_flag):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

p = xtl_path / "mook_file.txt"
p.write_text('')

cmd_add = [git2cpp_path, 'add', "mook_file.txt"]
p_add = subprocess.run(cmd_add, cwd=working_dir, text=True)
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
assert p_add.returncode == 0

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

assert "Changes to be committed" in p_status.stdout
assert "new file" in p_status.stdout

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

cmd_status_2 = [git2cpp_path, 'status', "--long"]
p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd=working_dir, text=True)
p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd=xtl_path, text=True)
assert p_status_2.returncode == 0
assert "mook_file" not in p_status_2.stdout
52 changes: 28 additions & 24 deletions test/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
import pytest


working_dir = 'test/data/xtl'

@pytest.mark.parametrize("short_flag", ["", "-s", "--short"])
@pytest.mark.parametrize("long_flag", ["", "--long"])
def test_status_new_file(xtl_clone, git2cpp_path, short_flag, long_flag):
with open("./test/data/xtl/mook_file.txt", "x"): # Untracked files
pass
def test_status_new_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_flag):
assert (tmp_path / "xtl").exists()
xtl_path = tmp_path / "xtl"

p = xtl_path / "mook_file.txt" # Untracked files
p.write_text('')

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

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

cmd = [git2cpp_path, 'status']
if short_flag != "":
cmd.append(short_flag)
if long_flag != "":
cmd.append(long_flag)
p = subprocess.run(cmd, capture_output=True, cwd=working_dir, text=True)
assert p.returncode == 0
p = subprocess.run(cmd, capture_output=True, cwd=xtl_path, text=True)

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

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

os.remove("./test/data/xtl/README.md") # Changes to be committed / deleted
p = xtl_path / "mook_file.txt" # Changes to be committed / new file
p.write_text('')

os.remove(xtl_path / "README.md") # Changes to be committed / deleted

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

cmd_status = [git2cpp_path, 'status']
if short_flag != "":
cmd_status.append(short_flag)
if long_flag != "":
cmd_status.append(long_flag)
p = subprocess.run(cmd_status, capture_output=True, cwd=working_dir, text=True)
p_status = subprocess.run(cmd_status, capture_output=True, cwd=xtl_path, text=True)
assert p_status.returncode == 0

if (long_flag == "--long") or ((long_flag == "") & (short_flag == "")):
assert "Changes to be committed" in p.stdout
assert "Changes not staged for commit" not in p.stdout
assert "Untracked files" not in p.stdout
assert "new file" in p.stdout
assert "deleted" in p.stdout
assert "Changes to be committed" in p_status.stdout
assert "Changes not staged for commit" not in p_status.stdout
assert "Untracked files" not in p_status.stdout
assert "new file" in p_status.stdout
assert "deleted" in p_status.stdout

elif short_flag in ["-s", "--short"]:
assert "A " in p.stdout
assert "D " in p.stdout
assert "A " in p_status.stdout
assert "D " in p_status.stdout