flutter_flutter/dev/bots/test/ci_yaml_validation_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

186 lines
5.8 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@TestOn('vm')
library;
import 'dart:io' as io;
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';
import 'package:yaml/yaml.dart';
import './common.dart';
void main() {
final String flutterRoot = () {
io.Directory current = io.Directory.current;
while (!io.File(p.join(current.path, 'DEPS')).existsSync()) {
if (current.path == current.parent.path) {
fail(
'Could not find flutter repository root (${io.Directory.current.path} -> ${current.path})',
);
}
current = current.parent;
}
return current.path;
}();
/// Verify that the platform specified in the target name matches one of the
/// platforms defined in the platform_properties section.
void checkTargetPlatform(String targetName, _CiYaml ciYaml) {
final String platformName = targetName.split(' ').first.toLowerCase();
expect(ciYaml.platforms, contains(platformName));
}
group('framework', () {
final _CiYaml ciYaml = _CiYaml.parse(p.join(flutterRoot, '.ci.yaml'));
for (final _CiYamlTarget target in ciYaml.targets) {
group(target.name, () {
setUp(() {
printOnFailure(target.span.message('One or more errors occurred validating'));
});
test('validate platform name', () {
checkTargetPlatform(target.name, ciYaml);
});
if (target.runIf != null && target.runIf!.isNotEmpty) {
test('must include .ci.yaml', () {
expect(
target.runIf,
contains('.ci.yaml'),
reason:
'.ci.yaml inclusion means changes to the runIfs will trigger presubmit tests.',
);
});
test('must include DEPS', () {
expect(
target.runIf,
contains('DEPS'),
reason: 'DEPS updates (including the Dart SDK) mean presubmit tests must be run.',
);
});
test('must include the engine sources', () {
expect(
target.runIf,
contains('engine/**'),
reason: 'Engine updates means framework presubmit tests must be run.',
);
});
}
});
}
});
group('engine', () {
final _CiYaml ciYaml = _CiYaml.parse(
p.join(flutterRoot, 'engine', 'src', 'flutter', '.ci.yaml'),
);
for (final _CiYamlTarget target in ciYaml.targets) {
group(target.name, () {
setUp(() {
printOnFailure(target.span.message('One or more errors occurred validating'));
});
test('validate platform name', () {
checkTargetPlatform(target.name, ciYaml);
});
if (target.runIf != null && target.runIf!.isNotEmpty) {
test('must include .ci.yaml', () {
expect(
target.runIf,
contains('engine/src/flutter/.ci.yaml'),
reason:
'.ci.yaml inclusion means changes to the runIfs will trigger presubmit tests.',
);
});
test('must include DEPS', () {
expect(
target.runIf,
contains('DEPS'),
reason: 'DEPS updates (including the Dart SDK) mean presubmit tests must be run.',
);
});
}
});
}
});
}
/// A minimal representation of an ostensibly well-formatted `.ci.yaml` file.
///
/// Due to the repository setup, it's not possible to reuse the existing
/// specifications of this file, and since the test case is only testing a
/// subset of the encoding, this class exposes only that subset.
///
/// For a discussion leading to this design decision, see
/// <https://github.com/flutter/flutter/issues/160915>.
///
/// See also:
/// - [`scheduler.proto`][1], the schema definition of the file format.
/// - [`CI_YAML.md`][2], a human-authored description of the file format.
/// - [`ci_yaml.dart`][3], where validation is performed (in `flutter/cocoon`).
///
/// [1]: https://github.com/flutter/cocoon/blob/main/app_dart/lib/src/model/proto/internal/scheduler.proto
/// [2]: https://github.com/flutter/cocoon/blob/main/CI_YAML.md
/// [3]: https://github.com/flutter/cocoon/blob/main/app_dart/lib/src/model/ci_yaml/ci_yaml.dart
final class _CiYamlTarget {
_CiYamlTarget({required this.name, required this.span, required this.runIf});
factory _CiYamlTarget.fromYamlMap(YamlMap map) {
return _CiYamlTarget(
name: map['name'] as String,
span: map.span,
runIf: () {
final runIf = map['runIf'] as YamlList?;
if (runIf == null) {
return null;
}
return runIf.cast<String>().toList();
}(),
);
}
/// Name of the target.
final String name;
/// Where the target was parsed at in the `.ci.yaml` file.
final SourceSpan span;
/// Which lines were present in a `runIf` block, if any.
final List<String>? runIf;
}
final class _CiYaml {
_CiYaml({required this.targets, required this.platforms});
final List<_CiYamlTarget> targets;
final Set<String> platforms;
/// Parses a list of targets from the provided `.ci.yaml` file [path].
static _CiYaml parse(String path) {
final YamlDocument yamlDoc = loadYamlDocument(
io.File(path).readAsStringSync(),
sourceUrl: Uri.parse(path),
);
final root = yamlDoc.contents as YamlMap;
final yamlTargets = root['targets'] as YamlList;
final List<_CiYamlTarget> targets = yamlTargets.nodes.map((YamlNode node) {
return _CiYamlTarget.fromYamlMap(node as YamlMap);
}).toList();
final yamlPlatforms = root['platform_properties'] as YamlMap;
final platforms = Set<String>.from(yamlPlatforms.keys.cast<String>());
return _CiYaml(targets: targets, platforms: platforms);
}
}