Ejemplo n.º 1
0
def test_pycache():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])
        files = find_all_files(d)

        root_file = os.path.join(d, '__pycache__', 'ok.txt')
        create_file(root_file)
        assert os.path.exists(root_file)

        non_root_file = os.path.join(d, 'ok', '__pycache__', 'ok.txt')
        create_file(non_root_file)
        assert os.path.exists(non_root_file)

        result = runner.invoke(hatch, ['clean', '-v'])

        assert result.exit_code == 0
        assert 'Cleaned!' in result.output
        assert ('Removed paths:\n'
                '{}\n'
                '{}\n'
                '{}\n'
                '{}\n'.format(os.path.join(d, '__pycache__'), root_file,
                              os.path.join(d, 'ok', '__pycache__'),
                              non_root_file)) in result.output
        assert not os.path.exists(root_file)
        assert not os.path.exists(non_root_file)
        assert_files_exist(files)
Ejemplo n.º 2
0
 def test_default_exists_dev_exists_dev_only(self):
     with temp_chdir() as d:
         file1 = os.path.join(d, 'requirements.txt')
         file2 = os.path.join(d, 'requirementss.txt')
         create_file(file1)
         create_file(file2)
         assert get_requirements_file(d, dev=True) == file2
Ejemplo n.º 3
0
def test_package():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['new', 'ok', '--cli', '-ne'])
        package_dir = os.path.join(d, 'ok')
        files = find_all_files(package_dir)

        test_file = os.path.join(package_dir, 'test.pyc')
        create_file(test_file)
        assert os.path.exists(test_file)

        venv_dir = os.path.join(d, 'venv')
        create_venv(venv_dir)

        with venv(venv_dir):
            os.chdir(package_dir)
            install_packages(['-e', '.'])
            os.chdir(d)

            result = runner.invoke(hatch, ['clean', 'ok'])

        assert result.exit_code == 0
        assert 'Cleaned!' in result.output
        assert not os.path.exists(test_file)
        assert os.path.exists(os.path.join(package_dir, 'ok.egg-info'))
        assert_files_exist(files)
Ejemplo n.º 4
0
def test_extras():
    with temp_chdir() as d:
        runner = CliRunner()
        test_dir = os.path.join(d, 'a', 'b')
        test_file1 = os.path.join(test_dir, 'file1.txt')
        test_file2 = os.path.join(d, 'x', 'y', 'file2.txt')
        test_glob = '{}{}*'.format(os.path.join(d, 'x'), os.path.sep)
        fake_file = os.path.join(test_dir, 'file.py')
        create_file(test_file1)
        create_file(test_file2)

        with temp_move_path(SETTINGS_FILE, d):
            new_settings = copy_default_settings()
            new_settings['extras'] = [
                test_dir, test_file1, test_glob, fake_file
            ]
            save_settings(new_settings)

            runner.invoke(hatch, ['egg', 'ok', '--basic'])
            d = os.path.join(d, 'ok')

        assert os.path.exists(os.path.join(d, 'b', 'file1.txt'))
        assert os.path.exists(os.path.join(d, 'file1.txt'))
        assert os.path.exists(os.path.join(d, 'y', 'file2.txt'))
        assert not os.path.exists(os.path.join(d, 'file2.txt'))
        assert not os.path.exists(os.path.join(d, 'file.py'))
Ejemplo n.º 5
0
    def test_depth_exceeded(self):
        with temp_chdir() as d:
            create_file(os.path.join(d, 'pyproject.toml'))
            test_dir = os.path.join(d, '3', '2', '1', 'test')
            os.makedirs(test_dir)

            with pytest.raises(Exception):
                find_project_root(test_dir)
Ejemplo n.º 6
0
def test_build_dir_file():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])
        build_dir = os.path.join(d, '_build_dir')
        create_file(build_dir)

        result = runner.invoke(hatch, ['build', '-d', build_dir])

        assert result.exit_code == 1
        assert 'Files found in' not in result.output
