|
| 1 | +import json |
| 2 | +import sys |
| 3 | + |
| 4 | +import pytest |
| 5 | +from repo_review.ghpath import GHPath |
1 | 6 | from repo_review.testing import compute_check |
2 | 7 |
|
| 8 | +from sp_repo_review.checks._jsonc import strip_jsonc |
| 9 | +from sp_repo_review.checks.renovate import renovate |
| 10 | + |
| 11 | +# Real-world Renovate configs, one per supported format/location, pinned to a |
| 12 | +# specific commit so upstream edits cannot break this. Skipped by default |
| 13 | +# (network access, GitHub rate limits); run manually to confirm coverage. |
| 14 | +REAL_WORLD_CONFIGS = [ |
| 15 | + ( |
| 16 | + "gulfofmaine/climatology_py_dash", |
| 17 | + "e4ead74d17d7c07dbb40b9c99fc1138f927abdd3", |
| 18 | + "renovate.json", |
| 19 | + ), |
| 20 | + ( |
| 21 | + "jumpstarter-dev/jumpstarter", |
| 22 | + "8bae226d0a2d9cbe156bded24628e85c6d9f6cd9", |
| 23 | + "renovate.jsonc", |
| 24 | + ), |
| 25 | + ( |
| 26 | + "SonarSource/docker-sonarqube", |
| 27 | + "9f00ce57d8654a3737ae0b22997d7198f71660d8", |
| 28 | + "renovate.json5", |
| 29 | + ), |
| 30 | + ( |
| 31 | + "adobe/spectrum-css", |
| 32 | + "37620864c60c4c142a506017e1a15348a26abb0e", |
| 33 | + ".github/renovate.json", |
| 34 | + ), |
| 35 | + ( |
| 36 | + "paddyroddy/.github", |
| 37 | + "c97ca7c448df211268616ce438777228fe103733", |
| 38 | + ".renovaterc.json5", |
| 39 | + ), |
| 40 | + ( |
| 41 | + "zammad/zammad", |
| 42 | + "93fb7f107b07b4b4294e21249b277bc48c431da5", |
| 43 | + ".gitlab/renovate.json", |
| 44 | + ), |
| 45 | + ( |
| 46 | + "prettier/eslint-config-prettier", |
| 47 | + "07829b4912d173986610a4985247896b09f9fcaf", |
| 48 | + ".renovaterc", |
| 49 | + ), |
| 50 | + ( |
| 51 | + "Esri/calcite-design-system", |
| 52 | + "5613d9f8000ba12bf55c7da50e2f119c12435302", |
| 53 | + ".renovaterc.json", |
| 54 | + ), |
| 55 | +] |
| 56 | + |
| 57 | + |
| 58 | +def test_strip_jsonc_line_comment() -> None: |
| 59 | + text = '{\n // a comment\n "a": 1 // trailing\n}' |
| 60 | + assert json.loads(strip_jsonc(text)) == {"a": 1} |
| 61 | + |
| 62 | + |
| 63 | +def test_strip_jsonc_block_comment() -> None: |
| 64 | + text = '{\n /* multi\n line */ "a": 1\n}' |
| 65 | + assert json.loads(strip_jsonc(text)) == {"a": 1} |
| 66 | + |
| 67 | + |
| 68 | +def test_strip_jsonc_preserves_string_content() -> None: |
| 69 | + # Comment markers and commas inside strings must survive untouched. |
| 70 | + text = '{"url": "https://x/y", "csv": "a,b,", "block": "/* not a comment */"}' |
| 71 | + assert json.loads(strip_jsonc(text)) == { |
| 72 | + "url": "https://x/y", |
| 73 | + "csv": "a,b,", |
| 74 | + "block": "/* not a comment */", |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +def test_strip_jsonc_escaped_quote_in_string() -> None: |
| 79 | + text = r'{"a": "she said \"hi\" // ok"}' |
| 80 | + assert json.loads(strip_jsonc(text)) == {"a": 'she said "hi" // ok'} |
| 81 | + |
| 82 | + |
| 83 | +def test_strip_jsonc_trailing_commas() -> None: |
| 84 | + text = '{\n "a": [1, 2, 3,],\n "b": {"c": 1,},\n}' |
| 85 | + assert json.loads(strip_jsonc(text)) == {"a": [1, 2, 3], "b": {"c": 1}} |
| 86 | + |
| 87 | + |
| 88 | +def test_strip_jsonc_plain_json_unchanged() -> None: |
| 89 | + text = '{"a": 1, "b": [1, 2]}' |
| 90 | + assert strip_jsonc(text) == text |
| 91 | + |
| 92 | + |
| 93 | +def test_renovate_fixture_jsonc(tmp_path) -> None: |
| 94 | + (tmp_path / "renovate.jsonc").write_text( |
| 95 | + '{\n // pin digests\n "extends": ["config:recommended"],\n}', |
| 96 | + encoding="utf-8", |
| 97 | + ) |
| 98 | + assert renovate(tmp_path) == {"extends": ["config:recommended"]} |
| 99 | + |
| 100 | + |
| 101 | +def test_renovate_fixture_json5_fallback(tmp_path) -> None: |
| 102 | + pytest.importorskip("json5") |
| 103 | + # Unquoted keys are true JSON5 and cannot be stripped to plain JSON. |
| 104 | + (tmp_path / "renovate.json5").write_text( |
| 105 | + '{\n extends: ["config:recommended"],\n}', |
| 106 | + encoding="utf-8", |
| 107 | + ) |
| 108 | + assert renovate(tmp_path) == {"extends": ["config:recommended"]} |
| 109 | + |
| 110 | + |
| 111 | +def test_renovate_fixture_json5_missing_errors(tmp_path, monkeypatch) -> None: |
| 112 | + (tmp_path / "renovate.json5").write_text( |
| 113 | + '{\n extends: ["config:recommended"],\n}', |
| 114 | + encoding="utf-8", |
| 115 | + ) |
| 116 | + # Simulate the json5 extra not being installed. |
| 117 | + monkeypatch.setitem(sys.modules, "json5", None) |
| 118 | + with pytest.raises(ImportError, match=r"sp-repo-review\[json5\]"): |
| 119 | + renovate(tmp_path) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.skip(reason="Network access, can be rate limited") |
| 123 | +@pytest.mark.parametrize(("repo", "sha", "location"), REAL_WORLD_CONFIGS) |
| 124 | +def test_renovate_real_world(repo, sha, location) -> None: |
| 125 | + root = GHPath(repo=repo, branch=sha) |
| 126 | + assert root.joinpath(location).is_file() |
| 127 | + assert renovate(root) |
| 128 | + |
3 | 129 |
|
4 | 130 | def test_ren200() -> None: |
5 | 131 | renovate = {"extends": ["config:recommended"]} |
|
0 commit comments