|
| 1 | +// ignore_for_file: prefer_const_constructors |
| 2 | + |
| 3 | +import 'dart:collection'; |
| 4 | +import 'dart:io'; |
| 5 | + |
| 6 | +import 'package:cli_completion/installer.dart'; |
| 7 | +import 'package:path/path.dart' as path; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + group('$CompletionConfiguration', () { |
| 12 | + final testUninstalls = UnmodifiableMapView({ |
| 13 | + SystemShell.bash: UnmodifiableSetView({'very_bad'}), |
| 14 | + }); |
| 15 | + |
| 16 | + group('fromFile', () { |
| 17 | + test( |
| 18 | + 'returns empty cache when file does not exist', |
| 19 | + () { |
| 20 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 21 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 22 | + |
| 23 | + final file = File(path.join(tempDirectory.path, 'config.json')); |
| 24 | + expect( |
| 25 | + file.existsSync(), |
| 26 | + isFalse, |
| 27 | + reason: 'File should not exist', |
| 28 | + ); |
| 29 | + |
| 30 | + final cache = CompletionConfiguration.fromFile(file); |
| 31 | + expect( |
| 32 | + cache.uninstalls, |
| 33 | + isEmpty, |
| 34 | + reason: 'Uninstalls should be initially empty', |
| 35 | + ); |
| 36 | + }, |
| 37 | + ); |
| 38 | + |
| 39 | + test('returns empty cache when file is empty', () { |
| 40 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 41 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 42 | + |
| 43 | + final file = File(path.join(tempDirectory.path, 'config.json')) |
| 44 | + ..writeAsStringSync(''); |
| 45 | + |
| 46 | + final cache = CompletionConfiguration.fromFile(file); |
| 47 | + expect( |
| 48 | + cache.uninstalls, |
| 49 | + isEmpty, |
| 50 | + reason: 'Uninstalls should be initially empty', |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + test("returns a $CompletionConfiguration with the file's defined members", |
| 55 | + () { |
| 56 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 57 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 58 | + |
| 59 | + final file = File(path.join(tempDirectory.path, 'config.json')); |
| 60 | + final cache = CompletionConfiguration.empty().copyWith( |
| 61 | + uninstalls: testUninstalls, |
| 62 | + )..writeTo(file); |
| 63 | + |
| 64 | + final newCache = CompletionConfiguration.fromFile(file); |
| 65 | + expect( |
| 66 | + newCache.uninstalls, |
| 67 | + cache.uninstalls, |
| 68 | + reason: 'Uninstalls should match those defined in the file', |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + test( |
| 73 | + '''returns a $CompletionConfiguration with empty uninstalls if the file's JSON "uninstalls" key has a string value''', |
| 74 | + () { |
| 75 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 76 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 77 | + |
| 78 | + const json = '{"uninstalls": "very_bad"}'; |
| 79 | + final file = File(path.join(tempDirectory.path, 'config.json')) |
| 80 | + ..writeAsStringSync(json); |
| 81 | + |
| 82 | + final cache = CompletionConfiguration.fromFile(file); |
| 83 | + expect( |
| 84 | + cache.uninstalls, |
| 85 | + isEmpty, |
| 86 | + reason: |
| 87 | + '''Uninstalls should be empty when the value is of an invalid type''', |
| 88 | + ); |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + test( |
| 93 | + '''returns a $CompletionConfiguration with empty uninstalls if file's JSON "uninstalls" key has a numeric value''', |
| 94 | + () { |
| 95 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 96 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 97 | + |
| 98 | + const json = '{"uninstalls": 1}'; |
| 99 | + final file = File(path.join(tempDirectory.path, 'config.json')) |
| 100 | + ..writeAsStringSync(json); |
| 101 | + |
| 102 | + final cache = CompletionConfiguration.fromFile(file); |
| 103 | + expect( |
| 104 | + cache.uninstalls, |
| 105 | + isEmpty, |
| 106 | + reason: |
| 107 | + '''Uninstalls should be empty when the value is of an invalid type''', |
| 108 | + ); |
| 109 | + }, |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + group('writeTo', () { |
| 114 | + test('creates a file when it does not exist', () { |
| 115 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 116 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 117 | + |
| 118 | + final file = File(path.join(tempDirectory.path, 'config.json')); |
| 119 | + expect( |
| 120 | + file.existsSync(), |
| 121 | + isFalse, |
| 122 | + reason: 'File should not exist', |
| 123 | + ); |
| 124 | + |
| 125 | + CompletionConfiguration.empty().writeTo(file); |
| 126 | + |
| 127 | + expect( |
| 128 | + file.existsSync(), |
| 129 | + isTrue, |
| 130 | + reason: 'File should exist after cache creation', |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + test('returns normally when file already exists', () { |
| 135 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 136 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 137 | + |
| 138 | + final file = File(path.join(tempDirectory.path, 'config.json')) |
| 139 | + ..createSync(); |
| 140 | + expect( |
| 141 | + file.existsSync(), |
| 142 | + isTrue, |
| 143 | + reason: 'File should exist', |
| 144 | + ); |
| 145 | + |
| 146 | + expect( |
| 147 | + () => CompletionConfiguration.empty().writeTo(file), |
| 148 | + returnsNormally, |
| 149 | + reason: 'Should not throw when file exists', |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + test('content can be read succesfully after written', () { |
| 154 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 155 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 156 | + |
| 157 | + final file = File(path.join(tempDirectory.path, 'config.json')); |
| 158 | + final cache = CompletionConfiguration.empty().copyWith( |
| 159 | + uninstalls: testUninstalls, |
| 160 | + )..writeTo(file); |
| 161 | + |
| 162 | + final newCache = CompletionConfiguration.fromFile(file); |
| 163 | + expect( |
| 164 | + newCache.uninstalls, |
| 165 | + cache.uninstalls, |
| 166 | + reason: 'Uninstalls should match those defined in the file', |
| 167 | + ); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + group('copyWith', () { |
| 172 | + test('members remain unchanged when nothing is specified', () { |
| 173 | + final cache = CompletionConfiguration.empty(); |
| 174 | + final newCache = cache.copyWith(); |
| 175 | + |
| 176 | + expect( |
| 177 | + newCache.uninstalls, |
| 178 | + cache.uninstalls, |
| 179 | + reason: 'Uninstalls should remain unchanged', |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + test('modifies uninstalls when specified', () { |
| 184 | + final cache = CompletionConfiguration.empty(); |
| 185 | + final uninstalls = testUninstalls; |
| 186 | + final newCache = cache.copyWith(uninstalls: uninstalls); |
| 187 | + |
| 188 | + expect( |
| 189 | + newCache.uninstalls, |
| 190 | + equals(uninstalls), |
| 191 | + reason: 'Uninstalls should be modified', |
| 192 | + ); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | +} |
0 commit comments