Skip to content

Commit a784110

Browse files
committed
Refactor integration tests.
1 parent 01da402 commit a784110

34 files changed

+1551
-2516
lines changed

_test/build.throws.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

_test/test/build_integration_test.dart

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ library;
77

88
import 'dart:io';
99

10-
import 'package:build_runner/src/internal.dart';
1110
import 'package:path/path.dart' as p;
1211
import 'package:test/test.dart';
1312

@@ -127,25 +126,6 @@ void main() {
127126
expect(dartSource.existsSync(), true);
128127
});
129128

130-
test('Re-snapshots if there is no asset graph', () async {
131-
final assetGraph = assetGraphPathFor(scriptKernelLocation);
132-
await File(assetGraph).delete();
133-
134-
final nextBuild = await runBuild();
135-
expect(
136-
(nextBuild.stdout as String).split('\n'),
137-
containsAllInOrder([
138-
contains('Generating the build script'),
139-
contains('Compiling the build script.'),
140-
contains('Creating the asset graph.'),
141-
contains(
142-
'Building, full build because there is no valid asset graph.',
143-
),
144-
contains(BuildLog.successPattern),
145-
]),
146-
);
147-
});
148-
149129
test('incremental build after resolve missing import', () async {
150130
final dartSource = File(p.join('lib', 'app.dart'));
151131
dartSource.writeAsStringSync(

_test/test/exception_handling_test.dart

Lines changed: 0 additions & 35 deletions
This file was deleted.

build_runner/test/bootstrap/build_script_generate_test.dart

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,5 @@ builders:
7979
expect(result.stdout, contains('could not be parsed'));
8080
});
8181
});
82-
83-
test('checks builder keys in global_options', () async {
84-
await d.dir('a', [
85-
d.file('build.yaml', '''
86-
global_options:
87-
a:a:
88-
runs_before:
89-
- b:b
90-
'''),
91-
]).create();
92-
93-
final result = await runPub('a', 'run', args: ['build_runner', 'build']);
94-
expect(result.stderr, isEmpty);
95-
expect(
96-
result.stdout,
97-
allOf(
98-
contains('Ignoring `global_options` for unknown builder `a:a`.'),
99-
contains(
100-
'Ignoring `runs_before` in `global_options` '
101-
'referencing unknown builder `b:b`.',
102-
),
103-
),
104-
);
105-
});
10682
});
10783
}

build_runner/test/common/sdk.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Future<ProcessResult> pubGet(String package, {bool offline = true}) async {
2727
return pubGetresult;
2828
}
2929

30+
/// Runs `dart run build_runner` [command] on [package] with [args].
31+
Future<ProcessResult> runBuildRunner(
32+
String package,
33+
String command, {
34+
Iterable<String>? args,
35+
}) => runPub(package, 'run', args: ['build_runner', command, ...?args]);
36+
3037
/// Runs the `pub` [command] on [package] with [args].
3138
Future<ProcessResult> runPub(
3239
String package,
@@ -38,6 +45,13 @@ Future<ProcessResult> runPub(
3845
...?args,
3946
], workingDirectory: p.join(d.sandbox, package));
4047

