2020-07-29 21:03:00 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from click.testing import CliRunner
|
|
|
|
|
2020-08-04 12:34:58 +02:00
|
|
|
from halfapi import __version__
|
2020-07-29 21:03:00 +02:00
|
|
|
from halfapi.cli import cli
|
2020-08-04 14:23:35 +02:00
|
|
|
from configparser import ConfigParser
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def runner():
|
|
|
|
return CliRunner()
|
|
|
|
|
2020-08-04 14:23:35 +02:00
|
|
|
@pytest.mark.skip
|
2020-07-29 21:03:00 +02:00
|
|
|
def test_init_project(runner):
|
|
|
|
# Missing argument (project)
|
2020-08-04 12:34:58 +02:00
|
|
|
testproject = 'testproject'
|
2020-07-29 21:03:00 +02:00
|
|
|
r = runner.invoke(cli, ['init-project'])
|
|
|
|
assert r.exit_code == 2
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
# Fail : Wrong project name
|
|
|
|
r = runner.invoke(cli, ['init-project', 'test*-project'])
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
# Fail : Already existing folder
|
2020-08-04 12:34:58 +02:00
|
|
|
os.mkdir(testproject)
|
|
|
|
r = runner.invoke(cli, ['init-project', testproject])
|
2020-07-29 21:03:00 +02:00
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
# Fail : Already existing nod
|
2020-08-04 12:34:58 +02:00
|
|
|
os.mknod(testproject)
|
|
|
|
r = runner.invoke(cli, ['init-project', testproject])
|
2020-07-29 21:03:00 +02:00
|
|
|
assert r.exit_code == 1
|
|
|
|
|
2020-08-04 14:23:35 +02:00
|
|
|
|
|
|
|
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):
|
2020-07-29 21:03:00 +02:00
|
|
|
with runner.isolated_filesystem():
|
|
|
|
# Success : New repo
|
2020-08-04 12:34:58 +02:00
|
|
|
r = runner.invoke(cli, ['init-project', testproject])
|
2020-07-29 21:03:00 +02:00
|
|
|
assert r.exit_code == 0
|
2020-08-04 12:34:58 +02:00
|
|
|
assert os.path.isdir(testproject)
|
|
|
|
assert os.path.isdir(f'{testproject}/.git')
|
|
|
|
assert os.path.isdir(f'{testproject}/.halfapi')
|
2020-08-04 14:23:35 +02:00
|
|
|
domains_file = 'testproject/.halfapi/domains'
|
|
|
|
assert os.path.isfile(domains_file)
|
2020-08-04 12:34:58 +02:00
|
|
|
conf_file = 'testproject/.halfapi/config'
|
|
|
|
assert os.path.isfile(conf_file)
|
|
|
|
config = ConfigParser()
|
|
|
|
config.read([conf_file])
|
|
|
|
assert config.has_section('project')
|
|
|
|
assert config.has_option('project', 'name')
|
|
|
|
assert config.get('project', 'name') == testproject
|
2020-08-04 14:23:35 +02:00
|
|
|
assert config.get('project', 'halfapi_version') == __version__
|
2020-08-04 12:34:58 +02:00
|
|
|
|
2020-07-29 21:03:00 +02:00
|
|
|
|
2020-08-04 14:23:35 +02:00
|
|
|
@pytest.mark.skip
|
|
|
|
def test_init_project_clone_success(runner):
|
2020-07-29 21:03:00 +02:00
|
|
|
with runner.isolated_filesystem():
|
|
|
|
# Success : Cloned repo
|
|
|
|
import pygit2
|
|
|
|
pygit2.init_repository('testproject.git', bare=True)
|
|
|
|
assert os.path.isdir('testproject.git')
|
|
|
|
|
2020-08-04 12:34:58 +02:00
|
|
|
r = runner.invoke(cli, [
|
|
|
|
'init-project', testproject, '--repo',
|
|
|
|
f'./{testproject}.git'])
|
2020-07-29 21:03:00 +02:00
|
|
|
assert r.exit_code == 0
|
2020-08-04 12:34:58 +02:00
|
|
|
assert os.path.isdir(testproject)
|
2020-07-29 21:03:00 +02:00
|
|
|
assert os.path.isdir('testproject/.git')
|