Skip to content

Commit ab062ef

Browse files
committed
test: add unit tests
Fixes #3
1 parent 529a007 commit ab062ef

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ jobs:
4747
run: flutter analyze --fatal-infos --fatal-warnings
4848
- name: Dart format
4949
run: dart format --set-exit-if-changed .
50-
# - name: Dart tests
51-
# run: melos run test
50+
- name: Dart tests
51+
run: melos run test
5252

5353
# macos_integration_test:
5454
# runs-on: macos-latest

pubspec.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ dev_dependencies:
2323

2424
melos:
2525
useRootAsPackage: true
26+
scripts:
27+
test:
28+
description: Run unit tests for a specific package in this project.
29+
run: flutter test test
30+
packageFilters:
31+
dirExists: test
32+
exec:
33+
concurrency: 1
34+
failFast: true

test/toml_parsing_test.dart

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import 'dart:io';
2+
3+
import 'package:native_toolchain_rs/src/exception.dart';
4+
import 'package:native_toolchain_rs/src/toml_parsing.dart';
5+
import 'package:test/test.dart';
6+
7+
void main() {
8+
group('TomlDocumentWrapper', () {
9+
late Directory tempDir;
10+
late String tempFilePath;
11+
12+
setUp(() {
13+
tempDir = Directory.systemTemp.createTempSync();
14+
tempFilePath = '${tempDir.path}/test.toml';
15+
});
16+
17+
tearDown(() {
18+
tempDir.deleteSync(recursive: true);
19+
});
20+
21+
test('walk returns value when path is valid', () {
22+
File(tempFilePath).writeAsStringSync('key = "value"\n');
23+
const factory = TomlDocumentWrapperFactory(null);
24+
final wrapper = factory.parseFile(tempFilePath);
25+
expect(wrapper.walk<String>('key'), 'value');
26+
});
27+
28+
test('walk throws RustValidationException when path is invalid', () {
29+
File(tempFilePath).writeAsStringSync('key = "value"\n');
30+
const factory = TomlDocumentWrapperFactory(null);
31+
final wrapper = factory.parseFile(tempFilePath);
32+
expect(
33+
() => wrapper.walk<String>('invalid.path'),
34+
throwsA(isA<RustValidationException>()),
35+
);
36+
});
37+
});
38+
39+
group('CargoManifestParser', () {
40+
const tomlDocumentFactory = TomlDocumentWrapperFactory(null);
41+
const parser = CargoManifestParser(null, tomlDocumentFactory);
42+
late Directory tempDir;
43+
late String tempManifestPath;
44+
45+
setUp(() {
46+
tempDir = Directory.systemTemp.createTempSync();
47+
tempManifestPath = '${tempDir.path}/Cargo.toml';
48+
});
49+
50+
tearDown(() {
51+
tempDir.deleteSync(recursive: true);
52+
});
53+
54+
test('parseManifest returns crate name and lib crate types', () {
55+
File(tempManifestPath).writeAsStringSync('''
56+
[package]
57+
name = "my_crate"
58+
59+
[lib]
60+
crate-type = ["staticlib"]
61+
''');
62+
63+
final result = parser.parseManifest(tempManifestPath);
64+
65+
expect(result.crateName, 'my_crate');
66+
expect(result.libCrateTypes, ['staticlib']);
67+
});
68+
69+
test('parseManifest throws when Cargo.toml not found', () {
70+
expect(
71+
() => parser.parseManifest('non_existent_Cargo.toml'),
72+
throwsA(isA<RustValidationException>()),
73+
);
74+
});
75+
76+
test('parseManifest throws when parsing fails', () {
77+
File(tempManifestPath).writeAsStringSync('invalid toml');
78+
79+
expect(
80+
() => parser.parseManifest(tempManifestPath),
81+
throwsA(isA<RustValidationException>()),
82+
);
83+
});
84+
});
85+
86+
group('ToolchainTomlParser', () {
87+
const tomlDocumentFactory = TomlDocumentWrapperFactory(null);
88+
const parser = ToolchainTomlParser(null, tomlDocumentFactory);
89+
late Directory tempDir;
90+
late String tempToolchainTomlPath;
91+
92+
setUp(() {
93+
tempDir = Directory.systemTemp.createTempSync();
94+
tempToolchainTomlPath = '${tempDir.path}/rust-toolchain.toml';
95+
});
96+
97+
tearDown(() {
98+
tempDir.deleteSync(recursive: true);
99+
});
100+
101+
test('parseToolchainToml returns channel and targets', () {
102+
File(tempToolchainTomlPath).writeAsStringSync('''
103+
[toolchain]
104+
channel = "stable"
105+
targets = ["aarch64-apple-darwin"]
106+
''');
107+
108+
final result = parser.parseToolchainToml(tempToolchainTomlPath);
109+
110+
expect(result.channel, 'stable');
111+
expect(result.targets, {'aarch64-apple-darwin'});
112+
});
113+
114+
test('parseToolchainToml throws when rust-toolchain.toml not found', () {
115+
expect(
116+
() => parser.parseToolchainToml('non_existent_rust-toolchain.toml'),
117+
throwsA(isA<RustValidationException>()),
118+
);
119+
});
120+
121+
test('parseToolchainToml throws when parsing fails', () {
122+
File(tempToolchainTomlPath).writeAsStringSync('invalid toml');
123+
124+
expect(
125+
() => parser.parseToolchainToml(tempToolchainTomlPath),
126+
throwsA(isA<RustValidationException>()),
127+
);
128+
});
129+
});
130+
}

0 commit comments

Comments
 (0)