From ac7a8ba7899daf6c04f4bcfa4f18981ce93184ef Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 22 Apr 2026 21:14:22 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=AA=20[testing=20improvement]=20Ad?= =?UTF-8?q?d=20unit=20tests=20for=20determine=5Foutput=5Ftarget=20fallback?= =?UTF-8?q?=20behavior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds unit tests for the `determine_output_target` function in `codeconcat/main.py`. The tests cover: - Explicit output path provided. - Forced stdout flag. - Fallback to a file named after the source directory when running in a TTY. - Fallback to stdout when not running in a TTY (e.g., piped output). - Edge case where the source path name is empty (e.g., root directory). These tests use mocking to simulate TTY states and to isolate the function from its logger, ensuring deterministic results regardless of the execution environment. Co-authored-by: lguibr <39469905+lguibr@users.noreply.github.com> --- tests/test_output_target.py | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/test_output_target.py diff --git a/tests/test_output_target.py b/tests/test_output_target.py new file mode 100644 index 0000000..44191c4 --- /dev/null +++ b/tests/test_output_target.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +import sys +from pathlib import Path +from unittest.mock import patch +from codeconcat.main import determine_output_target + +def test_determine_output_target_explicit(): + """Test when explicit output is provided.""" + # We mock logging to avoid errors during tests if logging is not fully initialized + with patch("codeconcat.main.logger"): + output, to_stdout = determine_output_target("source", "explicit.md", False) + assert output == "explicit.md" + assert to_stdout is False + +def test_determine_output_target_force_stdout(): + """Test when force_stdout flag is set.""" + with patch("codeconcat.main.logger"): + output, to_stdout = determine_output_target("source", None, True) + assert output is None + assert to_stdout is True + +def test_determine_output_target_tty_fallback(): + """Test fallback to file when in a TTY.""" + with patch("sys.stdout.isatty", return_value=True), \ + patch("codeconcat.main.logger"): + output, to_stdout = determine_output_target("my_project", None, False) + # my_project is relative, so it should be resolved + expected_name = f"{Path('my_project').resolve().name}.md" + assert output == expected_name + assert to_stdout is False + +def test_determine_output_target_non_tty_fallback(): + """Test fallback to stdout when not in a TTY (piped).""" + with patch("sys.stdout.isatty", return_value=False), \ + patch("codeconcat.main.logger"): + output, to_stdout = determine_output_target("source", None, False) + assert output is None + assert to_stdout is True + +def test_determine_output_target_root_fallback(): + """Test fallback when source path name is empty (e.g. root).""" + with patch("sys.stdout.isatty", return_value=True), \ + patch("codeconcat.main.logger"): + # On Linux/macOS, Path("/").name is "" + output, to_stdout = determine_output_target("/", None, False) + assert output == "codebase.md" + assert to_stdout is False From c39c646f19aad98b06d4517af5c1b6b2d9dd2a8d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 22 Apr 2026 21:26:56 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20[testing=20improvement]=20Ad?= =?UTF-8?q?d=20tests=20for=20determine=5Foutput=5Ftarget=20fallback=20and?= =?UTF-8?q?=20fix=20linting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add unit tests in `tests/test_output_target.py` for `determine_output_target`. - Ensure new test file is formatted with `ruff`. - Resolve linting issues (unused import, unsorted imports). Co-authored-by: lguibr <39469905+lguibr@users.noreply.github.com> --- tests/test_output_target.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/test_output_target.py b/tests/test_output_target.py index 44191c4..e40ff28 100644 --- a/tests/test_output_target.py +++ b/tests/test_output_target.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- -import sys from pathlib import Path from unittest.mock import patch + from codeconcat.main import determine_output_target + def test_determine_output_target_explicit(): """Test when explicit output is provided.""" # We mock logging to avoid errors during tests if logging is not fully initialized @@ -12,6 +13,7 @@ def test_determine_output_target_explicit(): assert output == "explicit.md" assert to_stdout is False + def test_determine_output_target_force_stdout(): """Test when force_stdout flag is set.""" with patch("codeconcat.main.logger"): @@ -19,28 +21,28 @@ def test_determine_output_target_force_stdout(): assert output is None assert to_stdout is True + def test_determine_output_target_tty_fallback(): """Test fallback to file when in a TTY.""" - with patch("sys.stdout.isatty", return_value=True), \ - patch("codeconcat.main.logger"): + with patch("sys.stdout.isatty", return_value=True), patch("codeconcat.main.logger"): output, to_stdout = determine_output_target("my_project", None, False) # my_project is relative, so it should be resolved expected_name = f"{Path('my_project').resolve().name}.md" assert output == expected_name assert to_stdout is False + def test_determine_output_target_non_tty_fallback(): """Test fallback to stdout when not in a TTY (piped).""" - with patch("sys.stdout.isatty", return_value=False), \ - patch("codeconcat.main.logger"): + with patch("sys.stdout.isatty", return_value=False), patch("codeconcat.main.logger"): output, to_stdout = determine_output_target("source", None, False) assert output is None assert to_stdout is True + def test_determine_output_target_root_fallback(): """Test fallback when source path name is empty (e.g. root).""" - with patch("sys.stdout.isatty", return_value=True), \ - patch("codeconcat.main.logger"): + with patch("sys.stdout.isatty", return_value=True), patch("codeconcat.main.logger"): # On Linux/macOS, Path("/").name is "" output, to_stdout = determine_output_target("/", None, False) assert output == "codebase.md"