def test_chdir(): origin = os.getcwd() parent_dir = os.path.dirname(os.path.abspath(origin)) with chdir(parent_dir): assert os.getcwd() == parent_dir assert os.getcwd() == origin
def egg(name, basic, cli, licenses): """Creates a new Python project. Think of an "egg" as a new idea. Values from your config file such as `name` and `pyversions` will be used to help populate fields. You can also specify things like the readme format and which CI service files to create. All options override the config file. Here is an example using an unmodified config file: \b $ hatch egg my-app Created project `my-app` $ tree --dirsfirst my-app my-app ├── my_app │ └── __init__.py ├── tests │ └── __init__.py ├── LICENSE-APACHE ├── LICENSE-MIT ├── MANIFEST.in ├── README.rst ├── requirements.txt ├── setup.py └── tox.ini 2 directories, 8 files """ try: settings = load_settings() except FileNotFoundError: click.echo('Unable to locate config file. Try `hatch config --restore`.') sys.exit(1) if basic: settings['basic'] = True if licenses: settings['licenses'] = licenses.split(',') settings['cli'] = cli origin = os.getcwd() d = os.path.join(origin, name) if os.path.exists(d): click.echo('Directory `{}` already exists.'.format(d)) sys.exit(1) os.makedirs(d) with chdir(d, cwd=origin): create_package(d, name, settings) click.echo('Created project `{}`'.format(name))
def build_package(d, universal=None, name=None, build_dir=None, pypath=None): command = [pypath or get_proper_python(), 'setup.py', 'sdist'] if build_dir: command.extend(['--dist-dir', build_dir]) command.append('bdist_wheel') if build_dir: command.extend(['--dist-dir', build_dir]) if universal: command.append('--universal') if name: command.extend(['--plat-name', name]) with chdir(d): result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL) return result.returncode
def test(package, local, path, cov, merge, test_args, cov_args, global_exe, no_detect): """Runs tests using `pytest`, optionally checking coverage. The path is derived in the following order: \b 1. The optional argument, which should be the name of a package that was installed via `hatch install -l` or `pip install -e`. 2. The --local flag. 3. The option --path, which can be a relative or absolute path. 4. The current directory. If the path points to a package, it should have a `tests` directory. If a project is detected but there is no dedicated virtual env, it will be created and any dev requirements will be installed in it. \b $ git clone https://github.com/ofek/privy && cd privy $ hatch test -c ========================= test session starts ========================== platform linux -- Python 3.5.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 rootdir: /home/ofek/privy, inifile: plugins: xdist-1.20.0, mock-1.6.2, httpbin-0.0.7, forked-0.2, cov-2.5.1 collected 10 items \b tests/test_privy.py .......... \b ====================== 10 passed in 4.34 seconds ======================= \b Tests completed, checking coverage... \b Name Stmts Miss Branch BrPart Cover Missing ----------------------------------------------------------------- privy/__init__.py 1 0 0 0 100% privy/core.py 30 0 0 0 100% privy/utils.py 13 0 4 0 100% tests/__init__.py 0 0 0 0 100% tests/test_privy.py 57 0 0 0 100% ----------------------------------------------------------------- TOTAL 101 0 4 0 100% """ if package: echo_waiting('Locating package...') path = get_editable_package_location(package) if not path: echo_failure('`{}` is not an editable package.'.format(package)) sys.exit(1) elif local: echo_waiting('Locating package...') name, path = get_editable_package_location() if not name: if path is None: echo_failure('There are no local packages available.') sys.exit(1) else: echo_failure( 'There are multiple local packages available. Select ' 'one with the optional argument.') sys.exit(1) echo_info('Package `{}` has been selected.'.format(name)) elif path: possible_path = resolve_path(path) if not possible_path: echo_failure('Directory `{}` does not exist.'.format(path)) sys.exit(1) path = possible_path else: path = os.getcwd() python_cmd = [sys.executable if global_exe else get_proper_python(), '-m'] command = python_cmd.copy() if cov: command.extend(['coverage', 'run']) command.extend(cov_args.split() if cov_args is not None else ( ['--parallel-mode'] if merge else [])) command.append('-m') command.append('pytest') command.extend(test_args.split()) try: # no cov sys.stdout.fileno() testing = False except io.UnsupportedOperation: # no cov testing = True # For testing we need to pipe because Click changes stdio streams. stdout = sys.stdout if not testing else subprocess.PIPE stderr = sys.stderr if not testing else subprocess.PIPE venv_dir = None if not (package or local) and not venv_active() and not no_detect and is_project(): venv_dir = os.path.join(path, get_venv_folder()) if not is_venv(venv_dir): echo_info('A project has been detected!') echo_waiting('Creating a dedicated virtual env... ', nl=False) create_venv(venv_dir) echo_success('complete!') with venv(venv_dir): echo_waiting('Installing this project in the virtual env...') install_packages(['-e', '.']) click.echo() echo_waiting('Ensuring pytest and coverage are available...') install_packages(['pytest', 'coverage']) click.echo() dev_requirements = get_requirements_file(path, dev=True) if dev_requirements: echo_waiting( 'Installing test dependencies in the virtual env...') install_packages(['-r', dev_requirements]) click.echo() with chdir(path): echo_waiting('Testing...') output = b'' if venv_dir: with venv(venv_dir): test_result = subprocess.run(command, stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) else: test_result = subprocess.run(command, stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) output += test_result.stdout or b'' output += test_result.stderr or b'' if cov: echo_waiting('\nTests completed, checking coverage...\n') if merge: if venv_dir: with venv(venv_dir): result = subprocess.run( python_cmd + ['coverage', 'combine', '--append'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) else: result = subprocess.run( python_cmd + ['coverage', 'combine', '--append'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) output += result.stdout or b'' output += result.stderr or b'' if venv_dir: with venv(venv_dir): result = subprocess.run( python_cmd + ['coverage', 'report', '--show-missing'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) else: result = subprocess.run( python_cmd + ['coverage', 'report', '--show-missing'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) output += result.stdout or b'' output += result.stderr or b'' if testing: # no cov click.echo(output.decode()) sys.exit(test_result.returncode)
def test(package, path, cov, merge, test_args, cov_args, env_aware): """Runs tests using `pytest`, optionally checking coverage. The path is derived in the following order: \b 1. The optional argument, which should be the name of a package that was installed via `hatch install -l` or `pip install -e`. 2. The option --path, which can be a relative or absolute path. 3. The current directory. If the path points to a package, it should have a `tests` directory. \b $ git clone https://github.com/ofek/privy && cd privy $ hatch test -c ========================= test session starts ========================== platform linux -- Python 3.5.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 rootdir: /home/ofek/privy, inifile: plugins: xdist-1.20.0, mock-1.6.2, httpbin-0.0.7, forked-0.2, cov-2.5.1 collected 10 items \b tests/test_privy.py .......... \b ====================== 10 passed in 4.34 seconds ======================= \b Tests completed, checking coverage... \b Name Stmts Miss Branch BrPart Cover Missing ----------------------------------------------------------------- privy/__init__.py 1 0 0 0 100% privy/core.py 30 0 0 0 100% privy/utils.py 13 0 4 0 100% tests/__init__.py 0 0 0 0 100% tests/test_privy.py 57 0 0 0 100% ----------------------------------------------------------------- TOTAL 101 0 4 0 100% """ if package: path = get_editable_package_location(package) if not path: click.echo('`{}` is not an editable package.'.format(package)) sys.exit(1) elif path: relative_path = os.path.join(os.getcwd(), basepath(path)) if os.path.exists(relative_path): path = relative_path elif not os.path.exists(path): click.echo('Directory `{}` does not exist.'.format(path)) sys.exit(1) else: path = os.getcwd() python_cmd = [get_proper_python(), '-m'] if env_aware else [] command = python_cmd.copy() if cov: command.extend(['coverage', 'run']) command.extend( cov_args.split() if cov_args is not None else (['--parallel-mode'] if merge else []) ) command.append('-m') command.append('pytest') command.extend(test_args.split()) try: # no cov sys.stdout.fileno() testing = False except io.UnsupportedOperation: # no cov testing = True # For testing we need to pipe because Click changes stdio streams. stdout = sys.stdout if not testing else subprocess.PIPE stderr = sys.stderr if not testing else subprocess.PIPE with chdir(path): output = b'' test_result = subprocess.run( command, stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL ) output += test_result.stdout or b'' output += test_result.stderr or b'' if cov: click.echo('\nTests completed, checking coverage...\n') if merge: result = subprocess.run( python_cmd + ['coverage', 'combine', '--append'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL ) output += result.stdout or b'' output += result.stderr or b'' result = subprocess.run( python_cmd + ['coverage', 'report', '--show-missing'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL ) output += result.stdout or b'' output += result.stderr or b'' if testing: # no cov click.echo(output.decode()) click.echo(output.decode()) sys.exit(test_result.returncode)
def new(name, no_env, pyname, pypath, global_packages, env_name, basic, cli, licenses): """Creates a new Python project. Values from your config file such as `name` and `pyversions` will be used to help populate fields. You can also specify things like the readme format and which CI service files to create. All options override the config file. By default a virtual env will be created in the project directory and will install the project locally so any edits will auto-update the installation. You can also locally install the created project in other virtual envs using the --env option. Here is an example using an unmodified config file: \b $ hatch new my-app Created project `my-app` $ tree --dirsfirst my-app my-app ├── my_app │ └── __init__.py ├── tests │ └── __init__.py ├── LICENSE-APACHE ├── LICENSE-MIT ├── MANIFEST.in ├── README.rst ├── requirements.txt ├── setup.py └── tox.ini 2 directories, 8 files """ try: settings = load_settings() except FileNotFoundError: settings = {} echo_warning( 'Unable to locate config file; try `hatch config --restore`. ' 'The default project structure will be used.') if basic: settings['basic'] = True if licenses: settings['licenses'] = licenses.split(',') settings['cli'] = cli origin = os.getcwd() d = os.path.join(origin, name) if os.path.exists(d): echo_failure('Directory `{}` already exists.'.format(d)) sys.exit(1) venvs = env_name.split('/') if env_name else [] if (venvs or not no_env) and pyname: try: settings = load_settings() except FileNotFoundError: # no cov echo_failure( 'Unable to locate config file. Try `hatch config --restore`.') sys.exit(1) pypath = settings.get('pypaths', {}).get(pyname, None) if not pypath: echo_failure( 'Unable to find a Python path named `{}`.'.format(pyname)) sys.exit(1) os.makedirs(d) with chdir(d, cwd=origin): create_package(d, name, settings) echo_success('Created project `{}`'.format(name)) if not no_env: venv_dir = os.path.join(d, 'venv') echo_waiting('Creating its own virtual env... ', nl=False) create_venv(venv_dir, pypath=pypath, use_global=global_packages) echo_success('complete!') with venv(venv_dir): echo_waiting('Installing locally in the virtual env... ', nl=False) install_packages(['-q', '-e', '.']) echo_success('complete!') for vname in venvs: venv_dir = os.path.join(VENV_DIR, vname) if not os.path.exists(venv_dir): echo_waiting('Creating virtual env `{}`... '.format(vname), nl=False) create_venv(venv_dir, pypath=pypath, use_global=global_packages) echo_success('complete!') with venv(venv_dir): echo_waiting( 'Installing locally in virtual env `{}`... '.format(vname), nl=False) install_packages(['-q', '-e', '.']) echo_success('complete!')