Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
118 lines (90 loc) · 3.71 KB
/
Copy pathconftest.py
File metadata and controls
118 lines (90 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import re
import subprocess
from pathlib import Path
import pytest
GIT2CPP_TEST_WASM = os.getenv("GIT2CPP_TEST_WASM") == "1"
if GIT2CPP_TEST_WASM:
from .conftest_wasm import *
# Fixture to run test in current tmp_path
@pytest.fixture
def run_in_tmp_path(tmp_path):
original_cwd = os.getcwd()
os.chdir(tmp_path)
yield
os.chdir(original_cwd)
@pytest.fixture(scope="session")
def git2cpp_path():
if GIT2CPP_TEST_WASM:
return "git2cpp"
else:
return Path(__file__).parent.parent / "build" / "git2cpp"
@pytest.fixture
def xtl_clone(git2cpp_path, tmp_path, run_in_tmp_path):
url = "https://github.com/xtensor-stack/xtl.git"
clone_cmd = [git2cpp_path, "clone", url]
p = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p.returncode == 0
@pytest.fixture
def commit_env_config(monkeypatch):
config = {
"GIT_AUTHOR_NAME": "Jane Doe",
"GIT_AUTHOR_EMAIL": "jane.doe@blabla.com",
"GIT_COMMITTER_NAME": "Jane Doe",
"GIT_COMMITTER_EMAIL": "jane.doe@blabla.com",
}
for key, value in config.items():
if GIT2CPP_TEST_WASM:
subprocess.run(["export", f"{key}='{value}'"], check=True)
else:
monkeypatch.setenv(key, value)
@pytest.fixture
def disable_credential_callback(monkeypatch):
if GIT2CPP_TEST_WASM:
subprocess.run(["export", "GIT_CREDENTIAL_CALLBACK=0"], check=True)
else:
monkeypatch.setenv("GIT_CREDENTIAL_CALLBACK", "0")
@pytest.fixture
def repo_init_with_commit(commit_env_config, git2cpp_path, tmp_path):
cmd_init = [git2cpp_path, "init", ".", "-b", "main"]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path, text=True)
assert p_init.returncode == 0
p = tmp_path / "initial.txt"
p.write_text("initial")
cmd_add = [git2cpp_path, "add", "initial.txt"]
p_add = subprocess.run(cmd_add, capture_output=True, cwd=tmp_path, text=True)
assert p_add.returncode == 0
cmd_commit = [git2cpp_path, "commit", "-m", "Initial commit"]
p_commit = subprocess.run(cmd_commit, capture_output=True, cwd=tmp_path, text=True)
assert p_commit.returncode == 0
@pytest.fixture(scope="session")
def private_test_repo():
# Fixture containing everything needed to access private github repo.
# GIT2CPP_TEST_PRIVATE_TOKEN is the fine-grained Personal Access Token for private test repo.
# If this is not available as an environment variable, tests that use this fixture are skipped.
token = os.getenv("GIT2CPP_TEST_PRIVATE_TOKEN")
if token is None or len(token) == 0:
pytest.skip("No token for private test repo GIT2CPP_TEST_PRIVATE_TOKEN")
repo_name = "git2cpp-test-private"
org_name = "QuantStack"
return {
"repo_name": repo_name,
"http_url": f"http://github.com/{org_name}/{repo_name}",
"https_url": f"https://github.com/{org_name}/{repo_name}",
"token": token,
}
# Functions not fixtures below here.
def get_filemode_lines(git2cpp_path, cwd):
# Return all core.filemode lines from config list.
cmd_list = [git2cpp_path, "config", "list"]
p_list = subprocess.run(cmd_list, capture_output=True, cwd=cwd, text=True)
assert p_list.returncode == 0
return [line for line in p_list.stdout.splitlines() if line.startswith("core.filemode")]
def set_filemode(git2cpp_path, cwd, value):
# Set core.filemode in the repository's config file.
cmd_set = [git2cpp_path, "config", "set", "core.filemode", value]
p_set = subprocess.run(cmd_set, capture_output=True, cwd=cwd, text=True)
assert p_set.returncode == 0
def strip_ansi_colours(text):
# Strip ansi colour code sequences from a string.
return re.sub(r"\x1b\[[^m]*m", "", text)