Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maroba committed Apr 29, 2021
1 parent 8c4a2d4 commit 21150fd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion manati/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create_project(name, no_git, no_install, author, description, license):

path = pathlib.Path.cwd() / name
if os.path.exists(path):
raise Exception('ERROR: Path already exists.')
raise click.BadParameter('ERROR: Path already exists.')

templates = pathlib.Path(__file__).parent / 'templates'
subs = {
Expand Down
6 changes: 2 additions & 4 deletions manati/manati.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ def create_project_command(name, no_git, no_install, author, description, licens
By default, the project is also pip-installed for development
in editable mode, and a local git repository is also created.
"""
try:
create_project(name, no_git, no_install, author, description, license)
except Exception as e:
click.echo(e)

create_project(name, no_git, no_install, author, description, license)


@cli.group('deploy')
Expand Down
8 changes: 8 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import os
from os.path import exists
import pathlib

Expand All @@ -24,3 +25,10 @@ def test_create_project(self):
assert exists(path / 'docs' / 'conf.py')
assert exists(path / 'docs' / '_build' / 'html' / 'index.html')

def test_create_project_do_not_overwrite(self):
runner = CliRunner()
with runner.isolated_filesystem():
os.mkdir('tee')
with self.assertRaises(Exception):
result = runner.invoke(cli, ['create', '-n' , 'tee'], input='\n\n\n')

18 changes: 18 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,21 @@ def test_run_docs(self, mock_launch):
result = runner.invoke(cli, ['run', 'docs'])
assert result.exit_code == 0
assert os.path.exists(Path.cwd() / 'docs' / '_build' / 'html' / 'index.html')

@unittest.mock.patch('manati.manati.find_project_data')
def test_run_flake8_guess(self, mock_fpd):
runner = CliRunner()
mock_fpd.return_value = {'package': 'test_project'}
with runner.isolated_filesystem():
create_project_structure('test_project', Path('test_project'), {'PROJECT_NAME': 'test_project',
'MODULE_NAME': 'test_project'})
result = runner.invoke(cli, ['run', 'flake8'], input='\n')
assert result.exit_code == 0

def test_run_flake8(self):
runner = CliRunner()
with runner.isolated_filesystem():
create_project_structure('test_project', Path('test_project'), {'PROJECT_NAME': 'test_project',
'MODULE_NAME': 'test_project'})
result = runner.invoke(cli, ['run', 'flake8', 'test_project'])
assert result.exit_code == 0
35 changes: 35 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
import unittest.mock
from pathlib import Path
import shutil
import os
import subprocess

from click.testing import CliRunner

from manati.utils import find_project_data, task


class TestUtils(unittest.TestCase):

@unittest.mock.patch('manati.utils.shell')
@unittest.mock.patch('manati.utils.os.environ.get')
def test_find_project_data_author_from_env(self, mock_env, mock_shell):
mock_shell.return_value = subprocess.run('blabla1234', shell=True)

runner = CliRunner()
with runner.isolated_filesystem():
mock_env.return_value = 'test_user'
info = find_project_data()
assert info.get('author') == 'test_user'

@unittest.mock.patch('manati.utils.click.secho')
def test_task(self, mock_secho):

@task('Before', 'After')
def func():
raise Exception

with self.assertRaises(Exception):
func()
mock_secho.assert_called_with('ERROR')

0 comments on commit 21150fd

Please sign in to comment.