[cli][init-project][tests][wip] creation du projet halfapi
This commit is contained in:
parent
74c2c4e056
commit
b34631cdd0
|
@ -6,14 +6,34 @@ import os
|
|||
import sys
|
||||
import re
|
||||
import importlib
|
||||
import logging
|
||||
from pprint import pprint
|
||||
from configparser import ConfigParser
|
||||
|
||||
from halfapi import __version__
|
||||
from halfapi.cli.lib.db import ProjectDB
|
||||
|
||||
CONTEXT_SETTINGS={
|
||||
CONTEXT_SETTINGS = {
|
||||
'default_map':{'run': {}}
|
||||
}
|
||||
|
||||
TMPL_HALFAPI_ETC = """Insert this into the HALFAPI_CONF_DIR/{project} file
|
||||
|
||||
[project]
|
||||
host = 127.0.0.1
|
||||
port = 8000
|
||||
secret = /path/to/secret_file
|
||||
production = False
|
||||
base_dir = {base_dir}
|
||||
"""
|
||||
|
||||
TMPL_HALFAPI_CONFIG = """[project]
|
||||
name = {name}
|
||||
halfapi_version = {halfapi_version}
|
||||
"""
|
||||
|
||||
logger = logging.getLogger('halfapi')
|
||||
|
||||
@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
|
||||
@click.option('--version', is_flag=True)
|
||||
@click.pass_context
|
||||
|
@ -336,19 +356,31 @@ def init_project(project, repo):
|
|||
click.echo(f'A file named {project} already exists, abort.', err=True)
|
||||
sys.exit(1)
|
||||
|
||||
if repo is not None:
|
||||
click.echo(f'Clone URL {repo} in directory {project}')
|
||||
pygit2.clone_repository(
|
||||
url=repo,
|
||||
path=project
|
||||
)
|
||||
|
||||
else:
|
||||
click.echo(f'Initialize project repository in directory {project}')
|
||||
pygit2.init_repository(project)
|
||||
click.echo(f'Initialize project repository in directory {project}')
|
||||
pygit2.init_repository(project)
|
||||
|
||||
try:
|
||||
pdb = ProjectDB(project)
|
||||
pdb.init()
|
||||
except Exception as e:
|
||||
logger.warning(e)
|
||||
logger.debug(os.environ.get('HALFORM_CONF_DIR'))
|
||||
raise e
|
||||
|
||||
os.mkdir(os.path.join(project, '.halfapi'))
|
||||
open(os.path.join(project, '.halfapi', 'domains'), 'w').write('')
|
||||
config_file = os.path.join(project, '.halfapi', 'config')
|
||||
with open(config_file, 'w') as f:
|
||||
f.write(TMPL_HALFAPI_CONFIG.format(
|
||||
name=project,
|
||||
halfapi_version=__version__
|
||||
))
|
||||
|
||||
print(TMPL_HALFAPI_ETC.format(
|
||||
project=project,
|
||||
base_dir=os.path.abspath(project)
|
||||
))
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
|
|
|
@ -111,3 +111,5 @@ class ProjectDB:
|
|||
"""
|
||||
"""
|
||||
self.__db.execute_query(_DB_SCHEMA)
|
||||
self.__db._connection.close()
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
[pytest]
|
||||
testpaths = tests halfapi
|
||||
addopts = --doctest-modules
|
||||
addopts = --doctest-modules -rP --log-level debug
|
||||
doctest_optionflags = ELLIPSIS
|
||||
|
|
|
@ -6,11 +6,13 @@ from click.testing import CliRunner
|
|||
|
||||
from halfapi import __version__
|
||||
from halfapi.cli import cli
|
||||
from configparser import ConfigParser
|
||||
|
||||
@pytest.fixture
|
||||
def runner():
|
||||
return CliRunner()
|
||||
|
||||
@pytest.mark.skip
|
||||
def test_init_project(runner):
|
||||
# Missing argument (project)
|
||||
testproject = 'testproject'
|
||||
|
@ -34,6 +36,27 @@ def test_init_project(runner):
|
|||
r = runner.invoke(cli, ['init-project', testproject])
|
||||
assert r.exit_code == 1
|
||||
|
||||
|
||||
with runner.isolated_filesystem():
|
||||
# Fail : No rights in /etc
|
||||
r = runner.invoke(cli, ['init-project', testproject], env={
|
||||
'HALFORM_CONF_DIR':'/etc/half_orm'
|
||||
})
|
||||
assert r.exit_code == 1
|
||||
assert type(r.exception) is PermissionError
|
||||
|
||||
testproject = 'testproject'
|
||||
|
||||
import subprocess
|
||||
@pytest.fixture
|
||||
def dropdb():
|
||||
subprocess.run(['/usr/bin/dropdb', f'halfapi_{testproject}'])
|
||||
|
||||
yield
|
||||
|
||||
return subprocess.run(['/usr/bin/dropdb', f'halfapi_{testproject}'])
|
||||
|
||||
def test_init_project_success(runner, dropdb):
|
||||
with runner.isolated_filesystem():
|
||||
# Success : New repo
|
||||
r = runner.invoke(cli, ['init-project', testproject])
|
||||
|
@ -41,8 +64,8 @@ def test_init_project(runner):
|
|||
assert os.path.isdir(testproject)
|
||||
assert os.path.isdir(f'{testproject}/.git')
|
||||
assert os.path.isdir(f'{testproject}/.halfapi')
|
||||
domain_file = 'testproject/.halfapi/domain'
|
||||
assert os.path.isfile(domain_file)
|
||||
domains_file = 'testproject/.halfapi/domains'
|
||||
assert os.path.isfile(domains_file)
|
||||
conf_file = 'testproject/.halfapi/config'
|
||||
assert os.path.isfile(conf_file)
|
||||
config = ConfigParser()
|
||||
|
@ -50,9 +73,11 @@ def test_init_project(runner):
|
|||
assert config.has_section('project')
|
||||
assert config.has_option('project', 'name')
|
||||
assert config.get('project', 'name') == testproject
|
||||
assert config.has_option('project', 'halfapi_version') == __version__
|
||||
assert config.get('project', 'halfapi_version') == __version__
|
||||
|
||||
|
||||
@pytest.mark.skip
|
||||
def test_init_project_clone_success(runner):
|
||||
with runner.isolated_filesystem():
|
||||
# Success : Cloned repo
|
||||
import pygit2
|
||||
|
|
Loading…
Reference in New Issue