Skip to content

Commit 19b96cb

Browse files
authored
test(dart_frog_cli): dart_frog build e2e tests (#74)
1 parent 5ca6a20 commit 19b96cb

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed

packages/dart_frog_cli/e2e/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ environment:
66
sdk: ">=2.17.0 <3.0.0"
77

88
dev_dependencies:
9+
dart_frog:
10+
path: ../../dart_frog
911
http: ^0.13.4
1012
meta: ^1.7.0
1113
path: ^1.8.2
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'dart:io';
2+
import 'dart:isolate';
3+
4+
import 'package:http/http.dart' as http;
5+
import 'package:path/path.dart' as path;
6+
import 'package:test/test.dart';
7+
8+
import 'helpers/helpers.dart';
9+
10+
/// Objectives:
11+
///
12+
/// * Generate a new Dart Frog project via `dart_frog create`
13+
/// * Generate a production build via `dart_frog build`
14+
/// * Ensure the server responds accordingly for built-in endpoints
15+
void main() {
16+
group('dart_frog build', () {
17+
const projectName = 'example';
18+
final tempDirectory = Directory.systemTemp.createTempSync();
19+
final projectDirectory = Directory(
20+
path.join(tempDirectory.path, projectName),
21+
);
22+
23+
late Isolate isolate;
24+
25+
setUpAll(() async {
26+
await dartFrogCreate(projectName: projectName, directory: tempDirectory);
27+
await dartFrogBuild(directory: projectDirectory);
28+
isolate = await Isolate.spawnUri(
29+
Uri.file(
30+
path.join(projectDirectory.path, 'build', 'bin', 'server.dart'),
31+
),
32+
[],
33+
null,
34+
);
35+
});
36+
37+
tearDownAll(() async {
38+
isolate.kill();
39+
await tempDirectory.delete(recursive: true);
40+
});
41+
42+
testServer('GET / returns 200 with greeting', (host) async {
43+
final response = await http.get(Uri.parse(host));
44+
expect(response.statusCode, equals(HttpStatus.ok));
45+
expect(response.body, equals('Welcome to Dart Frog!'));
46+
expect(response.headers, contains('date'));
47+
expect(
48+
response.headers,
49+
containsPair('content-type', 'text/plain; charset=utf-8'),
50+
);
51+
});
52+
53+
testServer('GET /not_here returns 404', (host) async {
54+
final response = await http.get(Uri.parse('$host/not_here'));
55+
expect(response.statusCode, HttpStatus.notFound);
56+
expect(response.body, 'Route not found');
57+
});
58+
});
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:io';
2+
3+
Future<void> dartFrogBuild({
4+
required Directory directory,
5+
}) async {
6+
final result = await Process.run(
7+
'dart_frog',
8+
['build'],
9+
workingDirectory: directory.path,
10+
runInShell: true,
11+
);
12+
13+
if (result.exitCode != 0) {
14+
throw Exception('dart_frog build exited with code ${result.exitCode}');
15+
}
16+
}

packages/dart_frog_cli/e2e/test/helpers/dart_frog_create.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Future<void> dartFrogCreate({
1010
workingDirectory: directory.path,
1111
runInShell: true,
1212
);
13+
1314
if (result.exitCode != 0) {
14-
throw Exception('dart_frog create exited with code $exitCode');
15+
throw Exception('dart_frog create exited with code ${result.exitCode}');
1516
}
1617
}

packages/dart_frog_cli/e2e/test/helpers/helpers.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export 'dart_analyze.dart';
22
export 'dart_format.dart';
3+
export 'dart_frog_build.dart';
34
export 'dart_frog_create.dart';
45
export 'dart_frog_dev.dart';
56
export 'dart_tst.dart';

packages/dart_frog_cli/e2e/test/helpers/kill_dart_frog_server.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ Future<void> killDartFrogServer(int pid) async {
77
['/F', '/T', '/PID', '$pid'],
88
runInShell: true,
99
);
10+
1011
if (result.exitCode != 0) {
1112
throw Exception(
1213
'taskkill /F /T /PID $pid exited with code ${result.exitCode}',
1314
);
1415
}
16+
1517
return;
1618
}
1719

1820
if (Platform.isLinux || Platform.isMacOS) {
1921
final result = await Process.run('pkill', ['-f', 'dart_frog']);
22+
2023
if (result.exitCode != 0) {
2124
throw Exception('pkill -f dart_frog exited with code ${result.exitCode}');
2225
}
26+
2327
return;
2428
}
2529
}

0 commit comments

Comments
 (0)