diff --git a/falyx/__main__.py b/falyx/__main__.py index 7832d45..67f422c 100644 --- a/falyx/__main__.py +++ b/falyx/__main__.py @@ -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) diff --git a/falyx/init.py b/falyx/init.py index caa4725..6b8857c 100644 --- a/falyx/init.py +++ b/falyx/init.py @@ -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 = """\ diff --git a/falyx/version.py b/falyx/version.py index 74acd0e..3cb7d95 100644 --- a/falyx/version.py +++ b/falyx/version.py @@ -1 +1 @@ -__version__ = "0.1.12" +__version__ = "0.1.13" diff --git a/pyproject.toml b/pyproject.toml index 83869b3..abf3d5c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT" diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..69f5dfd --- /dev/null +++ b/tests/test_main.py @@ -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()