Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Don't allow to overwrite a virtual enviroment, updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
constrict0r committed Nov 12, 2018
1 parent a431d5a commit 3f67d9b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 33 deletions.
13 changes: 8 additions & 5 deletions amanita/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ def version_msg():
@click.version_option(__version__, u'-V', u'--version', message=version_msg())
@click.argument('path', required=True)
@click.option(
u'-d', u'--direnv', is_flag=True,
u'-d', u'--direnv', is_flag=True, default=False,
help=u'Install and configure direnv console enviroment switcher.')
@click.option(
u'-v', '--venv', is_flag=True,
u'-v', '--venv', is_flag=True, default=False,
help=u'Create and configure a virtual enviroment inside the project.')
@click.option(
u'-e', '--venv-path',
u'-e', '--venv-path', default=None,
help=u'Create and configure a virtual enviroment on the given path.')
@click.option(
u'--venv-only', is_flag=True, default=False,
help=u'Only create a virtual enviroment on PATH.')
# TODO: add --dry-run
def main(path, direnv, venv, venv_path):
def main(path, direnv, venv, venv_path, venv_only):
"""Creates a customizable python project
Package main entry point.
"""

project.Project.create(path, direnv, venv, venv_path)
project.Project.create(path, direnv, venv, venv_path, venv_only)
60 changes: 41 additions & 19 deletions amanita/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Project:

# Create the project.
@staticmethod
def create(path, direnv=False, venv=False, venv_path=None):
def create(path, direnv=False, venv=False, venv_path=None,
venv_only=False):
"""Create customizable python projects.
Each project includes a package directory layout and optionally:
Expand All @@ -28,26 +29,34 @@ def create(path, direnv=False, venv=False, venv_path=None):
direnv (bool): Configure direnv.
venv (bool): Create a virtual enviroment inside the project folder.
venv_path (str): Create a virtual enviroment on the given path.
venv_only (bool): Only create a virtual enviroment on PATH.
Returns:
bool: True for success, False otherwise.
"""
# Create package folder structure.
try:
assert subprocess.call('poetry new -- ' +
path, shell=True) == 0

except AssertionError:
click.echo(click.style('An error occured creating the project',
fg='red'))
sys.exit(1)

# Create virtual enviroment.
if venv is True:
if venv_only:
# Only create a virtual enviroment.
amanita.project.Project.venv_setup(path)

if venv_path is not None:
amanita.project.Project.venv_setup(venv_path)
else:
# Create package folder structure.
try:
assert subprocess.call('poetry new -- ' +
path, shell=True) == 0

except AssertionError:
click.echo(click.style('An error occured creating the ' +
'project.', fg='red'))
sys.exit(1)

# Create virtual enviroment inside.
if venv is True:
amanita.project.Project.venv_setup(path)

# Create virtual enviroment outside.
if venv_path is not None:
amanita.project.Project.venv_setup(venv_path)

return True

Expand All @@ -64,24 +73,37 @@ def venv_setup(path):
"""
from poetry.utils.env import Env

# This will overwrite an existing virtual enviroment.
try:
if os.path.isdir(os.path.join(path, '.venv')):
assert not os.listdir(os.path.join(path, '.venv'))

except AssertionError:
click.echo(
click.style('An error occurred creating ', fg='red') +
click.style('the virtual enviroment:\n', fg='green') +
click.style('Destination ', fg='red') +
click.style(os.path.abspath(os.path.join(path, '.venv')),
fg='yellow') +
click.style(' exists and is not empty.', fg='red'))
sys.exit(1)

try:
Env.build_venv(os.path.join(path, ".venv"))

click.echo('Created virtualenv ' + click.style('.venv',
fg='green') + ' in ' + click.style(path, fg='blue'))

except PermissionError:
click.echo(click.style('Error creating the virtual enviroment: ',
fg='red') +
click.echo(click.style('Error creating the ', fg='red') +
click.style('virtual enviroment: ', fg='green') +
click.style('Permission denied writing to ', fg='red') +
click.style(path, fg='green'))
click.style(path, fg='yellow'))
sys.exit(1)

except NotADirectoryError:
click.echo(click.style('Error creating the virtual enviroment: ',
fg='red') +
click.style(path, fg='cyan') +
click.style(path, fg='yellow') +
click.style(' is not a directory', fg='red'))
sys.exit(1)

Expand Down
49 changes: 40 additions & 9 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@
from amanita import project


# Create only virtual enviroment.
def test_create_only_venv():

if os.path.isdir('venv'):
shutil.rmtree(os.path.join('venv'))

subprocess.check_call('amanita venv --venv-only',
env=os.environ.copy(),
shell=True)
assert os.path.isdir('venv')
shutil.rmtree(os.path.join('venv'))


# Create only virtual enviroment with nonexisting path.
@pytest.mark.xfail
def test_create_only_venv_nonexisting():

subprocess.check_call('amanita /dev/null --venv-only',
env=os.environ.copy(),
shell=True)


# Create only virtual enviroment with non-permission.
@pytest.mark.xfail
def test_create_only_venv_forbiden():

subprocess.check_call('amanita /root --venv-only',
env=os.environ.copy(),
shell=True)


# Create project.
def test_create_project():

Expand Down Expand Up @@ -66,15 +97,6 @@ def test_create_venv_forbidden():
project.Project.venv_setup('/root')


# Overwrite virtual enviroment.
def test_overwrite_venv():

project.Project.venv_setup(os.path.join(''))
project.Project.venv_setup(os.path.join(''))
assert os.path.isdir('.venv')
shutil.rmtree(os.path.join('.venv'))


# Create project with virtual enviroment.
def test_create_project_venv():

Expand All @@ -85,9 +107,18 @@ def test_create_project_venv():
shutil.rmtree(os.path.join('muscaria'))


# Overwrite virtual enviroment.
@pytest.mark.xfail
def test_overwrite_venv():

project.Project.venv_setup(os.path.join(''))
project.Project.venv_setup(os.path.join(''))


# Create project with virtual enviroment outside the project directory.
def test_create_project_venv_out():

shutil.rmtree(os.path.join('.venv'))
subprocess.check_call('amanita muscaria -e venv',
env=os.environ.copy(),
shell=True)
Expand Down

0 comments on commit 3f67d9b

Please sign in to comment.