diff --git a/duckdb/query_graph/__main__.py b/duckdb/query_graph/__main__.py index dd4ff9591..8fd66890c 100644 --- a/duckdb/query_graph/__main__.py +++ b/duckdb/query_graph/__main__.py @@ -642,7 +642,12 @@ def main() -> None: # noqa: D103 ) parser.add_argument("--profile_input", help="profile input in json") parser.add_argument("--out", required=False, default=False) - parser.add_argument("--open", required=False, action="store_true", default=True) + parser.add_argument( + "--open", + action=argparse.BooleanOptionalAction, + default=True, + help="Open the generated HTML in the browser (default: enabled).", + ) args = parser.parse_args() input = args.profile_input diff --git a/tests/fast/test_query_graph_cli.py b/tests/fast/test_query_graph_cli.py new file mode 100644 index 000000000..68b5a5879 --- /dev/null +++ b/tests/fast/test_query_graph_cli.py @@ -0,0 +1,30 @@ +import sys +from unittest.mock import Mock + +import pytest + +from duckdb.query_graph import __main__ as query_graph + + +@pytest.mark.parametrize( + ("options", "opens_browser"), + [((), True), (("--open",), True), (("--no-open",), False)], +) +def test_query_graph_browser_option(monkeypatch, tmp_path, options, opens_browser): + source = str(tmp_path / "profile.json") + output = tmp_path / "profile.html" + profile = Mock() + profiling_info = Mock(return_value=profile) + open_browser = Mock() + monkeypatch.setattr(query_graph, "ProfilingInfo", profiling_info) + monkeypatch.setattr(query_graph.webbrowser, "open", open_browser) + monkeypatch.setattr(sys, "argv", ["query_graph", "--profile_input", source, *options]) + + query_graph.main() + + profiling_info.assert_called_once_with(from_file=source) + profile.to_html.assert_called_once_with(output_file=str(output)) + if opens_browser: + open_browser.assert_called_once_with(output.as_uri(), new=2) + else: + open_browser.assert_not_called()