Skip to content

Commit

Permalink
Sanity checks for CSS variables. (dart-lang#7980)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Aug 27, 2024
1 parent b412186 commit 424cf80
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion pkg/web_css/test/variables_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:io';
import 'package:test/test.dart';

void main() {
test('all variables are used', () async {
test('all sass variables are used', () async {
final variables = (await File('lib/src/_variables.scss').readAsLines())
.where((l) => l.startsWith(r'$') && l.contains(':'))
.map((l) => l.substring(1).split(':').first.trim())
Expand All @@ -31,4 +31,49 @@ void main() {

expect(variables, isEmpty);
});

group('CSS variables', () {
late Set<String> variables;

setUp(() async {
variables = (await File('lib/src/_variables.scss').readAsLines())
.map((l) => l.trim())
.where((l) => l.startsWith('--') && l.contains(':'))
.map((l) => l.split(':').first.trim())
.where((v) => v.isNotEmpty)
.toSet();

// remove Material design variables
variables.removeWhere((v) => v.startsWith('--mdc-'));
});

test('variables are present', () {
expect(variables, isNotEmpty);
});

test('a variable does not share another as prefix', () {
for (final v in variables) {
final shared =
variables.where((x) => x != v && x.startsWith(v)).toList();
expect(shared, isEmpty, reason: v);
}
});

test('all CSS variables are used', () async {
final files = await Directory('lib')
.list(recursive: true)
.where((f) => f is File && f.path.endsWith('.scss'))
.cast<File>()
.toList();

final unused = <String>{...variables};
for (final file in files) {
if (unused.isEmpty) break;
final content = await file.readAsString();
unused.removeWhere((v) => content.contains('var($v)'));
}

expect(unused, isEmpty);
});
});
}

0 comments on commit 424cf80

Please sign in to comment.