Skip to content

Commit 1253793

Browse files
committed
test: add more unit tests
1 parent ab062ef commit 1253793

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

test/crate_resolver_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'dart:io';
2+
3+
import 'package:native_toolchain_rs/src/crate_resolver.dart';
4+
import 'package:native_toolchain_rs/src/exception.dart';
5+
import 'package:path/path.dart' as path;
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('CrateDirectoryResolver', () {
10+
late Directory tempDir;
11+
const resolver = CrateDirectoryResolver();
12+
13+
setUp(() {
14+
tempDir = Directory.systemTemp.createTempSync('crate_resolver_test');
15+
});
16+
17+
tearDown(() {
18+
tempDir.deleteSync(recursive: true);
19+
});
20+
21+
test('resolveCrateDirectory returns the correct directory', () {
22+
final crateDir = Directory(path.join(tempDir.path, 'crate'))
23+
..createSync();
24+
25+
final result = resolver.resolveCrateDirectory(
26+
rootPath: tempDir.path,
27+
cratePathOptions: ['crate'],
28+
);
29+
30+
expect(result.path, equals(crateDir.path));
31+
});
32+
33+
test(
34+
'resolveCrateDirectory throws RustValidationException '
35+
'if no directory exists',
36+
() {
37+
expect(
38+
() => resolver.resolveCrateDirectory(
39+
rootPath: tempDir.path,
40+
cratePathOptions: ['non_existent_crate'],
41+
),
42+
throwsA(isA<RustValidationException>()),
43+
);
44+
},
45+
);
46+
47+
test('resolveCrateDirectory returns the first existing directory', () {
48+
final crateDir1 = Directory(path.join(tempDir.path, 'crate1'));
49+
final crateDir2 = Directory(path.join(tempDir.path, 'crate2'));
50+
crateDir1.createSync();
51+
crateDir2.createSync();
52+
53+
final result = resolver.resolveCrateDirectory(
54+
rootPath: tempDir.path,
55+
cratePathOptions: ['crate1', 'crate2'],
56+
);
57+
58+
expect(result.path, equals(crateDir1.path));
59+
});
60+
});
61+
}

test/exception_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:native_toolchain_rs/src/exception.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group('RustValidationException', () {
6+
test('compose returns results when no exceptions are thrown', () {
7+
final results = RustValidationException.compose([
8+
() => 1,
9+
() => 2,
10+
]);
11+
expect(results, equals([1, 2]));
12+
});
13+
14+
test('compose throws aggregate exception', () {
15+
expect(
16+
() => RustValidationException.compose([
17+
() => throw const RustValidationException(['error1']),
18+
() => 1,
19+
() => throw const RustValidationException(['error2', 'error3']),
20+
]),
21+
throwsA(
22+
isA<RustValidationException>().having(
23+
(e) => e.validationErrors,
24+
'validationErrors',
25+
equals(['error1', 'error2', 'error3']),
26+
),
27+
),
28+
);
29+
});
30+
31+
test('compose does not catch other exceptions', () {
32+
expect(
33+
() => RustValidationException.compose([
34+
() => throw Exception('some other error'),
35+
]),
36+
throwsA(isA<Exception>()),
37+
);
38+
});
39+
});
40+
}

test/process_runner_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dart:io';
2+
3+
import 'package:logging/logging.dart';
4+
import 'package:native_toolchain_rs/src/exception.dart';
5+
import 'package:native_toolchain_rs/src/process_runner.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('ProcessRunner', () {
10+
late ProcessRunner processRunner;
11+
late List<LogRecord> records;
12+
13+
setUp(() {
14+
records = [];
15+
final logger = Logger.detached('test')..onRecord.listen(records.add);
16+
processRunner = ProcessRunner(logger);
17+
});
18+
19+
test('invoke succeeds with exit code 0', () async {
20+
final result = await processRunner.invoke('echo', ['hello']);
21+
expect(result.trim(), equals('hello'));
22+
expect(records, hasLength(1));
23+
expect(records.single.message, contains('Invoking "echo [hello]'));
24+
});
25+
26+
test('invoke throws RustProcessException on non-zero exit code', () {
27+
expect(
28+
() => processRunner.invoke('dart', ['run', 'non_existent_file.dart']),
29+
throwsA(isA<RustProcessException>()),
30+
);
31+
});
32+
33+
test('invoke throws ProcessException on command not found', () {
34+
expect(
35+
() => processRunner.invoke('command_not_found', []),
36+
throwsA(isA<ProcessException>()),
37+
);
38+
});
39+
});
40+
}

0 commit comments

Comments
 (0)