def test_install_requires_extra(deps, extras, installed): it = InstallTests() try: it.setUp() ins = Installer(samples_dir / 'extras' / 'pyproject.toml', python='mock_python', user=False, deps=deps, extras=extras) cmd = MockCommand('mock_python') get_reqs = ( "#!{python}\n" "import sys\n" "with open({recording_file!r}, 'wb') as w, open(sys.argv[-1], 'rb') as r:\n" " w.write(r.read())" ).format(python=sys.executable, recording_file=cmd.recording_file) cmd.content = get_reqs with cmd as mock_py: ins.install_requirements() with open(mock_py.recording_file) as f: str_deps = f.read() deps = str_deps.split('\n') if str_deps else [] assert set(deps) == installed finally: it.tearDown()
def test_symlink_other_python(self): if os.name == 'nt': raise SkipTest('symlink') (self.tmpdir / 'site-packages2').mkdir() (self.tmpdir / 'scripts2').mkdir() # Called by Installer._auto_user() : script1 = ("#!{python}\n" "import sysconfig\n" "print(True)\n" # site.ENABLE_USER_SITE "print({purelib!r})" # sysconfig.get_path('purelib') ).format(python=sys.executable, purelib=str(self.tmpdir / 'site-packages2')) # Called by Installer._get_dirs() : script2 = ("#!{python}\n" "import json, sys\n" "json.dump({{'purelib': {purelib!r}, 'scripts': {scripts!r} }}, " "sys.stdout)" ).format(python=sys.executable, purelib=str(self.tmpdir / 'site-packages2'), scripts=str(self.tmpdir / 'scripts2')) with MockCommand('mock_python', content=script1): ins = Installer(samples_dir / 'package1' / 'flit.ini', python='mock_python', symlink=True) with MockCommand('mock_python', content=script2): ins.install() assert_islink(self.tmpdir / 'site-packages2' / 'package1', to=samples_dir / 'package1' / 'package1') assert_isfile(self.tmpdir / 'scripts2' / 'pkg_script') with (self.tmpdir / 'scripts2' / 'pkg_script').open() as f: assert f.readline().strip() == "#!mock_python"
def test_console_example(): responses.add_callback( 'GET', re.compile(r'https://www.python.org/ftp/.*'), callback=respond_python_zip, content_type='application/zip', ) with TemporaryWorkingDirectory() as td: for src in example_dir.iterdir(): copy(str(src), td) with modified_env({CACHE_ENV_VAR: td}), \ MockCommand('makensis') as makensis: ec = main(['installer.cfg']) assert ec == 0 assert makensis.get_calls()[0]['argv'][1].endswith('installer.nsi') build_dir = Path(td, 'build', 'nsis') assert_isdir(build_dir) assert_isfile(build_dir / 'Python' / 'python.exe') assert_isfile(build_dir / 'pkgs' / 'guessnumber.py') assert_isfile(build_dir / 'Guess_the_Number.launch.py')
def test_get_files_list_hg(tmp_path): dir1 = tmp_path / 'dir1' copytree(str(samples_dir / 'module1_ini'), str(dir1)) (tmp_path / '.hg').mkdir() builder = sdist.SdistBuilder.from_ini_path(dir1 / 'flit.ini') with MockCommand('hg', LIST_FILES): files = builder.select_files() assert set(files) == {'bar', pjoin('subdir', 'qux')}
def test_install_requires(self): ins = Installer(samples_dir / 'requires-requests.toml', user=False, python='mock_python') with MockCommand('mock_python') as mockpy: ins.install_requirements() calls = mockpy.get_calls() assert len(calls) == 1 assert calls[0]['argv'][1:5] == ['-m', 'pip', 'install', '-r']
def test_git(temp_tree: TempTreeCB): spec = {".git": {}, "git_mod.py": "print('hello')\n"} with temp_tree(spec) as package, MockCommand( "git", mock_git_describe.format(package)): v = get_version.get_version_from_git(package) assert get_version.Version("0.1.2", "3", ["fefe123", "dirty"]) == v v = get_version.get_version(package / "git_mod.py") assert "0.1.2.dev3+fefe123.dirty" == v
def test_build_main(copy_sample): td = copy_sample('module1_toml') shutil.copy(str(samples_dir / 'EG_README.rst'), str(td)) (td / '.git').mkdir() # Fake a git repo with MockCommand('git', LIST_FILES_TEMPLATE.format( python=sys.executable, module='module1.py')): res = build.main(td / 'pyproject.toml') assert res.wheel.file.suffix == '.whl' assert res.sdist.file.name.endswith('.tar.gz') assert_isdir(td / 'dist')
def test_pip_install(self): ins = Installer(samples_dir / 'package1' / 'flit.ini', python='mock_python', user=False) with MockCommand('mock_python') as mock_py: ins.install() calls = mock_py.get_calls() assert len(calls) == 1 cmd = calls[0]['argv'] assert cmd[1:4] == ['-m', 'pip', 'install'] assert cmd[4].endswith('package1-0.1-py2.py3-none-any.whl')
def test_get_files_list_hg(): with TemporaryDirectory() as td: dir1 = Path(td, 'dir1') dir1.mkdir() copy(str(samples_dir / 'module1.py'), str(dir1)) copy(str(samples_dir / 'module1-pkg.ini'), str(dir1)) td = Path(td) (td / '.hg').mkdir() builder = sdist.SdistBuilder(dir1 / 'module1-pkg.ini') with MockCommand('hg', LIST_FILES): files = builder.find_tracked_files() assert set(files) == {'bar', pjoin('subdir', 'qux')}
def test_build_wheel_only(copy_sample): td = copy_sample('module1_toml') (td / '.git').mkdir() # Fake a git repo with MockCommand( 'git', LIST_FILES_TEMPLATE.format(python=sys.executable, module='module1.py')): res = build.main(td / 'pyproject.toml', formats={'wheel'}) assert res.sdist is None # Compare str path to work around pathlib/pathlib2 mismatch on Py 3.5 assert [str(p) for p in (td / 'dist').iterdir()] == [str(res.wheel.file)]
def test_pip_install(self): ins = Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', python='mock_python', user=False) with MockCommand('mock_python') as mock_py: ins.install() calls = mock_py.get_calls() assert len(calls) == 1 cmd = calls[0]['argv'] assert cmd[1:4] == ['-m', 'pip', 'install'] assert cmd[4].endswith('package1')
def test_build_main(): with TemporaryDirectory() as td: pyproject = Path(td, 'pyproject.toml') shutil.copy(str(samples_dir / 'module1-pkg.toml'), str(pyproject)) shutil.copy(str(samples_dir / 'module1.py'), td) shutil.copy(str(samples_dir / 'EG_README.rst'), td) Path(td, '.git').mkdir() # Fake a git repo with MockCommand('git', LIST_FILES): res = build.main(pyproject) assert res.wheel.file.suffix == '.whl' assert res.sdist.file.name.endswith('.tar.gz') assert_isdir(Path(td, 'dist'))
def test_install_requires_with_extra_index_url(self): ins = Installer.from_ini_path(samples_dir / 'requires-requests.toml', user=False, python='mock_python', extra_index_url='https://fake.index') with MockCommand('mock_python') as mockpy: ins.install_requirements() calls = mockpy.get_calls() assert len(calls) == 1 assert calls[0]['argv'][1:7] == [ '-m', 'pip', 'install', '--extra-index-url', 'https://fake.index', '-r' ]
def test_get_files_list_git(copy_sample): td = copy_sample('module1_ini') (td / '.git').mkdir() builder = sdist.SdistBuilder.from_ini_path(td / 'flit.ini') with MockCommand('git', LIST_FILES): files = builder.select_files() assert set(files) == { 'foo', pjoin('dir1', 'bar'), pjoin('dir1', 'subdir', 'qux'), pjoin('dir2', 'abc') }
def test_build_module_no_docstring(): with TemporaryDirectory() as td: pyproject = Path(td, 'pyproject.toml') shutil.copy(str(samples_dir / 'no_docstring-pkg.toml'), str(pyproject)) shutil.copy(str(samples_dir / 'no_docstring.py'), td) shutil.copy(str(samples_dir / 'EG_README.rst'), td) Path(td, '.git').mkdir() # Fake a git repo with MockCommand('git', LIST_FILES_TEMPLATE.format( python=sys.executable, module='no_docstring.py')): with pytest.raises(common.NoDocstringError) as exc_info: build.main(pyproject) assert 'no_docstring.py' in str(exc_info.value)
def test_get_files_list_git(): with TemporaryDirectory() as td: copy(str(samples_dir / 'module1.py'), td) copy(str(samples_dir / 'module1-pkg.ini'), td) td = Path(td) (td / '.git').mkdir() builder = sdist.SdistBuilder(td / 'module1-pkg.ini') with MockCommand('git', LIST_FILES): files = builder.find_tracked_files() assert set(files) == { 'foo', pjoin('dir1', 'bar'), pjoin('dir1', 'subdir', 'qux'), pjoin('dir2', 'abc') }
def test_pip_install_with_extra_index_url(self): ins = Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', python='mock_python', user=False, extra_index_url='https://fake.index') with MockCommand('mock_python') as mock_py: ins.install() calls = mock_py.get_calls() assert len(calls) == 1 cmd = calls[0]['argv'] assert cmd[1:4] == ['-m', 'pip', 'install'] assert cmd[4].endswith('package1') assert cmd[5:7] == ['--extra-index-url', 'https://fake.index'] assert len(cmd) == 7
def test_build_module_no_docstring(): with TemporaryDirectory() as td: pyproject = Path(td, 'pyproject.toml') shutil.copy(str(samples_dir / 'no_docstring-pkg.toml'), str(pyproject)) shutil.copy(str(samples_dir / 'no_docstring.py'), td) shutil.copy(str(samples_dir / 'EG_README.rst'), td) Path(td, '.git').mkdir() # Fake a git repo with MockCommand( 'git', LIST_FILES_TEMPLATE.format(python=sys.executable, module='no_docstring.py')): with pytest.raises(ValueError) as exc_info: build.main(pyproject) assert str(exc_info.value) == ( 'Flit cannot package module without docstring, or empty docstring. ' 'Please add a docstring to your module.')
def test_console_example(tmp_path, console_eg_copy, monkeypatch): responses.add_callback( 'GET', re.compile(r'https://www.python.org/ftp/.*'), callback=respond_python_zip, content_type='application/zip', ) monkeypatch.chdir(console_eg_copy) monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / 'cache')) with MockCommand('makensis') as makensis: ec = main(['installer.cfg']) assert ec == 0 assert makensis.get_calls()[0]['argv'][1].endswith('installer.nsi') build_dir = console_eg_copy / 'build' / 'nsis' assert_isdir(build_dir) assert_isfile(build_dir / 'Python' / 'python.exe') assert_isfile(build_dir / 'pkgs' / 'sample_printer' / '__init__.py') assert_isfile(build_dir / 'Sample_printer.launch.py')