Make dart_hooks config keys a single source of truth + guard shipped examples#152
Make dart_hooks config keys a single source of truth + guard shipped examples#152reidbaker wants to merge 1 commit into
Conversation
…d examples Follow-up to flutter#150/flutter#151. Two gaps let the wrong-key bug ship undetected: the test helpers hardcoded the key strings independently of the code, and no test ever loaded the committed dart_hooks.yaml example files. - Add a `static const String configKeyName` to DartFormatHook and DartAnalyzeHook; the configKey getter now delegates to it. BaseHook.run() still gates on the polymorphic instance getter, so runtime behavior is unchanged. - test_utils.dart's mockAnalyzeConfig/mockFormatConfig now derive their keys from configKeyName instead of hardcoded literals, so the test's keys can no longer drift from the code. - Add test/example_config_test.dart, which loads each committed dart_hooks.yaml and asserts both configKeyName keys are present and true. This is the check that catches a flutter#150-style regression; a guard test fails loudly if no shipped files are found.
There was a problem hiding this comment.
Code Review
This pull request introduces static configKeyName constants to DartAnalyzeHook and DartFormatHook to prevent configuration keys from drifting, and updates test utilities to use these constants. It also adds a new test suite example_config_test.dart to verify that the shipped dart_hooks.yaml files correctly enable both hooks. A review comment suggests adjusting how packageRoot is resolved in the test suite to ensure correct path resolution when tests are executed from the monorepo root instead of the package root.
| final String packageRoot = Directory.current.path; | ||
| final candidatePaths = <String>[ | ||
| path.join(packageRoot, 'dart_hooks.yaml'), | ||
| path.join(packageRoot, '..', 'dart_hooks.yaml'), | ||
| path.join(packageRoot, '..', '..', 'dart_hooks.yaml'), | ||
| path.join(packageRoot, '..', 'dart_skills_lint', 'dart_hooks.yaml'), | ||
| ]; |
There was a problem hiding this comment.
When running tests from the monorepo root (e.g., dart test tool/dart_hooks), Directory.current points to the monorepo root rather than the package root. This causes the relative paths in candidatePaths to resolve incorrectly, resulting in 3 out of the 4 configuration files being silently skipped during verification.
Adjusting packageRoot to point to the actual package directory when run from the monorepo root ensures all 4 configuration files are always verified.
var packageRoot = Directory.current.path;
final toolDartHooks = path.join(packageRoot, 'tool', 'dart_hooks');
if (Directory(toolDartHooks).existsSync()) {
packageRoot = toolDartHooks;
}
final candidatePaths = <String>[
path.join(packageRoot, 'dart_hooks.yaml'),
path.join(packageRoot, '..', 'dart_hooks.yaml'),
path.join(packageRoot, '..', '..', 'dart_hooks.yaml'),
path.join(packageRoot, '..', 'dart_skills_lint', 'dart_hooks.yaml'),
];|
Pr in draft because I am not sure the new test is worth adding. |
Summary
Follow-up to #150 / #151. That fix corrected the shipped
dart_hooks.yamlfiles, but two gaps let the wrong-key bug ship undetected in the first place, and this hardens against both:test/test_utils.dartwrote'DartAnalyzeHook'/'DartFormatHook'as string literals, so the test's notion of the key could drift from each hook'sconfigKeywith nothing reconciling them.Changes
static const String configKeyNametoDartFormatHookandDartAnalyzeHook; theconfigKeygetter now delegates to it.BaseHook.run()still gates on the polymorphic instance getter, so runtime behavior is unchanged.mockAnalyzeConfig/mockFormatConfigintest_utils.dartnow build their YAML fromconfigKeyNameinstead of literals — the test's keys can no longer drift from the code.test/example_config_test.dartloads each committeddart_hooks.yaml(package root,tool/, repo root,tool/dart_skills_lint/) and asserts bothconfigKeyNamekeys are present andtrue, mirroringBaseHook.run()'s enablement gate. A guard test fails loudly if no shipped files are found, so a wrong working directory can't pass the suite vacuously.Verification
dart analyze --fatal-infos— no issuesdart format --output=none --set-exit-if-changedon the touched files — cleandart test— 32 pass (was 27)agent_dart_format.dart: truekeys madeexample_config_test.dartfail on exactly that file while the other three passed; restored afterward.