Skip to content

Commit adab39c

Browse files
author
Esteban Echeverry
committed
Implementar y probar el componente main
1 parent cf4928d commit adab39c

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

taskit/__main__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from pathlib import Path
34
from taskit.application.coordinators.admin_coordinator import (
45
AdminCoordinator)
@@ -13,10 +14,9 @@
1314
from taskit.infrastructure.cli.taskit import cli, State
1415

1516

16-
def prepare_json_database() -> str:
17-
taskit_dir = Path.home().joinpath('.taskit')
18-
os.makedirs(str(taskit_dir), exist_ok=True)
19-
db_path = str(taskit_dir.joinpath('db.json'))
17+
def prepare_json_database(taskit_dir: str) -> str:
18+
os.makedirs(taskit_dir, exist_ok=True)
19+
db_path = os.sep.join([taskit_dir, 'db.json'])
2020
with open(db_path, 'a+') as f:
2121
f.seek(0)
2222
contents = f.read()
@@ -42,9 +42,17 @@ def build_state(json_file: str) -> State:
4242

4343

4444
def main():
45-
db_file = prepare_json_database()
45+
taskit_dir = (
46+
os.environ.get('TASKIT_DIR') or str(Path.home().joinpath('.taskit'))
47+
)
48+
db_file = prepare_json_database(taskit_dir)
4649
state = build_state(db_file)
47-
cli(obj=state)
50+
51+
try:
52+
cli(obj=state)
53+
except Exception as e:
54+
print("\nEXECUTION ERROR:", e)
55+
sys.exit(-1)
4856

4957

5058
if __name__ == '__main__':

tests/test_main.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1-
from taskit.__main__ import main
1+
import os
2+
import sys
3+
import taskit.__main__
4+
from subprocess import run
5+
from pytest import fixture
6+
from unittest.mock import Mock, patch
7+
from taskit.__main__ import main, build_state
28

39

4-
# def test_main():
5-
# main()
10+
@fixture(scope='module')
11+
def taskit_dir(tmpdir_factory: fixture) -> str:
12+
taskit_dir = tmpdir_factory.mktemp('taskitdir')
13+
return str(taskit_dir)
14+
15+
16+
def test_main(taskit_dir) -> None:
17+
sandbox = patch.dict(os.environ, {'TASKIT_DIR': taskit_dir})
18+
sandbox.start()
19+
python_bin = sys.executable
20+
result = run([python_bin, '-m', 'taskit'])
21+
sandbox.stop()
22+
assert result.returncode == 0
23+
24+
25+
def test_main_db_file_not_empty(taskit_dir) -> None:
26+
sandbox = patch.dict(os.environ, {'TASKIT_DIR': taskit_dir})
27+
sandbox.start()
28+
db_path = os.sep.join([taskit_dir, 'db.json'])
29+
with open(db_path, 'w+') as f:
30+
f.write('{"projects": {}}')
31+
python_bin = sys.executable
32+
result = run([python_bin, '-m', 'taskit'])
33+
sandbox.stop()
34+
assert result.returncode == 0
35+
36+
37+
def test_main_exception_handling(taskit_dir, monkeypatch) -> None:
38+
sandbox = patch.dict(os.environ, {'TASKIT_DIR': taskit_dir})
39+
sandbox.start()
40+
monkeypatch.setattr("taskit.__main__.build_state", lambda x: "NOTHING")
41+
python_bin = sys.executable
42+
result = run([python_bin, '-m', 'taskit', 'report', 'tasks'])
43+
sandbox.stop()
44+
assert result.returncode != 0

0 commit comments

Comments
 (0)