| 1 |
import ast |
| 2 |
from os.path import join as pjoin |
| 3 |
from pathlib import Path |
| 4 |
import pytest |
| 5 |
from shutil import which, copytree |
| 6 |
from subprocess import run |
| 7 |
import sys |
| 8 |
import tarfile |
| 9 |
from tempfile import TemporaryDirectory |
| 10 |
from testpath import assert_isfile, MockCommand |
| 11 |
|
| 12 |
from flit import sdist, common |
| 13 |
|
| 14 |
samples_dir = Path(__file__).parent / 'samples' |
| 15 |
|
| 16 |
def test_auto_packages(): |
| 17 |
module = common.Module('package1', samples_dir / 'package1') |
| 18 |
packages, pkg_data = sdist.auto_packages(module) |
| 19 |
assert packages == ['package1', 'package1.subpkg', 'package1.subpkg2'] |
| 20 |
assert pkg_data == {'': ['*'], |
| 21 |
'package1': ['data_dir/*'], |
| 22 |
'package1.subpkg': ['sp_data_dir/*'], |
| 23 |
} |
| 24 |
|
| 25 |
def test_make_sdist(): |
| 26 |
# Smoke test of making a complete sdist |
| 27 |
if not which('git'): |
| 28 |
pytest.skip("requires git") |
| 29 |
builder = sdist.SdistBuilder.from_ini_path(samples_dir / 'package1' / 'pyproject.toml') |
| 30 |
with TemporaryDirectory() as td: |
| 31 |
td = Path(td) |
| 32 |
builder.build(td) |
| 33 |
sdist_file = td / 'package1-0.1.tar.gz' |
| 34 |
assert_isfile(sdist_file) |
| 35 |
|
| 36 |
with tarfile.open(str(sdist_file)) as tf: |
| 37 |
assert 'package1-0.1/pyproject.toml' in tf.getnames() |
| 38 |
|
| 39 |
|
| 40 |
LIST_FILES = """\ |
| 41 |
#!{python} |
| 42 |
import sys |
| 43 |
from os.path import join |
| 44 |
if '--deleted' not in sys.argv: |
| 45 |
files = [ |
| 46 |
'foo', |
| 47 |
join('dir1', 'bar'), |
| 48 |
join('dir1', 'subdir', 'qux'), |
| 49 |
join('dir2', 'abc'), |
| 50 |
join('dist', 'def'), |
| 51 |
] |
| 52 |
mode = '{vcs}' |
| 53 |
if mode == 'git': |
| 54 |
print('\\0'.join(files), end='\\0') |
| 55 |
elif mode == 'hg': |
| 56 |
for f in files: |
| 57 |
print(f) |
| 58 |
""" |
| 59 |
|
| 60 |
LIST_FILES_GIT = LIST_FILES.format(python=sys.executable, vcs='git') |
| 61 |
LIST_FILES_HG = LIST_FILES.format(python=sys.executable, vcs='hg') |
| 62 |
|
| 63 |
|
| 64 |
def test_get_files_list_git(copy_sample): |
| 65 |
td = copy_sample('module1_toml') |
| 66 |
(td / '.git').mkdir() |
| 67 |
|
| 68 |
builder = sdist.SdistBuilder.from_ini_path(td / 'pyproject.toml') |
| 69 |
with MockCommand('git', LIST_FILES_GIT): |
| 70 |
files = builder.select_files() |
| 71 |
|
| 72 |
assert set(files) == { |
| 73 |
'foo', pjoin('dir1', 'bar'), pjoin('dir1', 'subdir', 'qux'), |
| 74 |
pjoin('dir2', 'abc') |
| 75 |
} |
| 76 |
|
| 77 |
def test_get_files_list_with_real_git(copy_sample): |
| 78 |
if not which('git'): |
| 79 |
pytest.skip("requires git") |
| 80 |
|
| 81 |
td = copy_sample('module1_toml') |
| 82 |
unicode_file = td / 'café.txt' |
| 83 |
unicode_file.write_text('tracked by git', encoding='utf-8') |
| 84 |
|
| 85 |
run(['git', 'init'], cwd=td, check=True, capture_output=True) |
| 86 |
run(['git', 'add', '.'], cwd=td, check=True, capture_output=True) |
| 87 |
|
| 88 |
builder = sdist.SdistBuilder.from_ini_path(td / 'pyproject.toml') |
| 89 |
files = builder.select_files() |
| 90 |
|
| 91 |
assert unicode_file.name in files |
| 92 |
|
| 93 |
def test_get_files_list_hg(tmp_path): |
| 94 |
dir1 = tmp_path / 'dir1' |
| 95 |
copytree(str(samples_dir / 'module1_toml'), str(dir1)) |
| 96 |
(tmp_path / '.hg').mkdir() |
| 97 |
builder = sdist.SdistBuilder.from_ini_path(dir1 / 'pyproject.toml') |
| 98 |
with MockCommand('hg', LIST_FILES_HG): |
| 99 |
files = builder.select_files() |
| 100 |
|
| 101 |
assert set(files) == { |
| 102 |
'bar', pjoin('subdir', 'qux') |
| 103 |
} |
| 104 |
|
| 105 |
def test_make_stubs_pkg(): |
| 106 |
builder = sdist.SdistBuilder.from_ini_path(samples_dir / 'sample-stubs' / 'pyproject.toml') |
| 107 |
with TemporaryDirectory() as td: |
| 108 |
td = Path(td) |
| 109 |
builder.build(td) |
| 110 |
sdist_file = td / 'wcwidth_stubs-0.2.13.1.tar.gz' |
| 111 |
assert_isfile(sdist_file) |