|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -// import 'dart:io'; |
| 5 | +import 'dart:io'; |
6 | 6 |
|
7 | 7 | import 'package:test/test.dart'; |
8 | 8 |
|
9 | | -// final _whitespace = RegExp(r'\s+'); |
| 9 | +final _whitespace = RegExp(r'\s+'); |
10 | 10 |
|
11 | | -// const _colors = { |
12 | | -// 'black', |
13 | | -// 'red', |
14 | | -// 'white', |
15 | | -// }; |
| 11 | +const _colors = { |
| 12 | + 'black', |
| 13 | + 'red', |
| 14 | + 'white', |
| 15 | +}; |
16 | 16 |
|
17 | 17 | void main() { |
18 | 18 | group('Color restrictions and rules', () { |
19 | | - // TODO(https://github.com/dart-lang/pub-dev/issues/8248): comment in once issue is resolved. |
20 | | - // test('text and bg colors are only defined in _variables.scss', |
21 | | - // skip: 'https://github.com/dart-lang/pub-dev/issues/8248', () { |
22 | | - // final files = Directory('lib') |
23 | | - // .listSync(recursive: true) |
24 | | - // .whereType<File>() |
25 | | - // .where((f) => f.path.endsWith('.scss')) |
26 | | - // // _variables.scss is not checked, this should be the place for all color definitions |
27 | | - // .where((f) => !f.path.endsWith('/_variables.scss')) |
28 | | - |
29 | | - // // _staging_ribbon.scss is only used on staging |
30 | | - // .where((f) => !f.path.endsWith('/_staging_ribbon.scss')) |
31 | | - |
32 | | - // // _footer.scss has only one color theme, skipping for now |
33 | | - // // TODO: migrate the variables in this file |
34 | | - // .where((f) => !f.path.endsWith('/_footer.scss')) |
35 | | - |
36 | | - // // dartdoc files are not checked |
37 | | - // .where((f) => !f.path.endsWith('/dartdoc.scss')) |
38 | | - // .toList(); |
39 | | - |
40 | | - // final badExpressions = <String>[]; |
41 | | - |
42 | | - // for (final file in files) { |
43 | | - // var content = file.readAsStringSync(); |
44 | | - // // remove multi-line comment blocks |
45 | | - // content = |
46 | | - // content.replaceAll(RegExp(r'\/\*.*\*/', multiLine: true), ' '); |
47 | | - // final lines = content.split('\n'); |
48 | | - // for (var i = 0; i < lines.length; i++) { |
49 | | - // final line = lines[i]; |
50 | | - // // remove single-line comment |
51 | | - // final expr = |
52 | | - // line.split('//').first.replaceAll(_whitespace, ' ').trim(); |
53 | | - // if (expr.isEmpty) continue; |
54 | | - |
55 | | - // // minimal parsing and sanity check |
56 | | - // final parts = expr.split(':'); |
57 | | - // if (parts.length != 2) continue; |
58 | | - // final name = parts[0].trim(); |
59 | | - // var value = parts[1].trim(); |
60 | | - |
61 | | - // // remove known overlapping variable names from checking |
62 | | - // value = |
63 | | - // value.replaceAll('--pub-badge-red-color', '--pub-badge-color'); |
64 | | - |
65 | | - // if (name.isEmpty || value.isEmpty) continue; |
66 | | - |
67 | | - // // local mdc overrides are exempted for now |
68 | | - // // TODO: move these values to _variables.scss |
69 | | - // if (name.startsWith('--mdc-theme-')) { |
70 | | - // continue; |
71 | | - // } |
72 | | - |
73 | | - // // border colors, box shadows and text-decorations are exempted for now |
74 | | - // // TODO: move these values to _variables.scss |
75 | | - // if (name == 'border' || |
76 | | - // name.startsWith('border-') || |
77 | | - // name == 'box-shadow' || |
78 | | - // name == 'text-decoration') { |
79 | | - // continue; |
80 | | - // } |
81 | | - |
82 | | - // // detect color patterns |
83 | | - // final hasColor = value.contains('#') || |
84 | | - // // TODO: also migrate color- variables |
85 | | - // // value.contains(r'$color-') || |
86 | | - // value.contains('rgb(') || |
87 | | - // value.contains('rgba(') || |
88 | | - // value.contains('hsl(') || |
89 | | - // _colors.any((c) => value.contains(c)); |
90 | | - // if (!hasColor) continue; |
91 | | - |
92 | | - // badExpressions.add('${file.path} line #${i + 1}: `${expr.trim()}`'); |
93 | | - // } |
94 | | - // } |
95 | | - |
96 | | - // expect(badExpressions, isEmpty); |
97 | | - // }); |
| 19 | + test('text and bg colors are only defined in _variables.scss', () { |
| 20 | + final files = Directory('lib') |
| 21 | + .listSync(recursive: true) |
| 22 | + .whereType<File>() |
| 23 | + .where((f) => f.path.endsWith('.scss')) |
| 24 | + // _variables.scss is not checked, this should be the place for all color definitions |
| 25 | + .where((f) => !f.path.endsWith('/_variables.scss')) |
| 26 | + |
| 27 | + // _staging_ribbon.scss is only used on staging |
| 28 | + .where((f) => !f.path.endsWith('/_staging_ribbon.scss')) |
| 29 | + |
| 30 | + // _footer.scss has only one color theme, skipping for now |
| 31 | + // TODO: migrate the variables in this file |
| 32 | + .where((f) => !f.path.endsWith('/_footer.scss')) |
| 33 | + |
| 34 | + // dartdoc files are not checked |
| 35 | + .where((f) => !f.path.endsWith('/dartdoc.scss')) |
| 36 | + .toList(); |
| 37 | + |
| 38 | + final badExpressions = <String>[]; |
| 39 | + |
| 40 | + for (final file in files) { |
| 41 | + var content = file.readAsStringSync(); |
| 42 | + // remove multi-line comment blocks |
| 43 | + content = |
| 44 | + content.replaceAll(RegExp(r'\/\*.*\*/', multiLine: true), ' '); |
| 45 | + final lines = content.split('\n'); |
| 46 | + for (var i = 0; i < lines.length; i++) { |
| 47 | + final line = lines[i]; |
| 48 | + // remove single-line comment |
| 49 | + final expr = |
| 50 | + line.split('//').first.replaceAll(_whitespace, ' ').trim(); |
| 51 | + if (expr.isEmpty) continue; |
| 52 | + |
| 53 | + // minimal parsing and sanity check |
| 54 | + final parts = expr.split(':'); |
| 55 | + if (parts.length != 2) continue; |
| 56 | + final name = parts[0].trim(); |
| 57 | + var value = parts[1].trim(); |
| 58 | + |
| 59 | + // remove known overlapping variable names from checking |
| 60 | + value = |
| 61 | + value.replaceAll('--pub-badge-red-color', '--pub-badge-color'); |
| 62 | + |
| 63 | + if (name.isEmpty || value.isEmpty) continue; |
| 64 | + |
| 65 | + // local mdc overrides are exempted for now |
| 66 | + // TODO: move these values to _variables.scss |
| 67 | + if (name.startsWith('--mdc-theme-')) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // border colors, box shadows and text-decorations are exempted for now |
| 72 | + // TODO: move these values to _variables.scss |
| 73 | + if (name == 'border' || |
| 74 | + name.startsWith('border-') || |
| 75 | + name == 'box-shadow' || |
| 76 | + name == 'text-decoration') { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // detect color patterns |
| 81 | + final hasColor = value.contains('#') || |
| 82 | + // TODO: also migrate color- variables |
| 83 | + // value.contains(r'$color-') || |
| 84 | + value.contains('rgb(') || |
| 85 | + value.contains('rgba(') || |
| 86 | + value.contains('hsl(') || |
| 87 | + (_colors.any((c) => value.contains(c)) && |
| 88 | + !value.contains('var(--pub-color-')); |
| 89 | + if (!hasColor) continue; |
| 90 | + |
| 91 | + badExpressions.add('${file.path} line #${i + 1}: `${expr.trim()}`'); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + expect(badExpressions, isEmpty); |
| 96 | + }); |
98 | 97 | }); |
99 | 98 | } |
0 commit comments