/
impl.dart
70 lines (54 loc) · 1.95 KB
/
impl.dart
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
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'utils.dart';
const pubPlaceHolder = 'PUB';
void expectBuildCleanImpl(
String workingDir,
List<String> command, {
String? packageRelativeDirectory,
}) {
if (command.isEmpty) {
throw ArgumentError.value(command, 'customCommand', 'Cannot be empty');
}
final repoRoot =
_runProc('git', ['rev-parse', '--show-toplevel'], workingDir);
final pkgDir = p.join(repoRoot, packageRelativeDirectory);
if (!p.equals(pkgDir, workingDir)) {
throw StateError('Expected the git root ($repoRoot) '
'to match the current directory ($workingDir).');
}
// 1 - get a list of modified files files - should be empty
expect(
_changedGeneratedFiles(workingDir),
isEmpty,
reason: 'The working directory should be clean before running build.',
);
var executable = command.first;
if (executable == pubPlaceHolder) {
executable = pubPath;
} else if (executable == 'DART') {
executable = dartPath;
}
final arguments = command.skip(1).toList();
// 2 - run build - should be no output, since nothing should change
final result = _runProc(executable, arguments, workingDir);
expectResultOutputSucceeds(result);
// 3 - get a list of modified files after the build - should still be empty
expect(_changedGeneratedFiles(workingDir), isEmpty);
}
void expectResultOutputSucceeds(String result) {
expect(result,
contains(RegExp(r'\[INFO\] Succeeded after .+ with \d+ outputs')));
}
String _changedGeneratedFiles(String workingDir) =>
_runProc('git', ['diff'], workingDir);
String _runProc(String proc, List<String> args, String workingDir) {
final result = Process.runSync(proc, args, workingDirectory: workingDir);
if (result.exitCode != 0) {
print(result.stdout);
print(result.stderr);
throw ProcessException(proc, args, 'Process failed', result.exitCode);
}
return (result.stdout as String).trim();
}