48+
/// Starts `dart run build_runner` [command] on [package] with [args].
49+
Future<Process> startBuildRunner(
50+
String package,
51+
String command, {
52+
Iterable<String>? args,
53+
}) => startPub(package, 'run', args: ['build_runner', command, ...?args]);
54+
4155
/// Starts the `pub` [command] on [package] with [args].
4256
Future<Process> startPub(
4357
String package,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2025, 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+
@Tags(['integration'])
6+
library;
7+
8+
import 'package:test/test.dart';
9+
10+
import 'build_runner_tester.dart';
11+
import 'fixture_packages.dart';
12+
13+
void main() async {
14+
test('build command --build-filter', () async {
15+
final pubspecs = await Pubspecs.load();
16+
final tester = BuildRunnerTester(pubspecs);
17+
18+
tester.writeFixturePackage(
19+
copyBuilderPackage(buildToCache: true, applyToAllPackages: true),
20+
);
21+
tester.writePackage(
22+
name: 'root_pkg',
23+
dependencies: ['build_runner'],
24+
pathDependencies: ['builder_pkg', 'other_pkg'],
25+
files: {
26+
'lib/a.txt': 'a',
27+
'lib/b.txt': 'b',
28+
'web/a.txt': 'a',
29+
'web/b.txt': 'b',
30+
},
31+
);
32+
tester.writePackage(
33+
name: 'other_pkg',
34+
files: {'lib/a.txt': 'a', 'lib/b.txt': 'b'},
35+
);
36+
37+
await tester.run(
38+
'root_pkg',
39+
'dart run build_runner build '
40+
'--build-filter package:*/a.txt.copy '
41+
'--build-filter web/a.txt.copy ',
42+
);
43+
expect(tester.readFileTree('root_pkg/.dart_tool/build/generated'), {
44+
'root_pkg/web/a.txt.copy': 'a',
45+
'root_pkg/lib/a.txt.copy': 'a',
46+
'other_pkg/lib/a.txt.copy': 'a',
47+
});
48+
});
49+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2025, 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+
@Tags(['integration'])
6+
library;
7+
8+
import 'package:io/io.dart';
9+
import 'package:test/test.dart';
10+
11+
import 'build_runner_tester.dart';
12+
13+
void main() async {
14+
test('build command config validation', () async {
15+
final pubspecs = await Pubspecs.load();
16+
final tester = BuildRunnerTester(pubspecs);
17+
18+
tester.writePackage(
19+
name: 'root_pkg',
20+
dependencies: ['build_runner'],
21+
files: {
22+
'build.yaml': r'''
23+
targets:
24+
$default:
25+
builders:
26+
bad:builder:
27+
enabled: true
28+
''',
29+
},
30+
);
31+
32+
var output = await tester.run('root_pkg', 'dart run build_runner build');
33+
expect(
34+
output,
35+
contains(
36+
'Ignoring options for unknown builder `bad:builder` '
37+
'in target `root_pkg:root_pkg`.',
38+
),
39+
);
40+
41+
tester.write('root_pkg/build.yaml', r'''
42+
global_options:
43+
bad:builder:
44+
options: {}
45+
''');
46+
output = await tester.run('root_pkg', 'dart run build_runner build');
47+
expect(
48+
output,
49+
contains('Ignoring `global_options` for unknown builder `bad:builder`.'),
50+
);
51+
52+
tester.delete('root_pkg/build.yaml');
53+
output = await tester.run(
54+
'root_pkg',
55+
'dart run build_runner build --define=bad:key=foo=bar',
56+
);
57+
expect(
58+
output,
59+
contains('Ignoring options overrides for unknown builder `bad:key`.'),
60+
);
61+
62+
// Compile errors caused by config errors.
63+
tester.write('root_pkg/build.yaml', r'''
64+
builders:
65+
test_builder:
66+
import: 'missing_builder.dart'
67+
builder_factories: ['missingFactory']
68+
build_extensions: {'.txt': ['.txt.copy']}
69+
auto_apply: root_package
70+
build_to: source
71+
''');
72+
output = await tester.run(
73+
'root_pkg',
74+
'dart run build_runner build',
75+
expectOutputCode: ExitCode.config.code,
76+
);
77+
expect(output, contains("Error when reading 'missing_builder.dart'"));
78+
expect(output, contains("Undefined name 'missingFactory'"));
79+
});
80+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2025, 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+
@Tags(['integration'])
6+
library;
7+
8+
import 'package:test/test.dart';
9+
10+
import 'build_runner_tester.dart';
11+
12+
void main() async {
13+
test('build command --define', () async {
14+
final pubspecs = await Pubspecs.load();
15+
final tester = BuildRunnerTester(pubspecs);
16+
17+
tester.writePackage(
18+
name: 'builder_pkg',
19+
dependencies: ['build', 'build_runner'],
20+
files: {
21+
'build.yaml': r'''
22+
builders:
23+
test_builder:
24+
import: 'package:builder_pkg/builder.dart'
25+
builder_factories: ['testBuilderFactory']
26+
build_extensions: {'.txt': ['.txt.copy']}
27+
auto_apply: root_package
28+
build_to: source
29+
''',
30+
'lib/builder.dart': '''
31+
import 'package:build/build.dart';
32+
33+
Builder testBuilderFactory(BuilderOptions options) =>
34+
TestBuilder(AssetId.parse(options.config['copy_from'] as String));
35+
36+
class TestBuilder implements Builder {
37+
final AssetId copyFrom;
38+
39+
TestBuilder(this.copyFrom);
40+
41+
@override
42+
Map<String, List<String>> get buildExtensions => {'.txt': ['.txt.copy']};
43+
44+
@override
45+
Future<void> build(BuildStep buildStep) async {
46+
buildStep.writeAsString(
47+
buildStep.inputId.addExtension('.copy'),
48+
await buildStep.readAsString(copyFrom),
49+
);
50+
}
51+
}''',
52+
},
53+
);
54+
tester.writePackage(
55+
name: 'root_pkg',
56+
dependencies: ['build_runner'],
57+
pathDependencies: ['builder_pkg'],
58+
files: {
59+
'build.yaml': r'''
60+
targets:
61+
$default:
62+
builders:
63+
builder_pkg:test_builder:
64+
options:
65+
copy_from: root_pkg|web/b.txt
66+
''',
67+
'web/a.txt': 'a',
68+
'web/b.txt': 'b',
69+
},
70+
);
71+
72+
// Config in `build.yaml` is the default.
73+
await tester.run('root_pkg', 'dart run build_runner build');
74+
expect(tester.read('root_pkg/web/a.txt.copy'), 'b');
75+
76+
// Override it with `--define`.
77+
await tester.run(
78+
'root_pkg',
79+
'dart run build_runner build '
80+
'--define=builder_pkg:test_builder=copy_from=root_pkg|web/a.txt',
81+
);
82+
expect(tester.read('root_pkg/web/a.txt.copy'), 'a');
83+
});
84+
}

0 commit comments

Comments
 (0)