Ejemplo n.º 7
0
def test_temp_move_path():
    with temp_chdir() as d1:
        path = os.path.join(d1, 'test')
        create_file(path)
        assert os.path.exists(path)

        with temp_chdir() as d2:
            with temp_move_path(path, d2) as dst:
                assert not os.path.exists(path)
                assert dst == os.path.join(d2, 'test')

        assert os.path.exists(path)
Ejemplo n.º 8
0
def test_compiled_only_project_venv_no_detect():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])
        files = find_all_files(d)

        test_file1 = os.path.join(d, 'test.pyc')
        test_file2 = os.path.join(d, 'ok', 'test.pyc')
        test_file3 = os.path.join(d, 'ok', 'deeper', 'test.pyc')
        test_file4 = os.path.join(d, 'venv', 'test.pyc')
        create_file(test_file1)
        create_file(test_file2)
        create_file(test_file3)
        create_file(test_file4)
        assert os.path.exists(test_file1)
        assert os.path.exists(test_file2)
        assert os.path.exists(test_file3)
        assert os.path.exists(test_file4)

        result = runner.invoke(hatch, ['clean', '-c', '-nd'])

        assert result.exit_code == 0
        assert 'Cleaned!' in result.output
        assert not os.path.exists(test_file1)
        assert not os.path.exists(test_file2)
        assert not os.path.exists(test_file3)
        assert not os.path.exists(test_file4)
        assert_files_exist(files)
Ejemplo n.º 9
0
def test_path_relative():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['new', 'ok', '--basic'])
        package_dir = os.path.join(d, 'ok')
        files = find_all_files(package_dir)

        test_file = os.path.join(package_dir, 'test.pyc')
        create_file(test_file)
        assert os.path.exists(test_file)

        result = runner.invoke(hatch, ['clean', '-p', 'ok'])

        assert result.exit_code == 0
        assert not os.path.exists(test_file)
        assert_files_exist(files)
Ejemplo n.º 10
0
def test_clean():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic'])

        old_artifact = os.path.join(d, 'dist', 'foo.whl')
        create_file(old_artifact)
        assert os.path.exists(old_artifact)

        result = runner.invoke(hatch, ['build', '-c'])
        files = os.listdir(os.path.join(d, 'dist'))

        assert result.exit_code == 0
        assert matching_file(r'.*\.whl$', files)
        assert len(files) == 2
        assert not os.path.exists(old_artifact)
Ejemplo n.º 11
0
def test_coverage():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic'])
        create_file(os.path.join(d, 'ok', '.coverage'))
        files = find_all_files(d)

        test_file = os.path.join(d, '.coverage')
        create_file(test_file)
        assert os.path.exists(test_file)

        result = runner.invoke(hatch, ['clean', '-v'])

        assert result.exit_code == 0
        assert result.output == ('Removed paths:\n' '{}\n'.format(test_file))
        assert not os.path.exists(test_file)
        assert_files_exist(files)
Ejemplo n.º 12
0
def test_path_full():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['new', 'ok', '--basic', '-ne'])
        runner.invoke(hatch, ['new', 'ko', '--basic', '-ne'])
        package_dir = os.path.join(d, 'ok')
        files = find_all_files(package_dir)

        test_file = os.path.join(package_dir, 'test.pyc')
        create_file(test_file)
        assert os.path.exists(test_file)

        os.chdir(os.path.join(d, 'ko'))
        result = runner.invoke(hatch, ['clean', '-p', package_dir])

        assert result.exit_code == 0
        assert 'Cleaned!' in result.output
        assert not os.path.exists(test_file)
        assert_files_exist(files)
Ejemplo n.º 13
0
def test_cwd():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic'])
        files = find_all_files(d)

        test_file1 = os.path.join(d, 'test.pyc')
        test_file2 = os.path.join(d, 'ok.egg-info', 'entry_points.txt')
        create_file(test_file1)
        create_file(test_file2)
        assert os.path.exists(test_file1)
        assert os.path.exists(test_file2)

        result = runner.invoke(hatch, ['clean'])

        assert result.exit_code == 0
        assert not os.path.exists(test_file1)
        assert not os.path.exists(os.path.join(d, 'ok.egg-info'))
        assert_files_exist(files)
