From 9fce00490725f1ab1579c3ef29be65bee44d9da4 Mon Sep 17 00:00:00 2001 From: Kegan Maher Date: Thu, 14 Nov 2024 02:20:47 +0000 Subject: [PATCH] refactor(time): convert uses env vars for IO targets this default matches the most common use case no need for helper script that encoded this same default --- bin/hours-convert.sh | 14 -------------- compiler_admin/main.py | 12 +++++++++--- tests/test_main.py | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 17 deletions(-) delete mode 100755 bin/hours-convert.sh diff --git a/bin/hours-convert.sh b/bin/hours-convert.sh deleted file mode 100755 index ee59fe6..0000000 --- a/bin/hours-convert.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -set -e - -read -p "Did you remember to check the project mapping? $TOGGL_PROJECT_INFO (Y/n): " check_project - -if [[ "$check_project" ~= ^[^Yy]$ ]]; then - echo "Check and then run this script again" - exit 1 -fi - -CMD="time convert" -OPTS="--input $TOGGL_DATA --output $HARVEST_DATA" - -exec compiler-admin $CMD $OPTS diff --git a/compiler_admin/main.py b/compiler_admin/main.py index 06f2006..b3632f9 100644 --- a/compiler_admin/main.py +++ b/compiler_admin/main.py @@ -69,10 +69,14 @@ def setup_time_command(cmd_parsers: _SubParsersAction): time_convert = add_sub_cmd(time_subcmds, "convert", help="Convert a time report from one format into another.") time_convert.add_argument( - "--input", default=sys.stdin, help="The path to the source data for conversion. Defaults to stdin." + "--input", + default=os.environ.get("TOGGL_DATA", sys.stdin), + help="The path to the source data for conversion. Defaults to $TOGGL_DATA or stdin.", ) time_convert.add_argument( - "--output", default=sys.stdout, help="The path to the file where converted data should be written. Defaults to stdout." + "--output", + default=os.environ.get("HARVEST_DATA", sys.stdout), + help="The path to the file where converted data should be written. Defaults to $HARVEST_DATA or stdout.", ) time_convert.add_argument("--client", default=None, help="The name of the client to use in converted data.") @@ -92,7 +96,9 @@ def setup_time_command(cmd_parsers: _SubParsersAction): help="The end date of the reporting period. Defaults to the end of the prior month.", ) time_download.add_argument( - "--output", default=sys.stdout, help="The path to the file where converted data should be written. Defaults to stdout." + "--output", + default=os.environ.get("TOGGL_DATA", sys.stdout), + help="The path to the file where downloaded data should be written. Defaults to $TOGGL_DATA or stdout.", ) time_download.add_argument( "--client", diff --git a/tests/test_main.py b/tests/test_main.py index 32a0f0c..5e7acaa 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -10,6 +10,12 @@ from compiler_admin.services.google import DOMAIN +@pytest.fixture(autouse=True) +def reset_env(monkeypatch): + monkeypatch.delenv("HARVEST_DATA", raising=False) + monkeypatch.delenv("TOGGL_DATA", raising=False) + + @pytest.fixture def mock_local_now(mocker): dt = datetime(2024, 9, 25, tzinfo=TZINFO) @@ -114,6 +120,20 @@ def test_main_time_convert_default(mock_commands_time): ) +def test_main_time_convert_env(monkeypatch, mock_commands_time): + monkeypatch.setenv("HARVEST_DATA", "harvest") + monkeypatch.setenv("TOGGL_DATA", "toggl") + + main(argv=["time", "convert"]) + + mock_commands_time.assert_called_once() + call_args = mock_commands_time.call_args.args + assert ( + Namespace(func=mock_commands_time, command="time", subcommand="convert", client=None, input="toggl", output="harvest") + in call_args + ) + + @pytest.mark.usefixtures("mock_local_now") def test_main_time_download_default(mock_commands_time, mock_start, mock_end): main(argv=["time", "download"])