:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
"""Tests for auto-detecting test runner."""
from __future__ import annotations
import io
import contextlib
from pathlib import Path
import pytest
from unittest.mock import MagicMock, patch
import click
from click.testing import CliRunner
from hogli.cli import cli
from hogli_commands.test_runner import (
_batch_find_rs_cfg_test,
_detect_all,
_find_test_files_for_source,
_get_changed_files,
_hint_scoped_run,
_is_test_file,
_quiet_pytest_in_cloud_sandbox,
_resolve_to_repo_relative,
_run_changed,
_run_grouped,
_warn_if_dev_stack_is_down,
detect_test_type,
)
from parameterized import parameterized
runner = CliRunner()
class TestDetectTestType:
"""Tests hit real files on disk — they validate that detection works
end-to-end against the actual repo layout (package.json, Cargo.toml, go.mod).
"""
@parameterized.expand(
[
("posthog/api/test/test_user.py", "python", ["pytest", "-s", "posthog/api/test/test_user.py"]),
("posthog/models/test/test_team.py", "python", ["pytest", "-s", "posthog/models/test/test_team.py"]),
("ee/clickhouse/test/test_client.py", "python", ["pytest", "-s", "ee/clickhouse/test/test_client.py"]),
(
"products/alerts/backend/test/test_api.py",
"python",
["pytest", "-s", "products/alerts/backend/test/test_api.py"],
),
("tools/hogli/tests/test_cli.py", "python", ["pytest", "-s", "tools/hogli/tests/test_cli.py"]),
("dags/tests/test_dag.py", "python", ["pytest", "-s", "dags/tests/test_dag.py"]),
]
)
def test_python_tests(self, file_path: str, expected_type: str, expected_command: list[str]) -> None:
config = detect_test_type(file_path)
assert config.test_type == expected_type
assert config.command == expected_command
assert "REDIS_URL" in config.env
def test_python_with_node_id(self) -> None:
config = detect_test_type("posthog/api/test/test_user.py::TestUserAPI::test_retrieve")
assert config.test_type == "python"
assert config.command == ["pytest", "-s", "posthog/api/test/test_user.py::TestUserAPI::test_retrieve"]
# -- Jest tests: these hit real package.json files on disk --
@parameterized.expand(
[
("frontend/src/scenes/dashboard/Dashboard.test.tsx", "@posthog/frontend"),
("frontend/src/lib/utils.test.ts", "@posthog/frontend"),
]
)
def test_frontend_jest(self, file_path: str, expected_filter: str) -> None:
config = detect_test_type(file_path)
assert config.test_type == "jest"
assert config.command == ["pnpm", f"--filter={expected_filter}", "exec", "jest", file_path]
@parameterized.expand(
[
"products/ai_observability/frontend/utils.test.ts",
"products/llm_analytics/frontend/scenes/Trace.test.tsx",
]
)
def test_product_frontend_jest_routes_to_frontend(self, file_path: str) -> None:
config = detect_test_type(file_path)
assert config.test_type == "jest"
assert config.command == ["pnpm", "--filter=@posthog/frontend", "exec", "jest", file_path]
@parameterized.expand(
[
(
"products/desktop/packages/core/src/archive/archiveListView.test.ts",
"products/desktop/packages/core",
"src/archive/archiveListView.test.ts",
),
(
"products/desktop/packages/ui/src/shell/logCapture.test.ts",
"products/desktop/packages/ui",
"src/shell/logCapture.test.ts",
),
(
"packages/agent/packages/agent/src/utils/gateway.test.ts",
"packages/agent/packages/agent",
"src/utils/gateway.test.ts",
),
]
)
def test_desktop_test_file_routes_to_package_vitest(self, file_path: str, pkg_dir: str, rel: str) -> None:
config = detect_test_type(file_path)
assert config.test_type == "vitest"
assert config.command == ["pnpm", "--dir", pkg_dir, "exec", "vitest", "run", rel]
def test_desktop_node_id_adds_test_name_filter(self) -> None:
config = detect_test_type("products/desktop/packages/core/src/archive/archiveListView.test.ts::renders rows")
assert config.test_type == "vitest"
assert config.command == [
"pnpm",
"--dir",
"products/desktop/packages/core",
"exec",
"vitest",
"run",
"src/archive/archiveListView.test.ts",
"-t",
"renders rows",
]
@parameterized.expand(
[
(
"products/desktop/packages/core",
["pnpm", "--dir", "products/desktop/packages/core", "exec", "vitest", "run"],
),
(
"products/desktop/packages/core/src/archive",
["pnpm", "--dir", "products/desktop/packages/core", "exec", "vitest", "run", "src/archive"],
),
("products/desktop", ["pnpm", "--dir", "products/desktop", "test"]),
("products/desktop/packages", ["pnpm", "--dir", "products/desktop", "test"]),
("packages/agent", ["pnpm", "--dir", "packages/agent", "test"]),
("packages/agent/packages", ["pnpm", "--dir", "packages/agent", "test"]),
]
)
def test_desktop_directories_route_to_vitest_or_turbo(self, dir_path: str, expected_command: list[str]) -> None:
config = detect_test_type(dir_path)
assert config.test_type == "vitest"
assert config.command == expected_command
def test_jest_node_id_adds_test_name_pattern(self) -> None:
config = detect_test_type("frontend/src/lib/utils.test.ts::some test name")
assert config.test_type == "jest"
assert config.command == [
"pnpm",
"--filter=@posthog/frontend",
"exec",
"jest",
"frontend/src/lib/utils.test.ts",
"--testNamePattern",
"some test name",
]
def test_nodejs_jest(self) -> None:
config = detect_test_type("nodejs/tests/cdp/cdp-api.test.ts")
assert config.test_type == "jest"
assert config.command[:4] == ["pnpm", "--filter=@posthog/nodejs", "exec", "jest"]
@parameterized.expand(
[
("common/hogvm/typescript/src/__tests__/execute.test.ts", "@posthog/hogvm"),
("common/replay-shared/src/replay.test.ts", "@posthog/replay-shared"),
("common/replay-headless/src/render.test.ts", "@posthog/replay-headless"),
]
)
def test_common_jest_packages(self, file_path: str, expected_filter: str) -> None:
config = detect_test_type(file_path)
assert config.test_type == "jest"
assert config.command[:4] == ["pnpm", f"--filter={expected_filter}", "exec", "jest"]
def test_playwright(self) -> None:
config = detect_test_type("playwright/e2e/dashboards.spec.ts")
assert config.test_type == "playwright"
assert config.command == ["pnpm", "exec", "playwright", "test", "playwright/e2e/dashboards.spec.ts"]
# -- Rust tests: these hit real Cargo.toml files on disk --
def test_rust_workspace_crate(self) -> None:
config = detect_test_type("rust/capture/tests/events.rs")
assert config.test_type == "rust"
assert "--manifest-path=rust/Cargo.toml" in config.command
assert "-p" in config.command
assert "capture" in config.command
assert "--test" in config.command
assert "events" in config.command
def test_rust_standalone_cli(self) -> None:
config = detect_test_type("cli/src/main.rs")
assert config.test_type == "rust"
assert "--manifest-path=cli/Cargo.toml" in config.command
assert "--" not in config.command # main.rs has no module filter
def test_rust_standalone_funnel_udf(self) -> None:
config = detect_test_type("funnel-udf/src/lib.rs")
assert config.test_type == "rust"
assert "--manifest-path=funnel-udf/Cargo.toml" in config.command
assert "--" not in config.command # lib.rs has no module filter
def test_rust_single_file_filters_to_module(self) -> None:
config = detect_test_type("cli/src/utils/throttler.rs")
assert config.test_type == "rust"
assert "--manifest-path=cli/Cargo.toml" in config.command
assert config.command[-2:] == ["--", "utils::throttler"]
def test_rust_directory_filters_to_module(self) -> None:
config = detect_test_type("cli/src/utils")
assert config.test_type == "rust"
assert "--manifest-path=cli/Cargo.toml" in config.command
assert config.command[-2:] == ["--", "utils"]
def test_rust_node_id_filters_to_test(self) -> None:
config = detect_test_type("cli/src/utils/throttler.rs::test_new_creates_empty_throttler")
assert config.test_type == "rust"
assert "--manifest-path=cli/Cargo.toml" in config.command
assert config.command[-2:] == ["--", "utils::throttler::test_new_creates_empty_throttler"]
def test_rust_no_cargo_toml_raises(self) -> None:
with pytest.raises(click.UsageError, match="No Cargo.toml found"):
detect_test_type("random/thing.rs")
# -- Go tests: these hit real go.mod files on disk --
# Any .go file or go.mod should run tests from the module root
@parameterized.expand(
[
("livestream/main_test.go", "livestream", "./..."),
("livestream/main.go", "livestream", "./..."),
("livestream/handlers/handler.go", "livestream", "./handlers/..."),
("livestream/go.mod", "livestream", "./..."),
("tools/phrocs/internal/tui/app_test.go", "tools/phrocs", "./internal/tui/..."),
("bin/hobby-installer/installer_test.go", "bin/hobby-installer", "./..."),
]
)
def test_go_tests(self, file_path: str, expected_mod_root: str, expected_target: str) -> None:
config = detect_test_type(file_path)
assert config.test_type == "go"
assert config.command == ["go", "test", expected_target]
assert config.cwd == _get_repo_root() / expected_mod_root
def test_go_no_go_mod_raises(self) -> None:
with pytest.raises(click.UsageError, match="No go.mod found"):
detect_test_type("random/foo_test.go")
# -- Directory tests: run all tests in a directory --
@parameterized.expand(
[
("posthog/api/test", "python", ["pytest", "-s", "posthog/api/test"]),
("ee/hogai", "python", ["pytest", "-s", "ee/hogai"]),
(
"tools/hogli-commands/hogli_commands/tests",
"python",
["pytest", "-s", "tools/hogli-commands/hogli_commands/tests"],
),
]
)
def test_python_directory(self, dir_path: str, expected_type: str, expected_command: list[str]) -> None:
config = detect_test_type(dir_path)
assert config.test_type == expected_type
assert config.command == expected_command
def test_go_directory(self) -> None:
config = detect_test_type("livestream")
assert config.test_type == "go"
assert config.command == ["go", "test", "./..."]
assert config.cwd == _get_repo_root() / "livestream"
def test_go_subdirectory(self) -> None:
config = detect_test_type("livestream/events")
assert config.test_type == "go"
assert config.command == ["go", "test", "./events/..."]
assert config.cwd == _get_repo_root() / "livestream"
def test_rust_directory(self) -> None:
config = detect_test_type("rust/capture/tests")
assert config.test_type == "rust"
assert "--manifest-path=rust/Cargo.toml" in config.command
def test_jest_directory(self) -> None:
config = detect_test_type("frontend/src/scenes/dashboard")
assert config.test_type == "jest"
assert config.command[:4] == ["pnpm", "--filter=@posthog/frontend", "exec", "jest"]
def test_playwright_directory(self) -> None:
config = detect_test_type("playwright/e2e")
assert config.test_type == "playwright"
assert config.command == ["pnpm", "exec", "playwright", "test", "playwright/e2e"]
# -- Product directory (Turbo) tests --
def test_product_root_uses_turbo(self) -> None:
config = detect_test_type("products/alerts")
assert config.test_type == "turbo"
assert config.command == ["pnpm", "turbo", "run", "backend:test", "--filter=@posthog/products-alerts"]
def test_product_root_trailing_slash_uses_turbo(self) -> None:
config = detect_test_type("products/alerts/")
assert config.test_type == "turbo"
assert config.command == ["pnpm", "turbo", "run", "backend:test", "--filter=@posthog/products-alerts"]
def test_product_subdirectory_uses_pytest(self) -> None:
config = detect_test_type("products/error_tracking/backend/test")
assert config.test_type == "python"
assert config.command == ["pytest", "-s", "products/error_tracking/backend/test"]
# -- Edge cases --
def test_unknown_file_raises(self) -> None:
with pytest.raises(click.UsageError, match="Could not detect test type"):
detect_test_type("random/file.txt")
def test_unknown_python_location_raises(self) -> None:
with pytest.raises(click.UsageError, match="Could not detect test type"):
detect_test_type("somewhere/test_foo.py")
@patch("hogli_commands.test_runner.platform")
def test_macos_env_var(self, mock_platform) -> None:
mock_platform.system.return_value = "Darwin"
config = detect_test_type("posthog/api/test/test_user.py")
assert config.env["OBJC_DISABLE_INITIALIZE_FORK_SAFETY"] == "YES"
@patch("hogli_commands.test_runner.platform")
def test_linux_no_objc_env_var(self, mock_platform) -> None:
mock_platform.system.return_value = "Linux"
config = detect_test_type("posthog/api/test/test_user.py")
assert "OBJC_DISABLE_INITIALIZE_FORK_SAFETY" not in config.env
class TestResolveToRepoRelative:
def test_relative_path_unchanged(self) -> None:
with patch("hogli_commands.test_runner.Path.cwd", return_value=Path(str(_get_repo_root()))):
result = _resolve_to_repo_relative("posthog/api/test/test_user.py")
assert result == "posthog/api/test/test_user.py"
def test_absolute_path_made_relative(self) -> None:
repo_root = str(_get_repo_root())
result = _resolve_to_repo_relative(f"{repo_root}/posthog/api/test/test_user.py")
assert result == "posthog/api/test/test_user.py"
def test_node_id_preserved(self) -> None:
repo_root = str(_get_repo_root())
result = _resolve_to_repo_relative(f"{repo_root}/posthog/test_foo.py::TestBar::test_baz")
assert result == "posthog/test_foo.py::TestBar::test_baz"
def test_relative_with_node_id(self) -> None:
with patch("hogli_commands.test_runner.Path.cwd", return_value=Path(str(_get_repo_root()))):
result = _resolve_to_repo_relative("posthog/test_foo.py::TestBar")
assert result == "posthog/test_foo.py::TestBar"
@pytest.fixture
def source_tree(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
for rel in (
"posthog/api/comments.py",
"posthog/api/test/test_comments.py",
"frontend/src/scenes/dashboard/DashboardHeader.tsx",
"frontend/src/scenes/dashboard/DashboardHeader.test.tsx",
"livestream/events/filter.go",
"livestream/events/filter_test.go",
"tools/hogli-commands/hogli_commands/test_runner.py",
"tools/hogli-commands/hogli_commands/tests/test_test_runner.py",
):
(tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)
(tmp_path / rel).touch()
for rel in ("rust/capture/src/api.rs", "rust/capture/src/v1/util.rs"):
(tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)
(tmp_path / rel).write_text("#[cfg(test)]\nmod tests {}\n")
monkeypatch.setattr("hogli_commands.test_runner.REPO_ROOT", tmp_path)
class TestIsTestFile:
@parameterized.expand(
[
("posthog/api/test/test_user.py", True),
("products/posthog_ai/evals/sql/eval_sql.py", False),
("products/posthog_ai/eval_harness/test/test_discovery.py", True),
("posthog/eval_something.py", False),
("ee/hogai/router.py", False),
("posthog/models/team.py", False),
("frontend/src/scenes/dashboard/Dashboard.test.tsx", True),
("frontend/src/scenes/dashboard/Dashboard.tsx", False),
("playwright/e2e/dashboards.spec.ts", True),
("playwright/e2e/helpers.ts", False),
("rust/capture/tests/events.rs", True),
("rust/capture/src/api_test.rs", True),
("livestream/main_test.go", True),
("livestream/main.go", False),
]
)
def test_is_test_file(self, path: str, expected: bool) -> None:
assert _is_test_file(path) == expected
@parameterized.expand(
[
("rust/capture/src/api.rs",),
("rust/capture/src/v1/util.rs",),
]
)
@pytest.mark.usefixtures("source_tree")
def test_rs_inline_cfg_test_detected_with_batch(self, path: str) -> None:
rs_cfg_test = _batch_find_rs_cfg_test("rust/capture/src")
assert _is_test_file(path, rs_cfg_test=rs_cfg_test)
def test_rs_inline_cfg_test_not_detected_without_batch(self) -> None:
assert not _is_test_file("rust/capture/src/api.rs")
def test_rs_inline_cfg_test_absent_from_batch(self) -> None:
assert not _is_test_file("rust/capture/src/api.rs", rs_cfg_test=set())
class TestGetChangedFiles:
@patch("hogli_commands.test_runner.changed_files")
@patch("hogli_commands.test_runner.subprocess.run")
def test_drops_paths_missing_from_disk(self, mock_run: MagicMock, mock_changed: MagicMock) -> None:
mock_run.return_value = MagicMock(stdout="feature-branch\n")
mock_changed.return_value = [
"tools/hogli-commands/hogli_commands/test_runner.py",
"renamed_away/test_gone.py",
]
assert _get_changed_files() == ["tools/hogli-commands/hogli_commands/test_runner.py"]
@pytest.mark.usefixtures("source_tree")
class TestFindTestFilesForSource:
@parameterized.expand(
[
("posthog/api/comments.py", "posthog/api/test/test_comments.py"),
(
"tools/hogli-commands/hogli_commands/test_runner.py",
"tools/hogli-commands/hogli_commands/tests/test_test_runner.py",
),
(
"frontend/src/scenes/dashboard/DashboardHeader.tsx",
"frontend/src/scenes/dashboard/DashboardHeader.test.tsx",
),
("livestream/events/filter.go", "livestream/events/filter_test.go"),
]
)
def test_finds_test_for_source(self, source: str, expected_test: str) -> None:
results = _find_test_files_for_source(source)
assert expected_test in results
def test_non_source_file_returns_empty(self) -> None:
assert _find_test_files_for_source("README.md") == []
def test_nonexistent_file_returns_empty(self) -> None:
assert _find_test_files_for_source("posthog/api/nonexistent_module.py") == []
class TestRunChanged:
@patch("hogli_commands.test_runner._run")
@patch("hogli_commands.test_runner._get_changed_files")
def test_runs_changed_python_files(self, mock_changed, mock_run) -> None:
mock_changed.return_value = [
"posthog/api/test/test_user.py",
"posthog/api/test/test_comments.py",
"posthog/api/views.py", # not a test file
]
_run_changed([])
mock_run.assert_called_once()
command = mock_run.call_args[0][0]
assert command[0] == "pytest"
assert "posthog/api/test/test_user.py" in command
assert "posthog/api/test/test_comments.py" in command
assert "posthog/api/views.py" not in command
@patch("hogli_commands.test_runner._run")
@patch("hogli_commands.test_runner._get_changed_files")
def test_jest_files_grouped_by_package(self, mock_changed, mock_run) -> None:
mock_changed.return_value = [
"frontend/src/scenes/dashboard/Dashboard.test.tsx",
"frontend/src/lib/utils.test.ts",
"nodejs/tests/cdp/cdp-api.test.ts",
]
_run_changed([])
assert mock_run.call_count == 2
commands = [call[0][0] for call in mock_run.call_args_list]
# One call for @posthog/frontend, one for @posthog/nodejs
frontend_cmd = next(c for c in commands if "--filter=@posthog/frontend" in c)
nodejs_cmd = next(c for c in commands if "--filter=@posthog/nodejs" in c)
assert "frontend/src/scenes/dashboard/Dashboard.test.tsx" in frontend_cmd
assert "frontend/src/lib/utils.test.ts" in frontend_cmd
assert "nodejs/tests/cdp/cdp-api.test.ts" in nodejs_cmd
@patch("hogli_commands.test_runner._run")
@patch("hogli_commands.test_runner._get_changed_files")
@pytest.mark.usefixtures("source_tree")
def test_discovers_tests_for_changed_source_files(self, mock_changed, mock_run) -> None:
mock_changed.return_value = ["posthog/api/comments.py"]
_run_changed([])
mock_run.assert_called_once()
command = mock_run.call_args[0][0]
assert "posthog/api/test/test_comments.py" in command
@patch("hogli_commands.test_runner._run")
@patch("hogli_commands.test_runner._get_changed_files")
def test_deduplicates_direct_and_discovered_tests(self, mock_changed, mock_run) -> None:
mock_changed.return_value = [
"posthog/api/comments.py",
"posthog/api/test/test_comments.py",
]
_run_changed([])
mock_run.assert_called_once()
command = mock_run.call_args[0][0]
assert command.count("posthog/api/test/test_comments.py") == 1
@patch("hogli_commands.test_runner._run")
@patch("hogli_commands.test_runner._get_changed_files")
def test_skip_playwright_leaves_out_specs(self, mock_changed, mock_run) -> None:
mock_changed.return_value = ["playwright/e2e/dashboards.spec.ts", "posthog/api/test/test_user.py"]
_run_changed([], skip_playwright=True)
commands = [call[0][0] for call in mock_run.call_args_list]
assert len(commands) == 1
assert commands[0][0] == "pytest"
assert "playwright/e2e/dashboards.spec.ts" not in commands[0]
@patch("hogli_commands.test_runner._get_changed_files")
def test_no_changed_files_exits(self, mock_changed) -> None:
mock_changed.return_value = ["posthog/api/views.py"]
with pytest.raises(SystemExit):
_run_changed([])
@patch("hogli_commands.test_runner._get_changed_files")
def test_changed_on_master_raises(self, mock_changed) -> None:
mock_changed.side_effect = click.UsageError("Cannot use --changed on the master branch.")
with pytest.raises(click.UsageError, match="master"):
_run_changed([])
class TestRunGrouped:
@patch("hogli_commands.test_runner._run")
def test_mixed_python_and_jest(self, mock_run) -> None:
detected = _detect_all(
[
"posthog/api/test/test_user.py",
"frontend/src/scenes/dashboard/Dashboard.test.tsx",
]
)
_run_grouped(detected, [])
assert mock_run.call_count == 2
commands = [call[0][0] for call in mock_run.call_args_list]
python_cmd = next(c for c in commands if c[0] == "pytest")
jest_cmd = next(c for c in commands if c[0] == "pnpm")
assert "posthog/api/test/test_user.py" in python_cmd
assert "frontend/src/scenes/dashboard/Dashboard.test.tsx" in jest_cmd
@patch("hogli_commands.test_runner._run")
def test_passes_extra_args(self, mock_run) -> None:
detected = _detect_all(["posthog/api/test/test_user.py"])
_run_grouped(detected, ["-v", "--tb=short"])
command = mock_run.call_args[0][0]
assert "-v" in command
assert "--tb=short" in command
@patch("hogli_commands.test_runner._run")
def test_jest_grouped_by_package(self, mock_run) -> None:
detected = _detect_all(
[
"frontend/src/scenes/dashboard/Dashboard.test.tsx",
"frontend/src/lib/utils.test.ts",
"nodejs/tests/cdp/cdp-api.test.ts",
]
)
_run_grouped(detected, [])
assert mock_run.call_count == 2
commands = [call[0][0] for call in mock_run.call_args_list]
frontend_cmd = next(c for c in commands if "--filter=@posthog/frontend" in c)
nodejs_cmd = next(c for c in commands if "--filter=@posthog/nodejs" in c)
assert "frontend/src/scenes/dashboard/Dashboard.test.tsx" in frontend_cmd
assert "frontend/src/lib/utils.test.ts" in frontend_cmd
assert "nodejs/tests/cdp/cdp-api.test.ts" in nodejs_cmd
class TestCliPassthrough:
@patch("hogli_commands.test_runner._run")
def test_hogli_test_passes_unknown_options_to_runner(self, mock_run: MagicMock, monkeypatch) -> None:
monkeypatch.delenv("POSTHOG_TASK_RUN_ID", raising=False)
result = runner.invoke(
cli,
[
"test",
"tools/hogli-commands/hogli_commands/tests/test_commands.py",
"-k",
"cache",
],
)
assert result.exit_code == 0, result.output
command = mock_run.call_args[0][0]
assert command == [
"pytest",
"-s",
"tools/hogli-commands/hogli_commands/tests/test_commands.py",
"-k",
"cache",
]
def _get_repo_root():
from hogli.manifest import REPO_ROOT
return REPO_ROOT
class TestScopedRunHints:
@pytest.mark.parametrize(
"command, extra_args, env, expected",
[
(["pytest", "-s", "a.py"], [], {"POSTHOG_TASK_RUN_ID": "run-1"}, ["pytest", "-q", "a.py"]),
(["pytest", "-s", "a.py"], [], {}, ["pytest", "-s", "a.py"]),
(
["pytest", "-s", "a.py"],
[],
{"POSTHOG_TASK_RUN_ID": "run-1", "HOGLI_TEST_VERBOSE": "1"},
["pytest", "-s", "a.py"],
),
(["pytest", "-s", "a.py"], ["-vv"], {"POSTHOG_TASK_RUN_ID": "run-1"}, ["pytest", "-s", "a.py"]),
(["pytest", "-s", "a.py"], ["--capture=no"], {"POSTHOG_TASK_RUN_ID": "run-1"}, ["pytest", "-s", "a.py"]),
(
["pnpm", "exec", "jest", "a.test.ts"],
[],
{"POSTHOG_TASK_RUN_ID": "run-1"},
["pnpm", "exec", "jest", "a.test.ts"],
),
],
)
def test_pytest_is_quiet_only_in_a_cloud_sandbox(self, monkeypatch, command, extra_args, env, expected):
monkeypatch.delenv("POSTHOG_TASK_RUN_ID", raising=False)
monkeypatch.delenv("HOGLI_TEST_VERBOSE", raising=False)
for key, value in env.items():
monkeypatch.setenv(key, value)
assert _quiet_pytest_in_cloud_sandbox(command, extra_args) == expected
@pytest.mark.parametrize(
"file_count, in_sandbox, hinted",
[(3, True, False), (10, True, True), (10, False, False)],
)
def test_directory_runs_in_a_sandbox_suggest_a_scoped_run(
self, monkeypatch, capsys, file_count, in_sandbox, hinted
):
monkeypatch.delenv("HOGLI_TEST_VERBOSE", raising=False)
if in_sandbox:
monkeypatch.setenv("POSTHOG_TASK_RUN_ID", "run-1")
else:
monkeypatch.delenv("POSTHOG_TASK_RUN_ID", raising=False)
monkeypatch.setattr(
"hogli_commands.test_runner._find_test_files", lambda _path: [f"t{i}.py" for i in range(file_count)]
)
_hint_scoped_run("posthog/api/test")
assert ("hogli test --changed" in capsys.readouterr().out) is hinted
@parameterized.expand(
[
("database_down_on_a_baked_image_warns", ["pytest", "a.py"], True, True, False, True),
("seeded_database_ready_is_quiet", ["pytest", "a.py"], True, True, True, False),
("outside_a_sandbox_is_quiet", ["pytest", "a.py"], False, True, False, False),
("unbaked_image_is_quiet", ["pytest", "a.py"], True, False, False, False),
("non_pytest_runner_is_quiet", ["pnpm", "exec", "jest", "a.test.ts"], True, True, False, False),
],
)
def test_a_down_dev_stack_is_only_reported_where_the_advice_applies(
self, _name: str, command, in_sandbox, baked_image, database_ready, warned
):
stdout = io.StringIO()
with pytest.MonkeyPatch.context() as monkeypatch, contextlib.redirect_stdout(stdout):
if in_sandbox:
monkeypatch.setenv("POSTHOG_TASK_RUN_ID", "run-1")
else:
monkeypatch.delenv("POSTHOG_TASK_RUN_ID", raising=False)
monkeypatch.setattr("hogli_commands.test_runner._seeded_test_database_ready", lambda: database_ready)
monkeypatch.setattr(
"hogli_commands.test_runner._DEV_STACK_BAKE_MANIFEST", MagicMock(exists=lambda: baked_image)
)
_warn_if_dev_stack_is_down(command)
assert ("bootstrap-dev-stack" in stdout.getvalue()) is warned