Ejemplo n.º 14
0
def test_project_ignore_venv():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])
        files = find_all_files(d)

        test_file1 = os.path.join(d, 'test.pyc')
        test_file2 = os.path.join(d, 'venv', 'test.pyc')
        create_file(test_file1)
        create_file(test_file2)
        assert os.path.exists(test_file1)
        assert os.path.exists(test_file2)

        result = runner.invoke(hatch, ['clean'])

        assert result.exit_code == 0
        assert 'Cleaned!' in result.output
        assert not os.path.exists(test_file1)
        assert os.path.exists(test_file2)
        assert_files_exist(files)
Ejemplo n.º 15
0
def test_egg_info():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])
        create_file(os.path.join(d, 'ok', 'ok.egg-info', 'PKG-INFO'))
        files = find_all_files(d)

        test_file = os.path.join(d, 'ok.egg-info', 'PKG-INFO')
        create_file(test_file)
        assert os.path.exists(test_file)

        result = runner.invoke(hatch, ['clean', '-v'])

        assert result.exit_code == 0
        assert 'Cleaned!' in result.output
        assert ('Removed paths:\n'
                '{}\n'
                '{}\n'.format(os.path.join(d, 'ok.egg-info'),
                              test_file)) in result.output
        assert not os.path.exists(test_file)
        assert_files_exist(files)
Ejemplo n.º 16
0
def test_pyd():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic'])
        files = find_all_files(d)

        root_file = os.path.join(d, 'ok.pyd')
        create_file(root_file)
        assert os.path.exists(root_file)

        non_root_file = os.path.join(d, 'ok', 'ko.pyd')
        create_file(non_root_file)
        assert os.path.exists(non_root_file)

        result = runner.invoke(hatch, ['clean', '-v'])

        assert result.exit_code == 0
        assert result.output == ('Removed paths:\n'
                                 '{}\n'
                                 '{}\n'.format(root_file, non_root_file))
        assert not os.path.exists(root_file)
        assert not os.path.exists(non_root_file)
        assert_files_exist(files)
Ejemplo n.º 17
0
def test_cwd_compiled_only():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic'])
        files = find_all_files(d)

        test_file1 = os.path.join(d, 'test.pyc')
        test_file2 = os.path.join(d, 'ok', 'test.pyc')
        test_file3 = os.path.join(d, 'ok', 'deeper', 'test.pyc')
        create_file(test_file1)
        create_file(test_file2)
        create_file(test_file3)
        assert os.path.exists(test_file1)
        assert os.path.exists(test_file2)
        assert os.path.exists(test_file3)

        result = runner.invoke(hatch, ['clean', '-c'])

        assert result.exit_code == 0
        assert not os.path.exists(test_file1)
        assert not os.path.exists(test_file2)
        assert not os.path.exists(test_file3)
        assert_files_exist(files)
Ejemplo n.º 18
0
 def test_depth(self):
     with temp_chdir() as d:
         create_file(os.path.join(d, 'pyproject.toml'))
         test_dir = os.path.join(d, '2', '1', 'test')
         os.makedirs(test_dir)
         assert find_project_root(test_dir) == d
Ejemplo n.º 19
0
def test_matching_file():
    with temp_chdir() as d:
        create_file(os.path.join(d, 'file.txt'))
        files = os.listdir(d)
        assert matching_file(r'^fi.+t$', files)
        assert not matching_file(r'\.json$', files)
Ejemplo n.º 20
0
def test_temp_chdir():
    with temp_chdir() as d:
        create_file(os.path.join(d, 'file.txt'))

    assert not os.path.exists(d)
Ejemplo n.º 21
0
 def test_default_not_exists_dev_exists(self):
     with temp_chdir() as d:
         file = os.path.join(d, 'dev-requirements.txt')
         create_file(file)
         assert get_requirements_file(d) == file
Ejemplo n.º 22
0
def restore_settings():
    create_file(SETTINGS_FILE)
    save_settings(DEFAULT_SETTINGS)
Ejemplo n.º 23
0
 def test_cwd_setup(self):
     with temp_chdir() as d:
         create_file(os.path.join(d, 'setup.py'))
         assert find_project_root() == d
