Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: tools/testing/dart/status_file.dart

Issue 2984203002: Move the status file parser into its own package. (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/testing/dart/status_expression.dart ('k') | tools/testing/dart/summary_report.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:io';
6
7 import 'environment.dart';
8 import 'expectation.dart';
9 import 'path.dart';
10 import 'status_expression.dart';
11
12 /// Matches the header that begins a new section, like:
13 ///
14 /// [ $compiler == dart2js && $minified ]
15 final _sectionPattern = new RegExp(r"^\[(.+?)\]");
16
17 /// Matches an entry that defines the status for a path in the current section,
18 /// like:
19 ///
20 /// some/path/to/some_test: Pass || Fail
21 final _entryPattern = new RegExp(r"^([^:#]+):(.*)");
22
23 /// Matches an issue number in a comment, like:
24 ///
25 /// blah_test: Fail # Issue 1234
26 /// ^^^^
27 final _issuePattern = new RegExp(r"[Ii]ssue (\d+)");
28
29 /// A parsed status file, which describes how a collection of tests are
30 /// expected to behave under various configurations and conditions.
31 ///
32 /// Each status file is made of a series of sections. Each section begins with
33 /// a header, followed by a series of entries. A header is enclosed in square
34 /// brackets and contains a Boolean expression. That expression is evaluated in
35 /// an environment. If it evaluates to true, then the entries after the header
36 /// take effect.
37 ///
38 /// Each entry is a glob-like file path followed by a colon and then a
39 /// comma-separated list of [Expectation]s. The path may point to an individual
40 /// file, or a directory, in which case it applies to all files under that path.
41 ///
42 /// Entries may also appear before any section header, in which case they
43 /// always apply.
44 class StatusFile {
45 final String _path;
46 final List<StatusSection> sections = [];
47
48 /// Parses the status file at [_path].
49 StatusFile.read(this._path) {
50 var lines = new File(_path).readAsLinesSync();
51
52 // The current section whose rules are being parsed.
53 StatusSection section;
54
55 var lineNumber = 0;
56
57 for (var line in lines) {
58 lineNumber++;
59
60 fail(String message, [List<String> errors]) {
61 print('$message in "$_shortPath" line $lineNumber:\n$line');
62
63 if (errors != null) {
64 for (var error in errors) {
65 print("- ${error.replaceAll('\n', '\n ')}");
66 }
67 }
68 exit(1);
69 }
70
71 // Strip off the comment and whitespace.
72 var source = line;
73 var comment = "";
74 var hashIndex = line.indexOf('#');
75 if (hashIndex >= 0) {
76 source = line.substring(0, hashIndex);
77 comment = line.substring(hashIndex);
78 }
79 source = source.trim();
80
81 // Ignore empty (or comment-only) lines.
82 if (source.isEmpty) continue;
83
84 // See if we are starting a new section.
85 var match = _sectionPattern.firstMatch(source);
86 if (match != null) {
87 try {
88 var condition = Expression.parse(match[1].trim());
89
90 var errors = <String>[];
91 condition.validate(errors);
92
93 if (errors.isNotEmpty) {
94 var s = errors.length > 1 ? "s" : "";
95 fail('Validation error$s', errors);
96 }
97
98 section = new StatusSection(condition);
99 sections.add(section);
100 } on FormatException {
101 fail("Status expression syntax error");
102 }
103 continue;
104 }
105
106 // Otherwise, it should be a new entry under the current section.
107 match = _entryPattern.firstMatch(source);
108 if (match != null) {
109 var path = match[1].trim();
110 // TODO(whesse): Handle test names ending in a wildcard (*).
111 var expectations = <Expectation>[];
112 for (var name in match[2].split(",")) {
113 name = name.trim();
114 try {
115 expectations.add(Expectation.find(name));
116 } on ArgumentError {
117 fail('Unrecognized test expectation "$name"');
118 }
119 }
120
121 var issue = _issueNumber(comment);
122
123 // If we haven't found a section header yet, create an implicit section
124 // that matches everything.
125 if (section == null) {
126 section = new StatusSection(null);
127 sections.add(section);
128 }
129
130 section.entries.add(new StatusEntry(path, expectations, issue));
131 continue;
132 }
133
134 fail("Unrecognized input");
135 }
136 }
137
138 /// Gets the path to this status file relative to the Dart repo root.
139 String get _shortPath {
140 var repoRoot = new Path(Platform.script
141 .toFilePath(windows: Platform.operatingSystem == "windows"))
142 .join(new Path("../../../../"));
143 return new Path(_path).relativeTo(repoRoot).toString();
144 }
145
146 /// Returns the issue number embedded in [comment] or `null` if there is none.
147 int _issueNumber(String comment) {
148 var match = _issuePattern.firstMatch(comment);
149 if (match == null) return null;
150
151 return int.parse(match[1]);
152 }
153
154 String toString() {
155 var buffer = new StringBuffer();
156 for (var section in sections) {
157 buffer.writeln("[${section._condition}]");
158
159 for (var entry in section.entries) {
160 buffer.write("${entry.path}: ${entry.expectations.join(', ')}");
161 if (entry.issue != null) buffer.write(" # Issue ${entry.issue}");
162 buffer.writeln();
163 }
164
165 buffer.writeln();
166 }
167
168 return buffer.toString();
169 }
170 }
171
172 /// One section in a status file.
173 ///
174 /// Contains the condition from the header that begins the section, then all of
175 /// the entries within the section.
176 class StatusSection {
177 /// The expression that determines when this section is applied.
178 ///
179 /// May be `null` for paths that appear before any section header in the file.
180 /// In that case, the section always applies.
181 final Expression _condition;
182 final List<StatusEntry> entries = [];
183
184 /// Returns true if this section should apply in the given [environment].
185 bool isEnabled(Environment environment) =>
186 _condition == null || _condition.evaluate(environment);
187
188 StatusSection(this._condition);
189 }
190
191 /// Describes the test status of the file or files at a given path.
192 class StatusEntry {
193 final String path;
194 final List<Expectation> expectations;
195 final int issue;
196
197 StatusEntry(this.path, this.expectations, this.issue);
198 }
OLDNEW
« no previous file with comments | « tools/testing/dart/status_expression.dart ('k') | tools/testing/dart/summary_report.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698