| 1 |
from contextlib import contextmanager |
| 2 |
import os |
| 3 |
from pathlib import Path |
| 4 |
from tempfile import TemporaryDirectory |
| 5 |
|
| 6 |
from flit import vcs |
| 7 |
|
| 8 |
@contextmanager |
| 9 |
def cwd(path): |
| 10 |
if isinstance(path, Path): |
| 11 |
path = str(path) |
| 12 |
old_wd = os.getcwd() |
| 13 |
os.chdir(path) |
| 14 |
try: |
| 15 |
yield |
| 16 |
finally: |
| 17 |
os.chdir(old_wd) |
| 18 |
|
| 19 |
def test_identify_git_parent(): |
| 20 |
with TemporaryDirectory() as td: |
| 21 |
td = Path(td) |
| 22 |
(td / '.git').mkdir() |
| 23 |
subdir = (td / 'subdir') |
| 24 |
subdir.mkdir() |
| 25 |
with cwd(subdir): |
| 26 |
assert vcs.identify_vcs(Path('.')).name == 'git' |
| 27 |
|
| 28 |
def test_identify_git_worktree(): |
| 29 |
# In a linked git worktree (or submodule) `.git` is a regular file |
| 30 |
# containing a `gitdir:` pointer rather than a directory. |
| 31 |
with TemporaryDirectory() as td: |
| 32 |
td = Path(td) |
| 33 |
(td / '.git').write_text('gitdir: /path/to/main/.git/worktrees/feature\n') |
| 34 |
subdir = (td / 'subdir') |
| 35 |
subdir.mkdir() |
| 36 |
with cwd(subdir): |
| 37 |
assert vcs.identify_vcs(Path('.')).name == 'git' |