|
| 1 | +import subprocess |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from socketsecurity.config import ( |
| 6 | + COMMIT_MESSAGE_TRUNCATION_MARKER, |
| 7 | + MAX_COMMIT_MESSAGE_LENGTH, |
| 8 | + CliConfig, |
| 9 | + truncate_commit_message, |
| 10 | +) |
| 11 | +from socketsecurity.socketcli import apply_git_context |
| 12 | + |
| 13 | + |
| 14 | +def _git(path, *args): |
| 15 | + return subprocess.run( |
| 16 | + ["git", *args], |
| 17 | + cwd=path, |
| 18 | + check=True, |
| 19 | + capture_output=True, |
| 20 | + text=True, |
| 21 | + ).stdout.strip() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def repo_with_large_commit_message(tmp_path): |
| 26 | + """A checkout whose HEAD commit message is far larger than the cap (~14 KB).""" |
| 27 | + path = tmp_path / "repo" |
| 28 | + path.mkdir() |
| 29 | + _git(path, "init", "-b", "main") |
| 30 | + _git(path, "config", "user.name", "Socket Test") |
| 31 | + _git(path, "config", "user.email", "socket@example.com") |
| 32 | + (path / "package.json").write_text("{}\n", encoding="utf-8") |
| 33 | + _git(path, "add", "package.json") |
| 34 | + _git(path, "commit", "-m", "Release notes\n\n" + ("- bumped a dependency\n" * 700)) |
| 35 | + return path |
| 36 | + |
| 37 | + |
| 38 | +class TestTruncateCommitMessage: |
| 39 | + def test_none_passes_through(self): |
| 40 | + assert truncate_commit_message(None) is None |
| 41 | + |
| 42 | + def test_empty_passes_through(self): |
| 43 | + assert truncate_commit_message("") == "" |
| 44 | + |
| 45 | + def test_under_limit_is_unchanged(self): |
| 46 | + msg = "a normal short commit message" |
| 47 | + assert truncate_commit_message(msg) == msg |
| 48 | + |
| 49 | + def test_at_limit_is_unchanged(self): |
| 50 | + msg = "a" * MAX_COMMIT_MESSAGE_LENGTH |
| 51 | + assert truncate_commit_message(msg) == msg |
| 52 | + |
| 53 | + def test_over_limit_is_capped(self): |
| 54 | + capped = truncate_commit_message("a" * 14_000) |
| 55 | + assert len(capped) == MAX_COMMIT_MESSAGE_LENGTH |
| 56 | + assert capped.endswith(COMMIT_MESSAGE_TRUNCATION_MARKER) |
| 57 | + |
| 58 | + def test_marker_fits_inside_the_limit(self): |
| 59 | + # The marker replaces the tail rather than extending past it, so the capped value |
| 60 | + # never grows the request line beyond what the proxy accepts. |
| 61 | + assert truncate_commit_message("a" * 201) == ( |
| 62 | + "a" * (MAX_COMMIT_MESSAGE_LENGTH - len(COMMIT_MESSAGE_TRUNCATION_MARKER)) |
| 63 | + + COMMIT_MESSAGE_TRUNCATION_MARKER |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +class TestCliConfigInvariant: |
| 68 | + def test_direct_construction_is_capped(self): |
| 69 | + config = CliConfig(api_token="test", repo="widgets", commit_message="a" * 14_000) |
| 70 | + assert len(config.commit_message) == MAX_COMMIT_MESSAGE_LENGTH |
| 71 | + |
| 72 | + def test_config_file_value_is_capped(self, tmp_path): |
| 73 | + config_file = tmp_path / "socketcli.json" |
| 74 | + config_file.write_text('{"commit_message": "%s"}' % ("a" * 14_000), encoding="utf-8") |
| 75 | + config = CliConfig.from_args(["--api-token", "test", "--config", str(config_file)]) |
| 76 | + assert len(config.commit_message) == MAX_COMMIT_MESSAGE_LENGTH |
| 77 | + |
| 78 | + |
| 79 | +class TestGitBackfill: |
| 80 | + def test_message_read_from_git_is_capped(self, repo_with_large_commit_message): |
| 81 | + config = CliConfig(api_token="test", repo=None, target_path=str(repo_with_large_commit_message)) |
| 82 | + assert config.commit_message is None |
| 83 | + |
| 84 | + is_repo, git_repo = apply_git_context(config) |
| 85 | + |
| 86 | + assert is_repo is True |
| 87 | + # The repository really does carry an oversized message; the cap is what keeps it |
| 88 | + # out of the full-scan query string. |
| 89 | + assert len(git_repo.commit_message) > 14_000 |
| 90 | + assert len(config.commit_message) == MAX_COMMIT_MESSAGE_LENGTH |
| 91 | + assert config.commit_message.startswith("Release notes") |
| 92 | + assert config.commit_message.endswith(COMMIT_MESSAGE_TRUNCATION_MARKER) |
| 93 | + |
| 94 | + def test_explicit_message_is_not_overwritten_by_git(self, repo_with_large_commit_message): |
| 95 | + config = CliConfig( |
| 96 | + api_token="test", |
| 97 | + repo=None, |
| 98 | + target_path=str(repo_with_large_commit_message), |
| 99 | + commit_message="explicit message", |
| 100 | + ) |
| 101 | + |
| 102 | + apply_git_context(config) |
| 103 | + |
| 104 | + assert config.commit_message == "explicit message" |
| 105 | + |
| 106 | + def test_non_repo_path_reports_no_repo(self, tmp_path): |
| 107 | + config = CliConfig(api_token="test", repo=None, target_path=str(tmp_path)) |
| 108 | + |
| 109 | + is_repo, git_repo = apply_git_context(config) |
| 110 | + |
| 111 | + assert is_repo is False |
| 112 | + assert git_repo is None |
| 113 | + assert config.ignore_commit_files is True |
0 commit comments