|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +import unittest |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +REPO_ROOT = Path(__file__).resolve().parents[3] |
| 18 | + |
| 19 | +# Fixture content, not real references. The linters under test would otherwise find |
| 20 | +# their own bait here and check it against this repo. |
| 21 | +BAD = "[broken](sub/missing.md)\nhttps://example.invalid/dead\n" # @lint-ignore |
| 22 | +GOOD = "[fine](sub/present.md)\nhttps://example.invalid/live\n" # @lint-ignore |
| 23 | + |
| 24 | +OVERSIZED = "x" * (1024 * 1024 + 1) |
| 25 | + |
| 26 | +CURL_STUB = """#!/bin/sh |
| 27 | +for arg in "$@"; do url=$arg; done |
| 28 | +case "$url" in *dead*) echo 404 ;; *) echo 200 ;; esac |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.skipif(sys.platform == "win32", reason="The scripts under test need bash") |
| 33 | +class TestLinkCheckDiffSelection(unittest.TestCase): |
| 34 | + """A branch cut before recent base commits keeps the base branch's old lines. Those |
| 35 | + are not the branch's to answer for, so the linters must diff from the merge base. |
| 36 | +
|
| 37 | + Each fixture file pins a different part of that: base_only.md the file selection, |
| 38 | + both_sides.md the per-file diff, big.bin the file size range, feature_only.md that |
| 39 | + the branch's own additions are still checked at all. |
| 40 | + """ |
| 41 | + |
| 42 | + def setUp(self): |
| 43 | + self.repo = Path(tempfile.mkdtemp()) |
| 44 | + self.addCleanup(shutil.rmtree, self.repo, ignore_errors=True) |
| 45 | + self.git("init", "-q") |
| 46 | + self.git("symbolic-ref", "HEAD", "refs/heads/main") |
| 47 | + self.git("config", "user.email", "test@example.com") |
| 48 | + self.git("config", "user.name", "test") |
| 49 | + |
| 50 | + self.write("sub/present.md", "target\n") |
| 51 | + self.write("base_only.md", BAD) |
| 52 | + self.write("both_sides.md", BAD) |
| 53 | + self.write("big.bin", OVERSIZED) |
| 54 | + self.commit("initial") |
| 55 | + self.git("branch", "feature") |
| 56 | + |
| 57 | + self.write("base_only.md", GOOD) |
| 58 | + self.write("both_sides.md", GOOD) |
| 59 | + self.write("big.bin", "small\n") |
| 60 | + self.commit("main repairs the links and shrinks the file") |
| 61 | + |
| 62 | + self.git("checkout", "-q", "feature") |
| 63 | + self.write("both_sides.md", BAD + GOOD) |
| 64 | + self.write("feature_only.md", GOOD) |
| 65 | + self.commit("feature edits one file and adds another") |
| 66 | + |
| 67 | + def env(self, **extra): |
| 68 | + # A developer's git config and any inherited GIT_DIR must not reach the fixture. |
| 69 | + env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} |
| 70 | + env["GIT_CONFIG_GLOBAL"] = os.devnull |
| 71 | + env["GIT_CONFIG_SYSTEM"] = os.devnull |
| 72 | + env.update(extra) |
| 73 | + return env |
| 74 | + |
| 75 | + def git(self, *args): |
| 76 | + subprocess.run( |
| 77 | + ["git", *args], |
| 78 | + cwd=self.repo, |
| 79 | + check=True, |
| 80 | + capture_output=True, |
| 81 | + env=self.env(), |
| 82 | + ) |
| 83 | + |
| 84 | + def write(self, relpath, text): |
| 85 | + path = self.repo / relpath |
| 86 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 87 | + path.write_text(text) |
| 88 | + |
| 89 | + def commit(self, message): |
| 90 | + self.git("add", "-A") |
| 91 | + self.git("commit", "-q", "-m", message) |
| 92 | + |
| 93 | + def add_on_feature(self, relpath, text): |
| 94 | + self.write(relpath, text) |
| 95 | + self.commit(f"feature adds {relpath}") |
| 96 | + |
| 97 | + def lint(self, script, **env): |
| 98 | + return subprocess.run( |
| 99 | + ["bash", str(REPO_ROOT / "scripts" / script), "main", "feature"], |
| 100 | + cwd=self.repo, |
| 101 | + capture_output=True, |
| 102 | + text=True, |
| 103 | + env=self.env(**env), |
| 104 | + ) |
| 105 | + |
| 106 | + def assert_scoped_to_feature(self, result): |
| 107 | + output = result.stdout + result.stderr |
| 108 | + self.assertEqual(result.returncode, 0, output) |
| 109 | + self.assertIn("feature_only.md", result.stdout) |
| 110 | + self.assertIn("both_sides.md", result.stdout) |
| 111 | + self.assertNotIn("base_only.md", output) |
| 112 | + |
| 113 | + def curl_stub(self): |
| 114 | + stub_dir = self.repo / "stub" |
| 115 | + stub_dir.mkdir() |
| 116 | + stub = stub_dir / "curl" |
| 117 | + stub.write_text(CURL_STUB) |
| 118 | + stub.chmod(0o755) |
| 119 | + return {"PATH": f"{stub_dir}{os.pathsep}{os.environ['PATH']}"} |
| 120 | + |
| 121 | + def test_lint_urls_ignores_lines_the_branch_only_lacks(self): |
| 122 | + self.assert_scoped_to_feature(self.lint("lint_urls.sh", **self.curl_stub())) |
| 123 | + |
| 124 | + def test_lint_xrefs_ignores_lines_the_branch_only_lacks(self): |
| 125 | + self.assert_scoped_to_feature(self.lint("lint_xrefs.sh")) |
| 126 | + |
| 127 | + def test_lint_file_size_ignores_files_the_branch_only_lacks(self): |
| 128 | + result = self.lint("lint_file_size.sh") |
| 129 | + output = result.stdout + result.stderr |
| 130 | + self.assertEqual(result.returncode, 0, output) |
| 131 | + self.assertIn("feature_only.md", result.stdout) |
| 132 | + self.assertNotIn("big.bin", output) |
| 133 | + |
| 134 | + def test_lint_urls_still_catches_a_url_the_branch_adds(self): |
| 135 | + self.add_on_feature("feature_bad.md", BAD) |
| 136 | + result = self.lint("lint_urls.sh", **self.curl_stub()) |
| 137 | + output = result.stdout + result.stderr |
| 138 | + self.assertEqual(result.returncode, 1, output) |
| 139 | + self.assertIn("example.invalid/dead", output) |
| 140 | + |
| 141 | + def test_lint_xrefs_still_catches_a_reference_the_branch_adds(self): |
| 142 | + self.add_on_feature("feature_bad.md", BAD) |
| 143 | + result = self.lint("lint_xrefs.sh") |
| 144 | + output = result.stdout + result.stderr |
| 145 | + self.assertEqual(result.returncode, 1, output) |
| 146 | + self.assertIn("sub/missing.md", output) |
| 147 | + |
| 148 | + def test_lint_file_size_still_catches_a_file_the_branch_adds(self): |
| 149 | + self.add_on_feature("feature_big.bin", OVERSIZED) |
| 150 | + result = self.lint("lint_file_size.sh") |
| 151 | + output = result.stdout + result.stderr |
| 152 | + self.assertEqual(result.returncode, 1, output) |
| 153 | + self.assertIn("feature_big.bin", output) |
| 154 | + |
| 155 | + def test_lint_urls_survives_a_colorizing_git_config(self): |
| 156 | + config = self.repo / "colorful.gitconfig" |
| 157 | + config.write_text("[color]\n\tui = always\n") |
| 158 | + result = self.lint( |
| 159 | + "lint_urls.sh", GIT_CONFIG_GLOBAL=str(config), **self.curl_stub() |
| 160 | + ) |
| 161 | + self.assert_scoped_to_feature(result) |
0 commit comments