Skip to content

Commit 0f7ad15

Browse files
authored
fix(cli): Don't throw if analysis_options.yaml is missing (#376)
1 parent c83ef23 commit 0f7ad15

File tree

4 files changed

+139
-8
lines changed

4 files changed

+139
-8
lines changed

apps/cli/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
- fix: Don't throw if analysis_options.yaml is missing
4+
15
## 1.0.12
26

37
- feat: Support cross-compilation with Dart 3.8

apps/cli/lib/src/analyzer/analysis_options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class AnalysisOptions {
1616
? path
1717
: p.canonicalize(p.join(projectPaths.projectRoot, path));
1818
final analysisOptionsFile = fileSystem.file(path);
19-
if (!await analysisOptionsFile.exists()) {
19+
if (!analysisOptionsFile.existsSync()) {
2020
_logger.finest('No analysis options file detected at $path');
2121
return empty;
2222
}

apps/cli/lib/src/init/migrations/add_analyzer_plugin.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ final class AddAnalyzerPlugin extends ProjectMigration {
77
AddAnalyzerPlugin(super.projectRoot, this.appRoot);
88

99
final String appRoot;
10+
late final parentAnalysisOptionsFile =
11+
fileSystem.directory(appRoot).childFile('analysis_options.yaml');
1012

1113
@override
12-
bool get needsMigration => !fileSystem
13-
.directory(appRoot)
14-
.childFile('analysis_options.yaml')
15-
.readAsStringSync()
16-
.contains('celest');
14+
bool get needsMigration {
15+
if (!parentAnalysisOptionsFile.existsSync()) {
16+
return true;
17+
}
18+
return !parentAnalysisOptionsFile.readAsStringSync().contains('- celest');
19+
}
1720

1821
@override
1922
String get name => 'core.project.add_analyzer_plugin';
2023

2124
@override
2225
Future<ProjectMigrationResult> create() async {
23-
final parentAnalysisOptionsFile =
24-
fileSystem.directory(appRoot).childFile('analysis_options.yaml');
2526
if (parentAnalysisOptionsFile.existsSync()) {
2627
final editor = YamlEditor(await parentAnalysisOptionsFile.readAsString());
2728

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import 'package:celest_cli/src/context.dart';
2+
import 'package:celest_cli/src/init/migrations/add_analyzer_plugin.dart';
3+
import 'package:celest_cli/src/sdk/versions.dart';
4+
import 'package:test/test.dart';
5+
import 'package:test_descriptor/test_descriptor.dart' as d;
6+
import 'package:yaml/yaml.dart';
7+
8+
void main() {
9+
Future<void> testMigration({
10+
required String? analysisOptionsContent,
11+
required bool needsMigration,
12+
required bool addsPlugin,
13+
}) async {
14+
final app = d.dir('app', [
15+
d.file('pubspec.yaml', '''
16+
name: app
17+
18+
environment:
19+
sdk: ^$minSupportedDartSdk
20+
flutter: ">=$minSupportedFlutterSdk"
21+
22+
dependencies:
23+
flutter:
24+
sdk: flutter
25+
'''),
26+
if (analysisOptionsContent != null)
27+
d.file('analysis_options.yaml', analysisOptionsContent),
28+
d.dir('celest', [
29+
d.file('pubspec.yaml', '''
30+
name: celest_backend
31+
'''),
32+
]),
33+
]);
34+
await app.create();
35+
36+
final migration = AddAnalyzerPlugin(d.path('app/celest'), d.path('app'));
37+
expect(migration.needsMigration, needsMigration);
38+
39+
if (needsMigration) {
40+
await migration.create();
41+
}
42+
43+
final analysisOptions = fileSystem.file(
44+
d.path('app/analysis_options.yaml'),
45+
);
46+
expect(analysisOptions.existsSync(), isTrue);
47+
final yaml = loadYaml(analysisOptions.readAsStringSync()) as YamlMap;
48+
expect(
49+
yaml,
50+
isA<YamlMap>().having(
51+
(e) => e['analyzer'],
52+
'analyzer',
53+
isA<YamlMap>().having(
54+
(e) => e['plugins'],
55+
'plugins',
56+
isA<YamlList>().having(
57+
(e) => e.value,
58+
'plugins values',
59+
addsPlugin ? contains('celest') : isNot(contains('celest')),
60+
),
61+
),
62+
),
63+
);
64+
}
65+
66+
group('AddAnalyzerPlugin', () {
67+
test('no analysis_options.yaml', () async {
68+
await testMigration(
69+
analysisOptionsContent: null,
70+
needsMigration: true,
71+
addsPlugin: true,
72+
);
73+
});
74+
75+
test('empty analysis_options.yaml', () async {
76+
await testMigration(
77+
analysisOptionsContent: '',
78+
needsMigration: true,
79+
addsPlugin: true,
80+
);
81+
});
82+
83+
test('no plugins section', () async {
84+
await testMigration(
85+
analysisOptionsContent: '''
86+
analyzer:
87+
errors:
88+
avoid_print: ignore
89+
''',
90+
needsMigration: true,
91+
addsPlugin: true,
92+
);
93+
});
94+
95+
test('no celest plugin', () async {
96+
await testMigration(
97+
analysisOptionsContent: '''
98+
analyzer:
99+
plugins:
100+
- some_other_plugin
101+
errors:
102+
avoid_print: ignore
103+
''',
104+
needsMigration: true,
105+
// Shouldn't replace existing plugin as the current plugin system
106+
// allows only one plugin to be listed.
107+
addsPlugin: false,
108+
);
109+
});
110+
111+
test('celest plugin already present', () async {
112+
await testMigration(
113+
analysisOptionsContent: '''
114+
analyzer:
115+
plugins:
116+
- celest
117+
- some_other_plugin
118+
errors:
119+
avoid_print: ignore
120+
''',
121+
needsMigration: false,
122+
addsPlugin: true,
123+
);
124+
});
125+
});
126+
}

0 commit comments

Comments
 (0)