|
| 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