From 2ff8fd9f975103fb32a273f9048135cfc84007bd Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Mon, 27 May 2024 11:58:53 -0400 Subject: [PATCH] add --host flag to docs serve --- core/dbt/cli/params.py | 8 ++++++++ core/dbt/task/docs/serve.py | 3 ++- tests/unit/task/docs/test_serve.py | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py index 0c24597a3bc..eb8e5594f76 100644 --- a/core/dbt/cli/params.py +++ b/core/dbt/cli/params.py @@ -135,6 +135,14 @@ is_flag=True, ) +host = click.option( + "--host", + envvar="DBT_HOST", + help="host to serve dbt docs on", + type=click.STRING, + default="127.0.0.1", +) + indirect_selection = click.option( "--indirect-selection", envvar="DBT_INDIRECT_SELECTION", diff --git a/core/dbt/task/docs/serve.py b/core/dbt/task/docs/serve.py index c0cd4e570ac..ce362eaeb4e 100644 --- a/core/dbt/task/docs/serve.py +++ b/core/dbt/task/docs/serve.py @@ -16,11 +16,12 @@ def run(self): shutil.copyfile(DOCS_INDEX_FILE_PATH, "index.html") port = self.args.port + host = self.args.host if self.args.browser: webbrowser.open_new_tab(f"http://localhost:{port}") - with socketserver.TCPServer(("127.0.0.1", port), SimpleHTTPRequestHandler) as httpd: + with socketserver.TCPServer((host, port), SimpleHTTPRequestHandler) as httpd: click.echo(f"Serving docs at {port}") click.echo(f"To access from your browser, navigate to: http://localhost:{port}") click.echo("\n\n") diff --git a/tests/unit/task/docs/test_serve.py b/tests/unit/task/docs/test_serve.py index cedb234a205..5a832341201 100644 --- a/tests/unit/task/docs/test_serve.py +++ b/tests/unit/task/docs/test_serve.py @@ -12,6 +12,7 @@ def serve_task(): task = ServeTask(config=MagicMock(), args=MagicMock()) task.config.project_target_path = "." task.args.port = 8000 + task.args.host = "127.0.0.1" return task @@ -21,3 +22,13 @@ def test_serve_bind_to_127(serve_task): patched_TCPServer.return_value = MagicMock() serve_task.run() patched_TCPServer.assert_called_once_with(("127.0.0.1", 8000), SimpleHTTPRequestHandler) + + +def test_serve_bind_to_all(serve_task): + serve_task.args.browser = False + serve_task.args.host = "" + + with patch("dbt.task.docs.serve.socketserver.TCPServer") as patched_TCPServer: + patched_TCPServer.return_value = MagicMock() + serve_task.run() + patched_TCPServer.assert_called_once_with(("", 8000), SimpleHTTPRequestHandler)