Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

- fix: Don't throw if analysis_options.yaml is missing

## 1.0.12

- feat: Support cross-compilation with Dart 3.8
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/lib/src/analyzer/analysis_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class AnalysisOptions {
? path
: p.canonicalize(p.join(projectPaths.projectRoot, path));
final analysisOptionsFile = fileSystem.file(path);
if (!await analysisOptionsFile.exists()) {
if (!analysisOptionsFile.existsSync()) {
_logger.finest('No analysis options file detected at $path');
return empty;
}
Expand Down
15 changes: 8 additions & 7 deletions apps/cli/lib/src/init/migrations/add_analyzer_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ final class AddAnalyzerPlugin extends ProjectMigration {
AddAnalyzerPlugin(super.projectRoot, this.appRoot);

final String appRoot;
late final parentAnalysisOptionsFile =
fileSystem.directory(appRoot).childFile('analysis_options.yaml');

@override
bool get needsMigration => !fileSystem
.directory(appRoot)
.childFile('analysis_options.yaml')
.readAsStringSync()
.contains('celest');
bool get needsMigration {
if (!parentAnalysisOptionsFile.existsSync()) {
return true;
}
return !parentAnalysisOptionsFile.readAsStringSync().contains('- celest');
}

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

@override
Future<ProjectMigrationResult> create() async {
final parentAnalysisOptionsFile =
fileSystem.directory(appRoot).childFile('analysis_options.yaml');
if (parentAnalysisOptionsFile.existsSync()) {
final editor = YamlEditor(await parentAnalysisOptionsFile.readAsString());

Expand Down
126 changes: 126 additions & 0 deletions apps/cli/test/init/migrations/add_analyzer_plugin_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import 'package:celest_cli/src/context.dart';
import 'package:celest_cli/src/init/migrations/add_analyzer_plugin.dart';
import 'package:celest_cli/src/sdk/versions.dart';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:yaml/yaml.dart';

void main() {
Future<void> testMigration({
required String? analysisOptionsContent,
required bool needsMigration,
required bool addsPlugin,
}) async {
final app = d.dir('app', [
d.file('pubspec.yaml', '''
name: app

environment:
sdk: ^$minSupportedDartSdk
flutter: ">=$minSupportedFlutterSdk"

dependencies:
flutter:
sdk: flutter
'''),
if (analysisOptionsContent != null)
d.file('analysis_options.yaml', analysisOptionsContent),
d.dir('celest', [
d.file('pubspec.yaml', '''
name: celest_backend
'''),
]),
]);
await app.create();

final migration = AddAnalyzerPlugin(d.path('app/celest'), d.path('app'));
expect(migration.needsMigration, needsMigration);

if (needsMigration) {
await migration.create();
}

final analysisOptions = fileSystem.file(
d.path('app/analysis_options.yaml'),
);
expect(analysisOptions.existsSync(), isTrue);
final yaml = loadYaml(analysisOptions.readAsStringSync()) as YamlMap;
expect(
yaml,
isA<YamlMap>().having(
(e) => e['analyzer'],
'analyzer',
isA<YamlMap>().having(
(e) => e['plugins'],
'plugins',
isA<YamlList>().having(
(e) => e.value,
'plugins values',
addsPlugin ? contains('celest') : isNot(contains('celest')),
),
),
),
);
}

group('AddAnalyzerPlugin', () {
test('no analysis_options.yaml', () async {
await testMigration(
analysisOptionsContent: null,
needsMigration: true,
addsPlugin: true,
);
});

test('empty analysis_options.yaml', () async {
await testMigration(
analysisOptionsContent: '',
needsMigration: true,
addsPlugin: true,
);
});

test('no plugins section', () async {
await testMigration(
analysisOptionsContent: '''
analyzer:
errors:
avoid_print: ignore
''',
needsMigration: true,
addsPlugin: true,
);
});

test('no celest plugin', () async {
await testMigration(
analysisOptionsContent: '''
analyzer:
plugins:
- some_other_plugin
errors:
avoid_print: ignore
''',
needsMigration: true,
// Shouldn't replace existing plugin as the current plugin system
// allows only one plugin to be listed.
addsPlugin: false,
);
});

test('celest plugin already present', () async {
await testMigration(
analysisOptionsContent: '''
analyzer:
plugins:
- celest
- some_other_plugin
errors:
avoid_print: ignore
''',
needsMigration: false,
addsPlugin: true,
);
});
});
}