|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import pytest |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +# Fixture to run test in current tmp_path |
| 8 | +@pytest.fixture |
| 9 | +def run_in_tmp_path(tmp_path): |
| 10 | + original_cwd = os.getcwd() |
| 11 | + os.chdir(tmp_path) |
| 12 | + yield |
| 13 | + os.chdir(original_cwd) |
| 14 | + |
| 15 | + |
| 16 | +def test_init_in_directory(tmp_path): |
| 17 | + # tmp_path exists and is empty. |
| 18 | + assert list(tmp_path.iterdir()) == [] |
| 19 | + |
| 20 | + cmd = ['/Users/iant/github/git2cpp/build/git2cpp', 'init', '--bare', str(tmp_path)] |
| 21 | + p = subprocess.run(cmd, capture_output=True) |
| 22 | + assert p.returncode == 0 |
| 23 | + assert p.stdout == b'' |
| 24 | + assert p.stderr == b'' |
| 25 | + |
| 26 | + assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [ |
| 27 | + 'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs' |
| 28 | + ] |
| 29 | + |
| 30 | + # TODO: check this is a valid git repo |
| 31 | + |
| 32 | + |
| 33 | +def test_init_in_cwd(tmp_path, run_in_tmp_path): |
| 34 | + # tmp_path exists and is empty. |
| 35 | + assert list(tmp_path.iterdir()) == [] |
| 36 | + assert Path.cwd() == tmp_path |
| 37 | + |
| 38 | + cmd = ['/Users/iant/github/git2cpp/build/git2cpp', 'init', '--bare'] |
| 39 | + p = subprocess.run(cmd, capture_output=True) |
| 40 | + assert p.returncode == 0 |
| 41 | + assert p.stdout == b'' |
| 42 | + assert p.stderr == b'' |
| 43 | + |
| 44 | + assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [ |
| 45 | + 'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs' |
| 46 | + ] |
| 47 | + |
| 48 | + # TODO: check this is a valid git repo |
| 49 | + |
| 50 | + |
| 51 | +# TODO: Test without bare flag. |
| 52 | + |
| 53 | + |
| 54 | +def test_error_on_unknown_option(): |
| 55 | + cmd = ['build/git2cpp', 'init', '--unknown'] |
| 56 | + p = subprocess.run(cmd, capture_output=True) |
| 57 | + assert p.returncode == 1 |
| 58 | + assert p.stdout == b'' |
| 59 | + assert p.stderr.startswith(b"The following argument was not expected: --unknown") |
| 60 | + |
| 61 | + |
| 62 | +def test_error_on_repeated_directory(): |
| 63 | + cmd = ['build/git2cpp', 'init', 'abc', 'def'] |
| 64 | + p = subprocess.run(cmd, capture_output=True) |
| 65 | + assert p.returncode == 1 |
| 66 | + assert p.stdout == b'' |
| 67 | + assert p.stderr.startswith(b"The following argument was not expected: def") |
0 commit comments