|
| 1 | +// ignore_for_file: prefer_const_constructors |
| 2 | + |
| 3 | +import 'dart:io'; |
| 4 | + |
| 5 | +import 'package:cli_completion/src/installer/script_configuration_entry.dart'; |
| 6 | +import 'package:path/path.dart' as path; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + group('$ScriptConfigurationEntry', () { |
| 11 | + test('can be instatiated', () { |
| 12 | + expect(() => ScriptConfigurationEntry('name'), returnsNormally); |
| 13 | + }); |
| 14 | + |
| 15 | + test('has a name', () { |
| 16 | + expect(ScriptConfigurationEntry('name').name, 'name'); |
| 17 | + }); |
| 18 | + |
| 19 | + group('appendsTo', () { |
| 20 | + test('returns normally when file exist', () { |
| 21 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 22 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 23 | + |
| 24 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 25 | + final file = File(filePath)..createSync(); |
| 26 | + |
| 27 | + final entry = ScriptConfigurationEntry('name'); |
| 28 | + |
| 29 | + expect( |
| 30 | + () => entry.appendTo(file), |
| 31 | + returnsNormally, |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + test('returns normally when file does not exist', () { |
| 36 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 37 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 38 | + |
| 39 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 40 | + final file = File(filePath); |
| 41 | + |
| 42 | + final entry = ScriptConfigurationEntry('name'); |
| 43 | + |
| 44 | + expect( |
| 45 | + () => entry.appendTo(file), |
| 46 | + returnsNormally, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + test('correctly appends content', () { |
| 51 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 52 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 53 | + |
| 54 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 55 | + final file = File(filePath)..createSync(); |
| 56 | + const initialContent = 'hello world\n'; |
| 57 | + file.writeAsStringSync(initialContent); |
| 58 | + |
| 59 | + final entry = ScriptConfigurationEntry('name'); |
| 60 | + const entryContent = 'hello world'; |
| 61 | + |
| 62 | + entry.appendTo(file, content: entryContent); |
| 63 | + |
| 64 | + final fileContent = file.readAsStringSync(); |
| 65 | + const expectedContent = ''' |
| 66 | +$initialContent |
| 67 | +## [name] |
| 68 | +$entryContent |
| 69 | +## [/name] |
| 70 | +
|
| 71 | +'''; |
| 72 | + expect(fileContent, equals(expectedContent)); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + group('existsIn', () { |
| 77 | + group('returns false', () { |
| 78 | + test('when when file does not exist', () { |
| 79 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 80 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 81 | + |
| 82 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 83 | + final file = File(filePath); |
| 84 | + |
| 85 | + final entry = ScriptConfigurationEntry('name'); |
| 86 | + |
| 87 | + expect(entry.existsIn(file), isFalse); |
| 88 | + }); |
| 89 | + |
| 90 | + test('when file exists without entry', () { |
| 91 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 92 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 93 | + |
| 94 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 95 | + final file = File(filePath)..createSync(); |
| 96 | + |
| 97 | + final entry = ScriptConfigurationEntry('name'); |
| 98 | + |
| 99 | + expect(entry.existsIn(file), isFalse); |
| 100 | + }); |
| 101 | + |
| 102 | + test('when file exists with another entry', () { |
| 103 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 104 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 105 | + |
| 106 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 107 | + final file = File(filePath)..createSync(); |
| 108 | + ScriptConfigurationEntry('other').appendTo(file); |
| 109 | + |
| 110 | + final entry = ScriptConfigurationEntry('name'); |
| 111 | + expect(entry.existsIn(file), isFalse); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + test('returns true when file exists with an entry', () { |
| 116 | + final tempDirectory = Directory.systemTemp.createTempSync(); |
| 117 | + addTearDown(() => tempDirectory.deleteSync(recursive: true)); |
| 118 | + |
| 119 | + final filePath = path.join(tempDirectory.path, 'file'); |
| 120 | + final file = File(filePath)..createSync(); |
| 121 | + |
| 122 | + final entry = ScriptConfigurationEntry('name')..appendTo(file); |
| 123 | + |
| 124 | + expect(entry.existsIn(file), isTrue); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +} |
0 commit comments