diff --git a/hey/cli.py b/hey/cli.py index 4c1317b..8d6df06 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -45,6 +45,9 @@ def main( if ctx.invoked_subcommand is None: user_input = open_tmp_editor() + if not user_input.strip(): + console.print("No input provided.") + raise typer.Exit(code=1) markdown_input = Markdown( user_input, code_theme=configs.get("code_block_theme") ) @@ -74,6 +77,9 @@ def ask( Ask Hey directly in-command. """ + if not user_input.strip(): + console.print("No input provided.") + raise typer.Exit(code=1) result = answer(user_input, no_style) console.print(result) diff --git a/hey/configs/cli.py b/hey/configs/cli.py index bfd6264..625715c 100644 --- a/hey/configs/cli.py +++ b/hey/configs/cli.py @@ -15,7 +15,7 @@ def create(): """Create a base config file.""" if not config_exists(): - os.makedirs(APP_CONFIG_DIR) + os.makedirs(APP_CONFIG_DIR, exist_ok=True) init_config() else: if Confirm.ask( diff --git a/hey/configs/utils.py b/hey/configs/utils.py index 5d522cf..16619b3 100644 --- a/hey/configs/utils.py +++ b/hey/configs/utils.py @@ -28,7 +28,8 @@ def read_configs() -> dict: def init_config(): - with open(CONFIG_FILE_PATH, "w+") as conf_file: + os.makedirs(APP_CONFIG_DIR, exist_ok=True) + with open(CONFIG_FILE_PATH, "w+", encoding="utf-8") as conf_file: conf_file.write(json.dumps(BASE_CONFIG, indent=4)) diff --git a/hey/openai.py b/hey/openai.py index 876592e..24cef21 100644 --- a/hey/openai.py +++ b/hey/openai.py @@ -38,7 +38,7 @@ def __init__( self.prompt = prompt def ask(self, text: str) -> str: - if not text: + if not text or not text.strip(): raise ValueError("provide a valid input.") try: client_mindsdb_serve = OpenAI( diff --git a/tests/test_configs_utils.py b/tests/test_configs_utils.py new file mode 100644 index 0000000..fcd0b80 --- /dev/null +++ b/tests/test_configs_utils.py @@ -0,0 +1,19 @@ +import json +from pathlib import Path + +from hey.configs import utils +from hey.consts import BASE_CONFIG + + +def test_init_config_creates_directory_and_file(tmp_path, monkeypatch): + config_dir = tmp_path / "hey" + config_file = config_dir / "config.json" + + monkeypatch.setattr(utils, "APP_CONFIG_DIR", config_dir) + monkeypatch.setattr(utils, "CONFIG_FILE_PATH", config_file) + + utils.init_config() + + assert config_file.exists() + loaded = json.loads(config_file.read_text(encoding="utf-8")) + assert loaded == BASE_CONFIG