2020-07-29 21:03:00 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import os
|
2020-08-05 11:10:14 +02:00
|
|
|
|
import subprocess
|
2020-08-05 14:28:55 +02:00
|
|
|
|
import importlib
|
2020-08-07 00:00:33 +02:00
|
|
|
|
import tempfile
|
|
|
|
|
from unittest.mock import patch
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from click.testing import CliRunner
|
2020-08-05 11:10:14 +02:00
|
|
|
|
from configparser import ConfigParser
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
from halfapi import __version__
|
2020-07-29 21:03:00 +02:00
|
|
|
|
from halfapi.cli import cli
|
2020-08-05 14:28:55 +02:00
|
|
|
|
Cli = cli.cli
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
PROJNAME = os.environ.get('PROJ','tmp_api')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.incremental
|
|
|
|
|
class TestCli():
|
2020-08-05 12:27:56 +02:00
|
|
|
|
def test_options(self, runner, dropdb, createdb):
|
2020-08-05 11:10:14 +02:00
|
|
|
|
# Wrong command
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['foobar'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 2
|
|
|
|
|
|
|
|
|
|
# Test existing commands
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['--version'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', '--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_project_fail(self, runner, dropdb):
|
|
|
|
|
# Missing argument (project)
|
|
|
|
|
testproject = 'testproject'
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init'])
|
2020-07-29 21:03:00 +02:00
|
|
|
|
assert r.exit_code == 2
|
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
# Fail : Wrong project name
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', 'test*-project'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
# Fail : Already existing folder
|
|
|
|
|
os.mkdir(testproject)
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', testproject])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
# Fail : Already existing nod
|
|
|
|
|
os.mknod(testproject)
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', testproject])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
2020-09-25 01:06:21 +02:00
|
|
|
|
def test_init_project(self, runner, halfapi_conf_dir):
|
|
|
|
|
"""
|
|
|
|
|
"""
|
2020-08-05 11:10:14 +02:00
|
|
|
|
cp = ConfigParser()
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
env = {
|
2020-08-07 00:00:33 +02:00
|
|
|
|
'HALFAPI_CONF_DIR': halfapi_conf_dir
|
2020-08-05 11:10:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME], env=env)
|
2020-08-05 11:10:14 +02:00
|
|
|
|
try:
|
|
|
|
|
assert os.path.isdir(PROJNAME)
|
|
|
|
|
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# .halfapi/config check
|
|
|
|
|
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'config'))
|
|
|
|
|
cp.read(os.path.join(PROJNAME, '.halfapi', 'config'))
|
|
|
|
|
assert cp.has_section('project')
|
|
|
|
|
assert cp.has_option('project', 'name')
|
|
|
|
|
assert cp.get('project', 'name') == PROJNAME
|
|
|
|
|
assert cp.get('project', 'halfapi_version') == __version__
|
|
|
|
|
assert cp.has_section('domains')
|
2020-08-07 00:00:33 +02:00
|
|
|
|
except AssertionError as e:
|
2020-08-05 11:10:14 +02:00
|
|
|
|
subprocess.run(['tree', '-a', os.getcwd()])
|
2020-08-07 00:00:33 +02:00
|
|
|
|
raise e
|
2020-08-05 11:10:14 +02:00
|
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
assert res.exception is None
|