|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from setuptools_scm.git_file_finder import find_files |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def inwd(wd): |
| 11 | + wd('git init') |
| 12 | + wd( 'git config user.email [email protected]') |
| 13 | + wd('git config user.name "a test"') |
| 14 | + wd.add_command = 'git add .' |
| 15 | + wd.commit_command = 'git commit -m test-{reason}' |
| 16 | + (wd.cwd / 'file1').ensure(file=True) |
| 17 | + adir = (wd.cwd / 'adir').ensure(dir=True) |
| 18 | + (adir / 'filea').ensure(file=True) |
| 19 | + bdir = (wd.cwd / 'bdir').ensure(dir=True) |
| 20 | + (bdir / 'fileb').ensure(file=True) |
| 21 | + wd.add_and_commit() |
| 22 | + with wd.cwd.as_cwd(): |
| 23 | + yield wd |
| 24 | + |
| 25 | + |
| 26 | +def _sep(paths): |
| 27 | + return { |
| 28 | + path.replace('/', os.path.sep) |
| 29 | + for path in paths |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +def test_basic(inwd): |
| 34 | + assert set(find_files()) == _sep({ |
| 35 | + 'file1', |
| 36 | + 'adir/filea', |
| 37 | + 'bdir/fileb', |
| 38 | + }) |
| 39 | + assert set(find_files('.')) == _sep({ |
| 40 | + './file1', |
| 41 | + './adir/filea', |
| 42 | + './bdir/fileb', |
| 43 | + }) |
| 44 | + assert set(find_files('adir')) == _sep({ |
| 45 | + 'adir/filea', |
| 46 | + }) |
| 47 | + |
| 48 | + |
| 49 | +def test_case(inwd): |
| 50 | + (inwd.cwd / 'CamelFile').ensure(file=True) |
| 51 | + (inwd.cwd / 'file2').ensure(file=True) |
| 52 | + inwd.add_and_commit() |
| 53 | + assert set(find_files()) == _sep({ |
| 54 | + 'CamelFile', |
| 55 | + 'file2', |
| 56 | + 'file1', |
| 57 | + 'adir/filea', |
| 58 | + 'bdir/fileb', |
| 59 | + }) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.skipif(sys.platform == 'win32', |
| 63 | + reason="symlinks to dir not supported") |
| 64 | +def test_symlink_dir(inwd): |
| 65 | + (inwd.cwd / 'adir' / 'bdirlink').mksymlinkto('../bdir') |
| 66 | + inwd.add_and_commit() |
| 67 | + assert set(find_files('adir')) == _sep({ |
| 68 | + 'adir/filea', |
| 69 | + 'adir/bdirlink/fileb', |
| 70 | + }) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.skipif(sys.platform == 'win32', |
| 74 | + reason="symlinks to files not supported on windows") |
| 75 | +def test_symlink_file(inwd): |
| 76 | + (inwd.cwd / 'adir' / 'file1link').mksymlinkto('../file1') |
| 77 | + inwd.add_and_commit() |
| 78 | + assert set(find_files('adir')) == _sep({ |
| 79 | + 'adir/filea', |
| 80 | + 'adir/file1link', |
| 81 | + }) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.skipif(sys.platform == 'win32', |
| 85 | + reason="symlinks to dir not supported") |
| 86 | +def test_symlink_loop(inwd): |
| 87 | + (inwd.cwd / 'adir' / 'loop').mksymlinkto('../adir') |
| 88 | + inwd.add_and_commit() |
| 89 | + assert set(find_files('adir')) == _sep({ |
| 90 | + 'adir/filea', |
| 91 | + }) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.skipif(sys.platform == 'win32', |
| 95 | + reason="symlinks to dir not supported") |
| 96 | +def test_symlink_dir_out_of_git(inwd): |
| 97 | + (inwd.cwd / 'adir' / 'outsidedirlink').\ |
| 98 | + mksymlinkto(os.path.join(__file__, '..')) |
| 99 | + inwd.add_and_commit() |
| 100 | + assert set(find_files('adir')) == _sep({ |
| 101 | + 'adir/filea', |
| 102 | + }) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.skipif(sys.platform == 'win32', |
| 106 | + reason="symlinks to files not supported on windows") |
| 107 | +def test_symlink_file_out_of_git(inwd): |
| 108 | + (inwd.cwd / 'adir' / 'outsidefilelink').mksymlinkto(__file__) |
| 109 | + inwd.add_and_commit() |
| 110 | + assert set(find_files('adir')) == _sep({ |
| 111 | + 'adir/filea', |
| 112 | + }) |
| 113 | + |
| 114 | + |
| 115 | +def test_empty_root(inwd): |
| 116 | + subdir = inwd.cwd / 'cdir' / 'subdir' |
| 117 | + subdir.ensure(dir=True) |
| 118 | + (subdir / 'filec').ensure(file=True) |
| 119 | + inwd.add_and_commit() |
| 120 | + assert set(find_files('cdir')) == _sep({ |
| 121 | + 'cdir/subdir/filec', |
| 122 | + }) |
| 123 | + |
| 124 | + |
| 125 | +def test_empty_subdir(inwd): |
| 126 | + subdir = inwd.cwd / 'adir' / 'emptysubdir' / 'subdir' |
| 127 | + subdir.ensure(dir=True) |
| 128 | + (subdir / 'xfile').ensure(file=True) |
| 129 | + inwd.add_and_commit() |
| 130 | + assert set(find_files('adir')) == _sep({ |
| 131 | + 'adir/filea', |
| 132 | + 'adir/emptysubdir/subdir/xfile', |
| 133 | + }) |
0 commit comments