diff --git a/.github/workflows/lockfile_guard.yml b/.github/workflows/lockfile_guard.yml index b30b06fee0..65fcbd6ecb 100644 --- a/.github/workflows/lockfile_guard.yml +++ b/.github/workflows/lockfile_guard.yml @@ -23,6 +23,18 @@ concurrency: cancel-in-progress: true jobs: + metadata: + name: hosted package metadata is coherent + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - name: Unit-test lockfile validation + run: python3 -m unittest discover -s tool/release -p test_check_lock_hosted_names.py -v + - name: Validate current lockfile hosted package metadata + run: python3 tool/release/check_lock_hosted_names.py pubspec.lock + guard: name: pubspec.lock changes runs-on: ubuntu-latest diff --git a/tool/release/check_lock_hosted_names.py b/tool/release/check_lock_hosted_names.py new file mode 100644 index 0000000000..9da4bd98b0 --- /dev/null +++ b/tool/release/check_lock_hosted_names.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 +"""Validate hosted package identity in a Dart pubspec.lock without dependencies. + +This checks only the deterministic package-block shape emitted by Dart pub. +It is not a general YAML parser and does not replace flutter pub get. +Non-hosted packages (git/path/sdk) are deliberately outside its scope. +""" +from __future__ import annotations + +import argparse +import re +from pathlib import Path + + +# Match pub_semver, including the leading zeros Dart pub preserves in lockfiles. +_VERSION = re.compile( + r"[0-9]+\.[0-9]+\.[0-9]+" + r"(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?" + r"(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?" +) + + +def validate_hosted_names(content: str) -> list[str]: + errors: list[str] = [] + in_packages = False + saw_packages = False + block: list[str] = [] + package = "" + line_number = 0 + + def check_block() -> None: + if not package: + return + source = None + name = None + version = None + in_description = False + for line in block: + if line.startswith(" ") and not line.startswith(" "): + # Only an immediate child of the package can open a description. + in_description = line == " description:" + if line.startswith(" source: "): + source = line[len(" source: ") :].strip().strip('"') + elif line.startswith(" version: "): + version = line[len(" version: ") :].strip().strip('"') + elif in_description and line.startswith(" name: "): + name = line[len(" name: ") :].strip().strip('"') + if source == "hosted": + if name != package: + errors.append( + f"pubspec.lock:{line_number}: hosted package {package!r} " + f"has description.name {name!r}" + ) + if not version: + errors.append( + f"pubspec.lock:{line_number}: hosted package {package!r} " + "has no version" + ) + elif _VERSION.fullmatch(version) is None: + errors.append( + f"pubspec.lock:{line_number}: hosted package {package!r} " + f"has invalid version {version!r}" + ) + + for number, raw in enumerate(content.splitlines(), 1): + if raw == "packages:": + if saw_packages: + errors.append(f"pubspec.lock:{number}: duplicate packages section") + in_packages = True + saw_packages = True + continue + if not in_packages: + continue + # Top-level keys end the package map; no other pubspec sections + # should be accidentally interpreted as package entries. + if raw and not raw.startswith((" ", "#")): + check_block() + in_packages = False + package = "" + block = [] + continue + if raw.startswith(" ") and not raw.startswith(" ") and raw.endswith(":"): + check_block() + package = raw[2:-1].strip() + line_number = number + block = [] + if not package: + errors.append(f"pubspec.lock:{number}: empty package key") + else: + block.append(raw) + if in_packages: + check_block() + + if not saw_packages: + errors.append("pubspec.lock: missing packages: section") + return errors + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("lockfile", type=Path) + args = parser.parse_args() + errors = validate_hosted_names(args.lockfile.read_text(encoding="utf-8")) + for error in errors: + print(f"::error::{error}") + if errors: + return 1 + print("Hosted package identity metadata is consistent.") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tool/release/test_check_lock_hosted_names.py b/tool/release/test_check_lock_hosted_names.py new file mode 100644 index 0000000000..e9311dfe65 --- /dev/null +++ b/tool/release/test_check_lock_hosted_names.py @@ -0,0 +1,126 @@ +import unittest + +from check_lock_hosted_names import validate_hosted_names + + +def lock(body: str) -> str: + return "packages:\n" + body + "sdks:\n dart: \"^3.12.0\"\n" + + +GOOD = """ flutter_markdown_plus: + dependency: "direct main" + description: + name: flutter_markdown_plus + sha256: "aaaa" + url: "https://pub.dev" + source: hosted + version: "1.0.12" + a_fork: + dependency: "direct main" + description: + path: packages/a_fork + ref: abc123 + resolved-ref: def456 + url: "https://github.com/Otzaria/a_fork" + source: git + version: "0.5.2" +""" + + +class HostedPackageLockMetadataTests(unittest.TestCase): + def test_valid_hosted_and_git_package(self): + self.assertEqual(validate_hosted_names(lock(GOOD)), []) + + def test_catches_original_markdown_rebase_regression(self): + bad = GOOD.replace(" name: flutter_markdown_plus", " name: flutter_markdown") + errors = validate_hosted_names(lock(bad)) + self.assertEqual(len(errors), 1) + self.assertIn("description.name 'flutter_markdown'", errors[0]) + + def test_missing_hosted_name_fails(self): + bad = GOOD.replace(" name: flutter_markdown_plus\n", "") + self.assertEqual(len(validate_hosted_names(lock(bad))), 1) + + def test_missing_hosted_version_fails(self): + bad = GOOD.replace(' version: "1.0.12"\n', "") + self.assertEqual(len(validate_hosted_names(lock(bad))), 1) + + def test_git_name_is_not_checked_as_hosted(self): + custom = GOOD.replace( + " source: git\n", + " name: a-different-remote-name\n source: git\n", + ) + self.assertEqual(validate_hosted_names(lock(custom)), []) + + def test_invalid_hosted_version_fails(self): + for value in ( + "nonsense", "1.0", "^1.0.0", "1.2.3+bad..build", + "1.2.3-", "1.2.3+", "1.2.3-dev..1", "1.2.3-dev_1", + "1.2.3+build_1", "1.2.3+build+2", + ): + with self.subTest(value=value): + bad = GOOD.replace('version: "1.0.12"', f"version: {value}") + errors = validate_hosted_names(lock(bad)) + self.assertEqual(len(errors), 1) + self.assertIn("invalid version", errors[0]) + + def test_valid_prerelease_and_build_version(self): + for value in ("0.0.0", "1.2.3-dev.1", "2.1.0+42", "3.4.5-rc.1+build.2"): + with self.subTest(value=value): + good = GOOD.replace('version: "1.0.12"', f'version: "{value}"') + self.assertEqual(validate_hosted_names(lock(good)), []) + + def test_dart_pub_versions_allow_leading_zeros(self): + for value in ( + "01.2.3", "1.02.3", "1.2.03", "01.02.03", + "00.00.00", "01.02.03-01.dev-2+001.build-2", + "1.2.3-01+002", "1.2.3--+-", + ): + with self.subTest(value=value): + good = GOOD.replace('version: "1.0.12"', f'version: "{value}"') + self.assertEqual(validate_hosted_names(lock(good)), []) + + def test_hosted_name_must_be_inside_description(self): + bad = GOOD.replace(" description:\n", " unexpected:\n", 1) + errors = validate_hosted_names(lock(bad)) + self.assertEqual(len(errors), 1) + self.assertIn("description.name None", errors[0]) + + def test_mixed_hosted_git_path_sdk(self): + tail = """ local: + description: + path: packages/local + source: path + version: "1.0.0" + flutter: + description: flutter + source: sdk + version: "0.0.0" +""" + self.assertEqual(validate_hosted_names(lock(GOOD + tail)), []) + + def test_missing_packages_section_fails(self): + self.assertTrue(validate_hosted_names("sdks:\n dart: ^3.12.0\n")) + + def test_lockfile_with_only_nonhosted_packages_is_valid(self): + nonhosted = ( + " only_git:\n source: git\n version: \"0.3.0\"\n" + " only_path:\n source: path\n version: \"1.0.0\"\n" + " flutter:\n description: flutter\n source: sdk\n" + " version: \"0.0.0\"\n" + ) + self.assertEqual(validate_hosted_names(lock(nonhosted)), []) + self.assertEqual(validate_hosted_names(lock("")), []) + + def test_final_package_validated_without_sdks(self): + self.assertEqual(validate_hosted_names("packages:\n" + GOOD), []) + + def test_duplicate_packages_section_fails(self): + self.assertTrue( + any("duplicate packages section" in e for e in + validate_hosted_names(lock(GOOD) + "packages:\n" + GOOD)) + ) + + +if __name__ == "__main__": + unittest.main()