Add tests for __main__.py
This commit is contained in:
parent
a90c447d5c
commit
6f159810b2
|
@ -9,6 +9,7 @@ import asyncio
|
|||
import sys
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from falyx.config import loader
|
||||
from falyx.falyx import Falyx
|
||||
|
@ -20,8 +21,11 @@ def find_falyx_config() -> Path | None:
|
|||
Path.cwd() / "falyx.yaml",
|
||||
Path.cwd() / "falyx.toml",
|
||||
Path.cwd() / ".falyx.yaml",
|
||||
Path.cwd() / ".falyx.toml",
|
||||
Path.home() / ".config" / "falyx" / "falyx.yaml",
|
||||
Path.home() / ".config" / "falyx" / "falyx.toml",
|
||||
Path.home() / ".falyx.yaml",
|
||||
Path.home() / ".falyx.toml",
|
||||
]
|
||||
return next((p for p in candidates if p.exists()), None)
|
||||
|
||||
|
@ -33,7 +37,7 @@ def bootstrap() -> Path | None:
|
|||
return config_path
|
||||
|
||||
|
||||
def parse_args() -> Namespace:
|
||||
def get_falyx_parsers() -> FalyxParsers:
|
||||
falyx_parsers: FalyxParsers = get_arg_parsers()
|
||||
init_parser = falyx_parsers.subparsers.add_parser(
|
||||
"init", help="Create a new Falyx CLI project"
|
||||
|
@ -43,12 +47,10 @@ def parse_args() -> Namespace:
|
|||
"init-global", help="Set up ~/.config/falyx with example tasks"
|
||||
)
|
||||
|
||||
return falyx_parsers.parse_args()
|
||||
return falyx_parsers
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
|
||||
def main(args: Namespace) -> Any:
|
||||
if args.command == "init":
|
||||
from falyx.init import init_project
|
||||
|
||||
|
@ -72,8 +74,10 @@ def main() -> None:
|
|||
columns=4,
|
||||
)
|
||||
flx.add_commands(loader(bootstrap_path))
|
||||
asyncio.run(flx.run())
|
||||
return asyncio.run(flx.run())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
parsers = get_falyx_parsers()
|
||||
args = parsers.parse_args()
|
||||
main(args)
|
||||
|
|
|
@ -6,9 +6,11 @@ from rich.console import Console
|
|||
TEMPLATE_TASKS = """\
|
||||
async def build():
|
||||
print("🔨 Building project...")
|
||||
return "Build complete!"
|
||||
|
||||
async def test():
|
||||
print("🧪 Running tests...")
|
||||
return "Tests complete!"
|
||||
"""
|
||||
|
||||
TEMPLATE_CONFIG = """\
|
||||
|
|
|
@ -1 +1 @@
|
|||
__version__ = "0.1.12"
|
||||
__version__ = "0.1.13"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[tool.poetry]
|
||||
name = "falyx"
|
||||
version = "0.1.12"
|
||||
version = "0.1.13"
|
||||
description = "Reliable and introspectable async CLI action framework."
|
||||
authors = ["Roland Thomas Jr <roland@rtj.dev>"]
|
||||
license = "MIT"
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from falyx.__main__ import bootstrap, find_falyx_config, get_falyx_parsers, main
|
||||
|
||||
|
||||
def test_find_falyx_config():
|
||||
"""Test if the falyx config file is found correctly."""
|
||||
|
||||
config_file = Path("falyx.yaml").resolve()
|
||||
config_file.touch()
|
||||
config_path = find_falyx_config()
|
||||
assert config_path == config_file
|
||||
config_file.unlink()
|
||||
|
||||
|
||||
def test_bootstrap():
|
||||
"""Test if the bootstrap function works correctly."""
|
||||
config_file = Path("falyx.yaml").resolve()
|
||||
config_file.touch()
|
||||
sys_path_before = list(sys.path)
|
||||
bootstrap_path = bootstrap()
|
||||
assert bootstrap_path == config_file
|
||||
assert str(config_file.parent) in sys.path
|
||||
config_file.unlink()
|
||||
sys.path = sys_path_before
|
||||
|
||||
|
||||
def test_bootstrap_no_config():
|
||||
"""Test if the bootstrap function works correctly when no config file is found."""
|
||||
sys_path_before = list(sys.path)
|
||||
bootstrap_path = bootstrap()
|
||||
assert bootstrap_path is None
|
||||
assert str(Path.cwd()) in sys.path
|
||||
sys.path = sys_path_before
|
||||
|
||||
|
||||
def test_bootstrap_with_global_config():
|
||||
"""Test if the bootstrap function works correctly when a global config file is found."""
|
||||
config_file = Path.home() / ".config" / "falyx" / "falyx.yaml"
|
||||
config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config_file.touch()
|
||||
sys_path_before = list(sys.path)
|
||||
bootstrap_path = bootstrap()
|
||||
assert bootstrap_path == config_file
|
||||
assert str(config_file.parent) in sys.path
|
||||
config_file.unlink()
|
||||
sys.path = sys_path_before
|
||||
|
||||
|
||||
def test_parse_args():
|
||||
"""Test if the parse_args function works correctly."""
|
||||
falyx_parsers = get_falyx_parsers()
|
||||
args = falyx_parsers.parse_args(["init", "test_project"])
|
||||
|
||||
assert args.command == "init"
|
||||
assert args.name == "test_project"
|
||||
|
||||
args = falyx_parsers.parse_args(["init-global"])
|
||||
assert args.command == "init-global"
|
||||
|
||||
|
||||
def test_main():
|
||||
"""Test if the main function works correctly."""
|
||||
falyx_parsers = get_falyx_parsers()
|
||||
args = falyx_parsers.parse_args(["init", "test_project"])
|
||||
main(args)
|
||||
assert args.command == "init"
|
||||
assert args.name == "test_project"
|
||||
# Check if the project directory was created
|
||||
assert Path("test_project").exists()
|
||||
# Clean up
|
||||
(Path("test_project") / "falyx.yaml").unlink()
|
||||
(Path("test_project") / "tasks.py").unlink()
|
||||
Path("test_project").rmdir()
|
||||
# Test init-global
|
||||
args = falyx_parsers.parse_args(["init-global"])
|
||||
main(args)
|
||||
# Check if the global config directory was created
|
||||
assert (Path.home() / ".config" / "falyx" / "falyx.yaml").exists()
|
||||
# Clean up
|
||||
(Path.home() / ".config" / "falyx" / "falyx.yaml").unlink()
|
||||
(Path.home() / ".config" / "falyx" / "tasks.py").unlink()
|
||||
(Path.home() / ".config" / "falyx").rmdir()
|
||||
|
||||
|
||||
def test_no_bootstrap():
|
||||
"""Test if the main function works correctly when no config file is found."""
|
||||
falyx_parsers = get_falyx_parsers()
|
||||
args = falyx_parsers.parse_args(["list"])
|
||||
assert main(args) is None
|
||||
# Check if the task was run
|
||||
assert args.command == "list"
|
||||
|
||||
|
||||
def test_run_test_project():
|
||||
"""Test if the main function works correctly with a test project."""
|
||||
falyx_parsers = get_falyx_parsers()
|
||||
args = falyx_parsers.parse_args(["init", "test_project"])
|
||||
main(args)
|
||||
|
||||
args = falyx_parsers.parse_args(["run", "B"])
|
||||
os.chdir("test_project")
|
||||
with pytest.raises(SystemExit):
|
||||
assert main(args) == "Build complete!"
|
||||
os.chdir("..")
|
||||
shutil.rmtree("test_project")
|
||||
assert not Path("test_project").exists()
|
Loading…
Reference in New Issue