File size: 4,994 Bytes
e0a2378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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;
  }();

  group('framework', () {
    final List<_CiYamlTarget> targets = _CiYamlTarget.parseAll(p.join(flutterRoot, '.ci.yaml'));

    for (final _CiYamlTarget target in targets) {
      if (target.runIf == null || target.runIf!.isEmpty) {
        continue;
      }

      setUp(() {
        printOnFailure(target.span.message('One or more errors occurred validating'));
      });

      group(target.name, () {
        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 List<_CiYamlTarget> targets = _CiYamlTarget.parseAll(
      p.join(flutterRoot, 'engine', 'src', 'flutter', '.ci.yaml'),
    );

    for (final _CiYamlTarget target in targets) {
      if (target.runIf == null || target.runIf!.isEmpty) {
        continue;
      }

      setUp(() {
        printOnFailure(target.span.message('One or more errors occurred validating'));
      });

      group(target.name, () {
        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 YamlList? runIf = map['runIf'] as YamlList?;
        if (runIf == null) {
          return null;
        }
        return runIf.cast<String>().toList();
      }(),
    );
  }

  /// Parses a list of targets from the provided `.ci.yaml` file [path].
  static List<_CiYamlTarget> parseAll(String path) {
    final YamlDocument yamlDoc = loadYamlDocument(
      io.File(path).readAsStringSync(),
      sourceUrl: Uri.parse(path),
    );

    final YamlMap root = yamlDoc.contents as YamlMap;
    final YamlList targets = root['targets'] as YamlList;
    return targets.nodes.map((YamlNode node) {
      return _CiYamlTarget.fromYamlMap(node as YamlMap);
    }).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;
}