[tests][cli] fixed cli import

This commit is contained in:
Maxime Alves LIRMM 2020-08-05 14:28:55 +02:00
parent 6a65aaeaef
commit 914cb149e1
1 changed files with 47 additions and 30 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import subprocess import subprocess
import importlib
import pytest import pytest
from click.testing import CliRunner from click.testing import CliRunner
@ -8,6 +9,7 @@ from configparser import ConfigParser
from halfapi import __version__ from halfapi import __version__
from halfapi.cli import cli from halfapi.cli import cli
Cli = cli.cli
PROJNAME = os.environ.get('PROJ','tmp_api') PROJNAME = os.environ.get('PROJ','tmp_api')
@ -37,47 +39,47 @@ class TestCli():
def test_options(self, runner, dropdb, createdb): def test_options(self, runner, dropdb, createdb):
# Wrong command # Wrong command
with runner.isolated_filesystem(): with runner.isolated_filesystem():
r = runner.invoke(cli, ['foobar']) r = runner.invoke(Cli, ['foobar'])
assert r.exit_code == 2 assert r.exit_code == 2
# Test existing commands # Test existing commands
with runner.isolated_filesystem(): with runner.isolated_filesystem():
r = runner.invoke(cli, ['--help']) r = runner.invoke(Cli, ['--help'])
assert r.exit_code == 0 assert r.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
r = runner.invoke(cli, ['--version']) r = runner.invoke(Cli, ['--version'])
assert r.exit_code == 0 assert r.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
r = runner.invoke(cli, ['init', '--help']) r = runner.invoke(Cli, ['init', '--help'])
assert r.exit_code == 0 assert r.exit_code == 0
def test_init_project_fail(self, runner, dropdb): def test_init_project_fail(self, runner, dropdb):
# Missing argument (project) # Missing argument (project)
testproject = 'testproject' testproject = 'testproject'
r = runner.invoke(cli, ['init']) r = runner.invoke(Cli, ['init'])
assert r.exit_code == 2 assert r.exit_code == 2
with runner.isolated_filesystem(): with runner.isolated_filesystem():
# Fail : Wrong project name # Fail : Wrong project name
r = runner.invoke(cli, ['init', 'test*-project']) r = runner.invoke(Cli, ['init', 'test*-project'])
assert r.exit_code == 1 assert r.exit_code == 1
with runner.isolated_filesystem(): with runner.isolated_filesystem():
# Fail : Already existing folder # Fail : Already existing folder
os.mkdir(testproject) os.mkdir(testproject)
r = runner.invoke(cli, ['init', testproject]) r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1 assert r.exit_code == 1
with runner.isolated_filesystem(): with runner.isolated_filesystem():
# Fail : Already existing nod # Fail : Already existing nod
os.mknod(testproject) os.mknod(testproject)
r = runner.invoke(cli, ['init', testproject]) r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1 assert r.exit_code == 1
def test_init_project(self, runner, dropdb): def test_init_project(self, runner, dropdb, createdb):
cp = ConfigParser() cp = ConfigParser()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
env = { env = {
@ -85,7 +87,7 @@ class TestCli():
'HALFAPI_CONF_DIR': os.environ.get('HALFAPI_CONF_DIR', os.getcwd()), 'HALFAPI_CONF_DIR': os.environ.get('HALFAPI_CONF_DIR', os.getcwd()),
} }
res = runner.invoke(cli, ['init', PROJNAME], env=env) res = runner.invoke(Cli, ['init', PROJNAME], env=env)
try: try:
assert os.path.isdir(PROJNAME) assert os.path.isdir(PROJNAME)
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi')) assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
@ -109,58 +111,73 @@ class TestCli():
assert res.exit_code == 0 assert res.exit_code == 0
assert res.exception is None assert res.exception is None
def test_run_commands(self, runner, dropdb): def test_run_commands(self, runner, dropdb, createdb):
def reloadcli():
importlib.reload(cli)
return cli.cli
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['run', '--help']) assert res.exit_code == 0
os.chdir(PROJNAME)
Cli2 = reloadcli()
res = runner.invoke(Cli2, ['run', '--help'])
assert res.exception is None
assert res.exit_code == 0 assert res.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['run', 'foobar']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['run', 'foobar'])
assert res.exit_code == 2 assert res.exit_code == 2
def test_domain_commands(self, runner, dropdb): def test_domain_commands(self, runner, dropdb, createdb):
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['domain', 'foobar']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['domain', 'foobar'])
assert res.exit_code == 2 assert res.exit_code == 2
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['domain', '--help']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['domain', '--help'])
assert r.exit_code == 0 assert r.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['domain', 'create', '--help']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['domain', 'create', '--help'])
assert r.exit_code == 0 assert r.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['domain', 'read', '--help']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['domain', 'read', '--help'])
assert r.exit_code == 0 assert r.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['domain', 'update', '--help']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['domain', 'update', '--help'])
assert r.exit_code == 0 assert r.exit_code == 0
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
res = runner.invoke(cli, ['domain', 'delete', '--help']) os.chdir(PROJNAME)
res = runner.invoke(Cli, ['domain', 'delete', '--help'])
assert r.exit_code == 0 assert r.exit_code == 0
def test_domain_create(self, runner, dropdb): def test_domain_create(self, runner, dropdb):
with runner.isolated_filesystem(): with runner.isolated_filesystem():
res = runner.invoke(cli, ['init', PROJNAME]) res = runner.invoke(Cli, ['init', PROJNAME])
assert res.exit_code == 0 assert res.exit_code == 0
os.chdir(PROJNAME) os.chdir(PROJNAME)
DOMNAME='tmp_domain' DOMNAME='tmp_domain'
res = runner.invoke(cli, ['domain', 'create', DOMNAME]) res = runner.invoke(Cli, ['domain', 'create', DOMNAME])
srcdir = os.path.join('domains', 'src', DOMNAME) srcdir = os.path.join('domains', 'src', DOMNAME)
assert os.path.isdir(srcdir) assert os.path.isdir(srcdir)
moddir = os.path.join(srcdir, DOMNAME) moddir = os.path.join(srcdir, DOMNAME)