Skip to content

Commit 885fc31

Browse files
authored
Use pub deps to validate target directory (#21)
Much more robust than just checking for a .packages file
1 parent 79a61e5 commit 885fc31

File tree

3 files changed

+114
-12
lines changed

3 files changed

+114
-12
lines changed

webdev/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Remove check for `build_web_compilers`. Allows general support for
44
`build_runner` from tools.
5+
- Use `pub deps` to validate target directory.
56

67
## 0.1.1
78

webdev/lib/src/pubspec.dart

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,28 @@ class PackageExceptionDetails {
3737
String toString() => [error, description].join('\n');
3838
}
3939

40-
Future checkPubspecLock() async {
41-
var file = new File('pubspec.lock');
42-
if (!file.existsSync()) {
43-
throw new PackageException._([PackageExceptionDetails.noPubspecLock]);
40+
Future _runPubDeps() async {
41+
var result = Process.runSync('pub', ['deps']);
42+
43+
if (result.exitCode == 65 || result.exitCode == 66) {
44+
throw new PackageException._(
45+
[new PackageExceptionDetails._((result.stderr as String).trim())]);
46+
}
47+
48+
if (result.exitCode != 0) {
49+
throw new ProcessException(
50+
'pub',
51+
['deps'],
52+
'***OUT***\n${result.stdout}\n***ERR***\n${result.stderr}\n***',
53+
exitCode);
4454
}
55+
}
56+
57+
Future checkPubspecLock() async {
58+
await _runPubDeps();
4559

46-
var pubspecLock = loadYaml(await file.readAsString()) as YamlMap;
60+
var pubspecLock =
61+
loadYaml(await new File('pubspec.lock').readAsString()) as YamlMap;
4762

4863
var packages = pubspecLock['packages'] as YamlMap;
4964

webdev/test/integration_test.dart

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:io';
67

78
import 'package:path/path.dart' as p;
@@ -45,6 +46,10 @@ A dependency on `build_runner` was not found.'''));
4546
group('should fail when `build_runner` is the wrong version', () {
4647
for (var version in ['0.7.13+1', '0.9.0']) {
4748
test(version, () async {
49+
await d.file('pubspec.yaml', '''
50+
name: sample
51+
''').create();
52+
4853
await d.file('pubspec.lock', '''
4954
# Copy-pasted from a valid run
5055
packages:
@@ -57,6 +62,9 @@ packages:
5762
version: "$version"
5863
''').create();
5964

65+
await d.file('.packages', '''
66+
''').create();
67+
6068
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
6169
workingDirectory: d.sandbox);
6270

@@ -71,7 +79,38 @@ packages:
7179
}
7280
});
7381

74-
test('should fail gracefully if there is no .packages file', () async {
82+
test('no pubspec.yaml', () async {
83+
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
84+
workingDirectory: d.sandbox);
85+
86+
var output = await process.stdoutStream().join('\n');
87+
88+
expect(output, contains('Could not run in the current directory.'));
89+
expect(output, contains('Could not find a file named "pubspec.yaml"'));
90+
await process.shouldExit(78);
91+
});
92+
93+
test('pubspec.yaml, no pubspec.lock', () async {
94+
await d.file('pubspec.yaml', '''
95+
name: sample
96+
''').create();
97+
98+
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
99+
workingDirectory: d.sandbox);
100+
101+
var output = await process.stdoutStream().join('\n');
102+
103+
expect(output, contains('Could not run in the current directory.'));
104+
expect(output,
105+
contains('No pubspec.lock file found, please run "pub get" first.'));
106+
await process.shouldExit(78);
107+
});
108+
109+
test('pubspec.yaml, pubspec.lock, no .packages', () async {
110+
await d.file('pubspec.yaml', '''
111+
name: sample
112+
''').create();
113+
75114
await d.file('pubspec.lock', '''
76115
# Copy-pasted from a valid run
77116
packages:
@@ -87,14 +126,19 @@ packages:
87126
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
88127
workingDirectory: d.sandbox);
89128

90-
await expectLater(
91-
process.stdout, emits('Could not run in the current directory.'));
92-
await expectLater(process.stdout,
93-
emits('A `.packages` file does not exist in the target directory.'));
129+
var output = await process.stdoutStream().join('\n');
130+
131+
expect(output, contains('Could not run in the current directory.'));
132+
expect(output,
133+
contains('No .packages file found, please run "pub get" first.'));
94134
await process.shouldExit(78);
95135
});
96136

97137
test('should fail gracefully if there is an isolate error', () async {
138+
await d.file('pubspec.yaml', '''
139+
name: sample
140+
''').create();
141+
98142
await d.file('pubspec.lock', '''
99143
# Copy-pasted from a valid run
100144
packages:
@@ -112,11 +156,53 @@ packages:
112156
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
113157
workingDirectory: d.sandbox);
114158

115-
await expectLater(
116-
process.stdout, emits('An unexpected exception has occurred.'));
159+
var output = await process.stdoutStream().join('\n');
160+
161+
expect(output, contains('An unexpected exception has occurred.'));
162+
163+
// The isolate will fail - broken .packages file
164+
expect(output, contains('Unable to spawn isolate'));
117165
await process.shouldExit(70);
118166
});
119167

168+
test('should fail if there has been a dependency change', () async {
169+
await d.file('pubspec.lock', '''
170+
# Copy-pasted from a valid run
171+
packages:
172+
build_runner:
173+
dependency: "direct main"
174+
description:
175+
name: build_runner
176+
url: "https://pub.dartlang.org"
177+
source: hosted
178+
version: "0.8.0"
179+
''').create();
180+
181+
await d.file('.packages', '').create();
182+
183+
// Ensure there is a noticeable delta in the creation times
184+
await new Future.delayed(const Duration(milliseconds: 500));
185+
186+
await d.file('pubspec.yaml', '''
187+
name: sample
188+
dependencies:
189+
args: ^1.0.0
190+
''').create();
191+
192+
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
193+
workingDirectory: d.sandbox);
194+
195+
var output = await process.stdoutStream().join('\n');
196+
197+
expect(output, contains('Could not run in the current directory.'));
198+
expect(
199+
output,
200+
contains(
201+
'The pubspec.yaml file has changed since the pubspec.lock file '
202+
'was generated, please run "pub get" again.'));
203+
await process.shouldExit(78);
204+
});
205+
120206
test('should succeed with valid configuration', () async {
121207
var exampleDirectory = p.absolute(p.join(p.current, '..', 'example'));
122208
var process = await TestProcess.start('pub', ['get'],

0 commit comments

Comments
 (0)