|
| 1 | +import re |
| 2 | +import subprocess |
| 3 | + |
| 4 | + |
| 5 | +def test_showref_list(repo_init_with_commit, git2cpp_path, tmp_path): |
| 6 | + """`show-ref` lists the repository references (heads present after init+commit).""" |
| 7 | + cmd = [git2cpp_path, "show-ref"] |
| 8 | + p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) |
| 9 | + assert p.returncode == 0 |
| 10 | + # repo_init_with_commit in conftest creates the branch "main" |
| 11 | + assert "refs/heads/main" in p.stdout |
| 12 | + |
| 13 | + |
| 14 | +def test_showref_includes_tag(repo_init_with_commit, git2cpp_path, tmp_path): |
| 15 | + """A created tag appears in show-ref output as refs/tags/<name>.""" |
| 16 | + # create a lightweight tag using the CLI under test |
| 17 | + subprocess.run([git2cpp_path, "tag", "v1.0"], cwd=tmp_path, check=True) |
| 18 | + |
| 19 | + p = subprocess.run([git2cpp_path, "show-ref"], capture_output=True, cwd=tmp_path, text=True) |
| 20 | + assert p.returncode == 0 |
| 21 | + assert "refs/tags/v1.0" in p.stdout |
| 22 | + |
| 23 | + |
| 24 | +def test_showref_line_format(repo_init_with_commit, git2cpp_path, tmp_path): |
| 25 | + """Each line of show-ref is: <40-hex-oid> <refname>.""" |
| 26 | + p = subprocess.run([git2cpp_path, "show-ref"], capture_output=True, cwd=tmp_path, text=True) |
| 27 | + assert p.returncode == 0 |
| 28 | + print(p.stdout) |
| 29 | + |
| 30 | + hex_re = re.compile(r"^[0-9a-f]{40}$") |
| 31 | + for line in p.stdout.splitlines(): |
| 32 | + line = line.strip() |
| 33 | + if not line: |
| 34 | + continue |
| 35 | + parts = line.split() |
| 36 | + # Expect at least two tokens: oid and refname |
| 37 | + assert len(parts) >= 2 |
| 38 | + oid, refname = parts[0], parts[1] |
| 39 | + assert hex_re.match(oid), f"OID not a 40-char hex: {oid!r}" |
| 40 | + assert refname.startswith("refs/"), f"Refname does not start with refs/: {refname!r}" |
| 41 | + |
| 42 | + |
| 43 | +def test_showref_nogit(git2cpp_path, tmp_path): |
| 44 | + """Running show-ref outside a repository returns an error and non-zero exit.""" |
| 45 | + cmd = [git2cpp_path, "show-ref"] |
| 46 | + p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) |
| 47 | + assert p.returncode != 0 |
| 48 | + assert "error: could not find repository at" in p.stderr |
0 commit comments