Skip to content

Commit bbf967b

Browse files
SandrinePianthomas23
authored andcommitted
Run all tests in wasm except for credentials tests
1 parent 1ed7644 commit bbf967b

File tree

7 files changed

+82
-19
lines changed

7 files changed

+82
-19
lines changed

test/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import subprocess
34
from pathlib import Path
45

@@ -73,6 +74,10 @@ def private_test_repo():
7374
# Fixture containing everything needed to access private github repo.
7475
# GIT2CPP_TEST_PRIVATE_TOKEN is the fine-grained Personal Access Token for private test repo.
7576
# If this is not available as an environment variable, tests that use this fixture are skipped.
77+
78+
if GIT2CPP_TEST_WASM:
79+
pytest.skip("Use of credentials in wasm not yet implemented")
80+
7681
token = os.getenv("GIT2CPP_TEST_PRIVATE_TOKEN")
7782
if token is None or len(token) == 0:
7883
pytest.skip("No token for private test repo GIT2CPP_TEST_PRIVATE_TOKEN")
@@ -84,3 +89,11 @@ def private_test_repo():
8489
"https_url": f"https://github.com/{org_name}/{repo_name}",
8590
"token": token,
8691
}
92+
93+
94+
# Functions not fixtures below here.
95+
96+
97+
def strip_ansi_colours(text):
98+
# Strip ansi colour code sequences from a string.
99+
return re.sub(r"\x1b\[[^m]*m", "", text)

test/conftest_wasm.py

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ def pytest_ignore_collect(collection_path: pathlib.Path) -> bool:
1919
"test_clone.py",
2020
"test_commit.py",
2121
"test_config.py",
22+
"test_diff.py",
23+
"test_fetch.py",
2224
"test_fixtures.py",
2325
"test_git.py",
2426
"test_init.py",
2527
"test_log.py",
2628
"test_merge.py",
2729
"test_mv.py",
30+
"test_push.py",
2831
"test_rebase.py",
2932
"test_remote.py",
3033
"test_reset.py",
@@ -33,6 +36,7 @@ def pytest_ignore_collect(collection_path: pathlib.Path) -> bool:
3336
"test_rm.py",
3437
"test_stash.py",
3538
"test_status.py",
39+
"test_tag.py",
3640
]
3741

3842

@@ -54,18 +58,6 @@ def load_page(page: Page):
5458
page.locator("#loaded").wait_for()
5559

5660

57-
def os_chdir(dir: str):
58-
subprocess.run(["cd", str(dir)], capture_output=True, check=True, text=True)
59-
60-
61-
def os_getcwd():
62-
return subprocess.run(["pwd"], capture_output=True, check=True, text=True).stdout.strip()
63-
64-
65-
def os_remove(file: str):
66-
return subprocess.run(["rm", str(file)], capture_output=True, check=True, text=True)
67-
68-
6961
class MockPath(pathlib.Path):
7062
def __init__(self, path: str = ""):
7163
super().__init__(path)
@@ -95,26 +87,62 @@ def mkdir(self, *, parents=False):
9587
args.append("-p")
9688
subprocess.run(["mkdir"] + args, capture_output=True, text=True, check=True)
9789

90+
def read_bytes(self) -> bytes:
91+
raise RuntimeError("Not implemented")
92+
9893
def read_text(self) -> str:
9994
p = subprocess.run(["cat", str(self)], capture_output=True, text=True, check=True)
10095
text = p.stdout
10196
if text.endswith("\n"):
10297
text = text[:-1]
10398
return text
10499

100+
def write_bytes(self, data: bytes):
101+
# Convert binary data to a string where each element is backslash-escaped so that we can
102+
# write to file in cockle using `echo -e <backslash-escaped data>`.
103+
encoded_string = "".join(map(lambda d: f"\\x{d:02x}", data))
104+
cmd = ["echo", "-e", encoded_string, ">", str(self)]
105+
subprocess.run(cmd, capture_output=True, text=True, check=True)
106+
return len(data)
107+
105108
def write_text(self, data: str):
106109
# Note that in general it is not valid to direct output of a subprocess.run call to a file,
107110
# but we get away with it here as the command arguments are passed straight through to
108111
# cockle without being checked.
109-
p = subprocess.run(["echo", data, ">", str(self)], capture_output=True, text=True)
110-
assert p.returncode == 0
112+
if data.endswith("\n"):
113+
data = data[:-1]
114+
cmd = ["echo", data, ">", str(self)]
115+
subprocess.run(cmd, capture_output=True, text=True, check=True)
116+
return len(data)
111117