Ejemplo n.º 24
0
def create_package(d, package_name, settings):
    normalized_package_name = normalize_package_name(package_name)
    cli = settings.get('cli')
    basic = settings.get('basic', DEFAULT_SETTINGS['basic'])
    extra_files = []

    author = settings.get('name') or DEFAULT_SETTINGS['name']
    version = settings.get('version') or '0.0.1'
    email = settings.get('email') or DEFAULT_SETTINGS['email']
    description = settings.get('description') or ''
    pyversions = sorted(
        settings.get('pyversions') or DEFAULT_SETTINGS['pyversions'])
    vc_setup = VC_SETUP[settings.get('vc') or DEFAULT_SETTINGS['vc']]
    vc_url = settings.get('vc_url') or DEFAULT_SETTINGS['vc_url']
    package_url = vc_url + '/' + package_name

    readme_format = (settings.get('readme', {}).get('format')
                     or DEFAULT_SETTINGS['readme']['format'])

    licenses = [
        LICENSES[li](author)
        for li in settings.get('licenses') or DEFAULT_SETTINGS['licenses']
    ]

    badges = []
    if not basic:
        for badge_info in settings.get('readme', {}).get('badges', []):
            image = badge_info.pop('image', 'no_image')
            target = badge_info.pop('target', 'no_target')
            alt = badge_info.pop('alt', 'badge')

            badges.append(
                Badge(image.format(package_name), target.format(package_name),
                      alt, badge_info))

    readme = README[readme_format](package_name, pyversions, licenses, badges)

    setup_py = SetupFile(author, email, package_name, pyversions, licenses,
                         readme, package_url, cli)
    projectfile = ProjectFile(package_name, version, author, email,
                              description, pyversions, licenses, package_url)

    coverage_service = settings.get('coverage') if not basic else None
    if coverage_service:
        coverage_service = COVERAGE_SERVICES[coverage_service]()
        extra_files.append(coverage_service)

    for service in settings.get('ci', []):
        if not basic:
            extra_files.append(CI_SERVICES[service](pyversions,
                                                    coverage_service))

    coveragerc = CoverageConfig(package_name, cli)
    tox = Tox(pyversions, coverage_service)

    package_dir = os.path.join(d, normalized_package_name)
    init_py = File('__init__.py',
                   "__version__ = '{version}'\n".format(version=version))
    init_py.write(package_dir)
    create_file(os.path.join(d, 'tests', '__init__.py'))

    if cli:
        cli_py = File(
            'cli.py', "def {}():\n    print('Hello world!')\n".format(
                normalized_package_name))
        cli_py.write(package_dir)
        main_py = File(
            '__main__.py', 'import sys\n'
            'from {npn}.cli import {npn}\n'
            'sys.exit({npn}())\n'.format(npn=normalized_package_name))
        main_py.write(package_dir)

    setup_py.write(d)
    projectfile.write(d)
    readme.write(d)
    coveragerc.write(d)
    tox.write(d)

    requirements = File('requirements.txt', "-e .\n")
    requirements.write(d)

    for li in licenses:
        li.write(d)

    for file in extra_files:
        file.write(d)

    for p in settings.get('extras', []):
        for path in glob.iglob(p):
            copy_path(path, d)

    manifest_text = ''

    manifest_files = {
        'AUTHORS*',
        'CHANGELOG*',
        'CHANGES*',
        'CONTRIBUTING*',
        'HISTORY*',
        'LICENCE*',
        'LICENSE*',
        'README*',
    }

    for pattern in manifest_files:
        for path in sorted(glob.iglob(os.path.join(d, pattern))):
            if os.path.isfile(path):  # no cov
                manifest_text += 'include {}\n'.format(os.path.basename(path))

    manifest = File('MANIFEST.in', manifest_text)
    manifest.write(d)

    vc_setup(d, package_name)
Ejemplo n.º 25
0
 def test_cwd_pyproject(self):
     with temp_chdir() as d:
         create_file(os.path.join(d, 'pyproject.toml'))
         assert find_project_root() == d