112118
def __truediv__(self, other):
113119
if isinstance(other, str):
114120
return MockPath(f"{self}/{other}")
115121
raise RuntimeError("MockPath.__truediv__ only supports strings")
116122

117123

124+
def os_chdir(dir: str):
125+
subprocess.run(["cd", str(dir)], capture_output=True, check=True, text=True)
126+
127+
128+
def os_getcwd():
129+
return subprocess.run(["pwd"], capture_output=True, check=True, text=True).stdout.strip()
130+
131+
132+
def os_remove(file: str):
133+
return subprocess.run(["rm", str(file)], capture_output=True, check=True, text=True)
134+
135+
136+
def os_rename(src: str | MockPath, dst: str | MockPath):
137+
return subprocess.run(["mv", str(src), str(dst)], capture_output=True, check=True, text=True)
138+
139+
140+
def os_symlink(src: str | MockPath, dst: str | MockPath):
141+
return subprocess.run(
142+
["ln", "-s", str(src), str(dst)], capture_output=True, check=True, text=True
143+
)
144+
145+
118146
def subprocess_run(
119147
page: Page,
120148
cmd: list[str],
@@ -192,3 +220,5 @@ def mock_subprocess_run(page: Page, monkeypatch):
192220
monkeypatch.setattr(os, "chdir", os_chdir)
193221
monkeypatch.setattr(os, "getcwd", os_getcwd)
194222
monkeypatch.setattr(os, "remove", os_remove)
223+
monkeypatch.setattr(os, "rename", os_rename)
224+
monkeypatch.setattr(os, "symlink", os_symlink)

test/test_checkout.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pytest
44

5+
from .conftest import strip_ansi_colours
6+
57

68
def test_checkout(repo_init_with_commit, git2cpp_path, tmp_path):
79
assert (tmp_path / "initial.txt").exists()
@@ -103,6 +105,7 @@ def test_checkout_with_unstaged_changes(repo_init_with_commit, git2cpp_path, tmp
103105

104106
# Should succeed and show status
105107
assert p_checkout.returncode == 0
108+
p_checkout.stdout = strip_ansi_colours(p_checkout.stdout)
106109
assert " M initial.txt" in p_checkout.stdout
107110
assert "Switched to branch 'newbranch'" in p_checkout.stdout
108111

test/test_diff.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
from .conftest import strip_ansi_colours
7+
68

79
def test_diff_nogit(git2cpp_path, tmp_path):
810
cmd = [git2cpp_path, "diff"]
@@ -84,6 +86,8 @@ def test_diff_stat(repo_init_with_commit, git2cpp_path, tmp_path):
8486
cmd = [git2cpp_path, "diff", "--stat"]
8587
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
8688
assert p.returncode == 0
89+
90+
p.stdout = strip_ansi_colours(p.stdout)
8791
assert "initial.txt" in p.stdout
8892
assert "1 file changed, 1 insertion(+)" in p.stdout
8993
assert "Modified content" not in p.stdout
@@ -132,6 +136,7 @@ def test_diff_summary(repo_init_with_commit, git2cpp_path, tmp_path):
132136
cmd = [git2cpp_path, "diff", "--cached", "--summary"]
133137
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
134138
assert p.returncode == 0
139+
p.stdout = strip_ansi_colours(p.stdout)
135140
assert "newfile.txt" in p.stdout
136141
assert "+" not in p.stdout
137142

@@ -146,6 +151,7 @@ def test_diff_name_only(repo_init_with_commit, git2cpp_path, tmp_path):
146151
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
147152

148153
assert p.returncode == 0
154+
p.stdout = strip_ansi_colours(p.stdout)
149155
assert p.stdout == "initial.txt\n"
150156
assert "+" not in p.stdout
151157

@@ -159,6 +165,7 @@ def test_diff_name_status(repo_init_with_commit, git2cpp_path, tmp_path):
159165
cmd = [git2cpp_path, "diff", "--name-status"]
160166
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
161167
assert p.returncode == 0
168+
p.stdout = strip_ansi_colours(p.stdout)
162169
assert p.stdout == "M\tinitial.txt\n"
163170

164171

@@ -172,6 +179,7 @@ def test_diff_raw(repo_init_with_commit, git2cpp_path, tmp_path):
172179
cmd = [git2cpp_path, "diff", "--raw"]
173180
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
174181
assert p.returncode == 0
182+
p.stdout = strip_ansi_colours(p.stdout)
175183
assert "M\tinitial.txt" in p.stdout
176184
assert bool(re.search(":[0-9]*", p.stdout))
177185

@@ -635,7 +643,6 @@ def test_diff_find_copies_harder(
635643
[git2cpp_path, "commit", "-m", "add original file"],
636644
cwd=tmp_path,
637645
check=True,
638-
env=commit_env_config,
639646
)
640647

641648
# Create identical copy
@@ -669,7 +676,6 @@ def test_diff_find_copies_with_threshold(
669676
[git2cpp_path, "commit", "-m", "add original file"],
670677
cwd=tmp_path,
671678
check=True,
672-
env=commit_env_config,
673679
)
674680

675681
# Create a partial copy (60% similar)

test/test_log.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
from .conftest import strip_ansi_colours
7+
68

79
@pytest.mark.parametrize("format_flag", ["", "--format=full", "--format=fuller"])
810
def test_log(commit_env_config, git2cpp_path, tmp_path, format_flag):
@@ -101,7 +103,7 @@ def test_log_with_head_reference(repo_init_with_commit, commit_env_config, git2c
101103
assert p_log.returncode == 0
102104

103105
# Check that HEAD reference is shown
104-
assert "HEAD ->" in p_log.stdout
106+
assert "HEAD ->" in strip_ansi_colours(p_log.stdout)
105107
assert "master" in p_log.stdout or "main" in p_log.stdout
106108

107109

@@ -253,7 +255,7 @@ def test_log_commit_without_references(commit_env_config, git2cpp_path, tmp_path
253255
assert p_log.returncode == 0
254256

255257
# First commit line should have references
256-
lines = p_log.stdout.split("\n")
258+
lines = strip_ansi_colours(p_log.stdout).split("\n")
257259
first_commit_line = [line for line in lines if line.startswith("commit")][0]
258260
assert "(" in first_commit_line # Has references
259261

@@ -286,7 +288,7 @@ def test_log_abbrev_commit_flags(
286288
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
287289
assert p.returncode == 0
288290

289-
m = re.search(r"^commit\s+([0-9a-fA-F]+)", p.stdout, flags=re.MULTILINE)
291+
m = re.search(r"^commit\s+([0-9a-fA-F]+)", strip_ansi_colours(p.stdout), flags=re.MULTILINE)
290292
if abbrev_commit_flag in ["", "--no-abbrev-commit"]:
291293
assert len(m.group(1)) == 40
292294
else:

test/test_status.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
from .conftest import strip_ansi_colours
8+
79

810
@pytest.mark.parametrize("short_flag", ["", "-s", "--short"])
911
@pytest.mark.parametrize("long_flag", ["", "--long"])
@@ -185,6 +187,8 @@ def test_status_mixed_changes(repo_init_with_commit, git2cpp_path, tmp_path, sho
185187
p = subprocess.run(cmd_status, capture_output=True, cwd=tmp_path, text=True)
186188

187189
assert p.returncode == 0
190+
191+
p.stdout = strip_ansi_colours(p.stdout)
188192
if short_flag == "-s":
189193
assert "A staged.txt" in p.stdout
190194
assert "D to_delete.txt" in p.stdout

test/test_tag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pytest
44

5+
from .conftest import GIT2CPP_TEST_WASM
6+
57

68
def test_tag_list_empty(repo_init_with_commit, git2cpp_path, tmp_path):
79
"""Test listing tags when there are no tags."""
@@ -235,6 +237,9 @@ def test_tag_annotated_no_message(repo_init_with_commit, commit_env_config, git2
235237
commit_cmd = [git2cpp_path, "commit", "-m", "my specific commit message"]
236238
subprocess.run(commit_cmd, cwd=tmp_path, check=True)
237239

240+
if GIT2CPP_TEST_WASM:
241+
pytest.skip("Not possible to pass empty argument to wasm")
242+
238243
# Create tag with empty message (should create lightweight tag)
239244
create_cmd = [git2cpp_path, "tag", "-m", "", "v1.0.0"]
240245
subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, check=True)

0 commit comments

Comments
 (0)