|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 2 | + |
| 3 | +const { mockFs, mockPrompter } = require('../../mocks/mockAll'); |
| 4 | + |
| 5 | +import path from 'path'; |
| 6 | +import { Constants } from '../../../constants'; |
| 7 | +import { |
| 8 | + gradlePropertiesTask, |
| 9 | + runTask, |
| 10 | +} from '../../../tasks/gradlePropertiesTask'; |
| 11 | +import { GradlePropertiesTaskType } from '../../../types/mod.types'; |
| 12 | + |
| 13 | +describe('gradlePropertiesTask', () => { |
| 14 | + it('should prepend text into empty body ', async () => { |
| 15 | + let content = ''; |
| 16 | + const task: GradlePropertiesTaskType = { |
| 17 | + task: 'gradle_properties', |
| 18 | + actions: [ |
| 19 | + { |
| 20 | + append: 'ignoredfile', |
| 21 | + prepend: 'ignoredfile', |
| 22 | + }, |
| 23 | + ], |
| 24 | + }; |
| 25 | + content = await gradlePropertiesTask({ |
| 26 | + configPath: 'path/to/config', |
| 27 | + task: task, |
| 28 | + content, |
| 29 | + packageName: 'test-package', |
| 30 | + }); |
| 31 | + content = await gradlePropertiesTask({ |
| 32 | + configPath: 'path/to/config', |
| 33 | + task: task, |
| 34 | + content, |
| 35 | + packageName: 'test-package', |
| 36 | + }); |
| 37 | + expect(content).toEqual(` |
| 38 | +ignoredfile |
| 39 | +`); |
| 40 | + }); |
| 41 | + it('should prepend text into empty body without block', async () => { |
| 42 | + let content = ''; |
| 43 | + const task: GradlePropertiesTaskType = { |
| 44 | + task: 'gradle_properties', |
| 45 | + actions: [ |
| 46 | + { |
| 47 | + append: 'ignoredfile', |
| 48 | + prepend: 'ignoredfile', |
| 49 | + }, |
| 50 | + ], |
| 51 | + }; |
| 52 | + content = await gradlePropertiesTask({ |
| 53 | + configPath: 'path/to/config', |
| 54 | + task: task, |
| 55 | + content, |
| 56 | + packageName: 'test-package', |
| 57 | + }); |
| 58 | + content = await gradlePropertiesTask({ |
| 59 | + configPath: 'path/to/config', |
| 60 | + task: task, |
| 61 | + content, |
| 62 | + packageName: 'test-package', |
| 63 | + }); |
| 64 | + expect(content).toEqual(` |
| 65 | +ignoredfile |
| 66 | +`); |
| 67 | + }); |
| 68 | + it('should skip insert when ifNotPresent exists', async () => { |
| 69 | + const content = ` |
| 70 | +someignored |
| 71 | +`; |
| 72 | + const task: GradlePropertiesTaskType = { |
| 73 | + task: 'gradle_properties', |
| 74 | + actions: [ |
| 75 | + { |
| 76 | + ifNotPresent: 'someignored', |
| 77 | + prepend: 'ignored', |
| 78 | + }, |
| 79 | + { |
| 80 | + ifNotPresent: 'someignored', |
| 81 | + append: 'ignored', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }; |
| 85 | + |
| 86 | + await gradlePropertiesTask({ |
| 87 | + configPath: 'path/to/config', |
| 88 | + task: task, |
| 89 | + content, |
| 90 | + packageName: 'test-package', |
| 91 | + }); |
| 92 | + expect(mockPrompter.log.message).toHaveBeenCalledWith( |
| 93 | + expect.stringContaining('found existing ') |
| 94 | + ); |
| 95 | + }); |
| 96 | + it('should prepend text into partial body ', async () => { |
| 97 | + let content = ` |
| 98 | +ignoredfile |
| 99 | +`; |
| 100 | + const task: GradlePropertiesTaskType = { |
| 101 | + task: 'gradle_properties', |
| 102 | + actions: [ |
| 103 | + { |
| 104 | + prepend: 'ignoredfile', |
| 105 | + }, |
| 106 | + ], |
| 107 | + }; |
| 108 | + |
| 109 | + content = await gradlePropertiesTask({ |
| 110 | + configPath: 'path/to/config', |
| 111 | + task: task, |
| 112 | + content, |
| 113 | + packageName: 'test-package', |
| 114 | + }); |
| 115 | + expect(content).toEqual(` |
| 116 | +ignoredfile |
| 117 | +`); |
| 118 | + }); |
| 119 | + it('should append text into existing body ', async () => { |
| 120 | + let content = ` |
| 121 | +ignoredfile |
| 122 | +`; |
| 123 | + const task: GradlePropertiesTaskType = { |
| 124 | + task: 'gradle_properties', |
| 125 | + actions: [ |
| 126 | + { |
| 127 | + append: 'config2 = use_native_modules!', |
| 128 | + }, |
| 129 | + ], |
| 130 | + }; |
| 131 | + content = await gradlePropertiesTask({ |
| 132 | + configPath: 'path/to/config', |
| 133 | + task: task, |
| 134 | + content, |
| 135 | + packageName: 'test-package', |
| 136 | + }); |
| 137 | + expect(content).toEqual(` |
| 138 | +ignoredfile |
| 139 | +config2 = use_native_modules! |
| 140 | +`); |
| 141 | + }); |
| 142 | + it('should insert text after point with comment', async () => { |
| 143 | + let content = ` |
| 144 | +ignoredfile |
| 145 | +`; |
| 146 | + const task: GradlePropertiesTaskType = { |
| 147 | + task: 'gradle_properties', |
| 148 | + actions: [ |
| 149 | + { |
| 150 | + after: 'config', |
| 151 | + prepend: 'config2 = use_native_modules!', |
| 152 | + comment: 'test comment', |
| 153 | + }, |
| 154 | + ], |
| 155 | + }; |
| 156 | + |
| 157 | + content = await gradlePropertiesTask({ |
| 158 | + configPath: 'path/to/config', |
| 159 | + task: task, |
| 160 | + content, |
| 161 | + packageName: 'test-package', |
| 162 | + }); |
| 163 | + expect(content).toContain(` |
| 164 | +ignoredfile |
| 165 | +`); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should skip if condition not met', async () => { |
| 169 | + const content = ''; |
| 170 | + const task: GradlePropertiesTaskType = { |
| 171 | + task: 'gradle_properties', |
| 172 | + actions: [ |
| 173 | + { |
| 174 | + when: { test: 'random' }, |
| 175 | + prepend: 'random;', |
| 176 | + }, |
| 177 | + ], |
| 178 | + }; |
| 179 | + |
| 180 | + await expect( |
| 181 | + gradlePropertiesTask({ |
| 182 | + configPath: 'path/to/config', |
| 183 | + task: task, |
| 184 | + content, |
| 185 | + packageName: 'test-package', |
| 186 | + }) |
| 187 | + ).resolves.not.toThrowError('target not found'); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('runTask', () => { |
| 191 | + it('should read and write gradleProperties file', async () => { |
| 192 | + let content = ` |
| 193 | +test1; |
| 194 | +test3; |
| 195 | +`; |
| 196 | + const gradlePropertiesPath = path.resolve( |
| 197 | + __dirname, |
| 198 | + `../../mock-project/android/${Constants.GRADLE_PROPERTIES_FILE_NAME}` |
| 199 | + ); |
| 200 | + mockFs.writeFileSync(gradlePropertiesPath, content); |
| 201 | + const task: GradlePropertiesTaskType = { |
| 202 | + task: 'gradle_properties', |
| 203 | + actions: [ |
| 204 | + { |
| 205 | + prepend: 'test2;', |
| 206 | + }, |
| 207 | + ], |
| 208 | + }; |
| 209 | + |
| 210 | + await runTask({ |
| 211 | + configPath: 'path/to/config', |
| 212 | + task: task, |
| 213 | + packageName: 'test-package', |
| 214 | + }); |
| 215 | + content = mockFs.readFileSync(gradlePropertiesPath); |
| 216 | + // @ts-ignore |
| 217 | + expect(content).toContain(task.actions[0].prepend); |
| 218 | + }); |
| 219 | + it('should throw when insertion point not found with strict', async () => { |
| 220 | + const content = ` |
| 221 | +test1; |
| 222 | +test3; |
| 223 | +`; |
| 224 | + const taskInsertBefore: GradlePropertiesTaskType = { |
| 225 | + task: 'gradle_properties', |
| 226 | + actions: [ |
| 227 | + { |
| 228 | + before: 'random', |
| 229 | + append: 'test2;', |
| 230 | + strict: true, |
| 231 | + }, |
| 232 | + ], |
| 233 | + }; |
| 234 | + const taskInsertBeforeNonStrict: GradlePropertiesTaskType = { |
| 235 | + task: 'gradle_properties', |
| 236 | + actions: [ |
| 237 | + { |
| 238 | + before: 'random', |
| 239 | + append: 'test2;', |
| 240 | + }, |
| 241 | + ], |
| 242 | + }; |
| 243 | + |
| 244 | + await expect( |
| 245 | + gradlePropertiesTask({ |
| 246 | + configPath: 'path/to/config', |
| 247 | + task: taskInsertBefore, |
| 248 | + content, |
| 249 | + packageName: 'test-package', |
| 250 | + }) |
| 251 | + ).rejects.toThrowError('insertion point'); |
| 252 | + await expect( |
| 253 | + gradlePropertiesTask({ |
| 254 | + configPath: 'path/to/config', |
| 255 | + task: taskInsertBeforeNonStrict, |
| 256 | + content, |
| 257 | + packageName: 'test-package', |
| 258 | + }) |
| 259 | + ).resolves.not.toThrowError('insertion point'); |
| 260 | + const taskInsertAfter: GradlePropertiesTaskType = { |
| 261 | + task: 'gradle_properties', |
| 262 | + actions: [ |
| 263 | + { |
| 264 | + after: 'random', |
| 265 | + prepend: 'test2;', |
| 266 | + strict: true, |
| 267 | + }, |
| 268 | + ], |
| 269 | + }; |
| 270 | + |
| 271 | + const taskInsertAfterNonStrict: GradlePropertiesTaskType = { |
| 272 | + task: 'gradle_properties', |
| 273 | + actions: [ |
| 274 | + { |
| 275 | + after: 'random', |
| 276 | + prepend: 'test2;', |
| 277 | + }, |
| 278 | + ], |
| 279 | + }; |
| 280 | + |
| 281 | + await expect( |
| 282 | + gradlePropertiesTask({ |
| 283 | + configPath: 'path/to/config', |
| 284 | + task: taskInsertAfter, |
| 285 | + content, |
| 286 | + packageName: 'test-package', |
| 287 | + }) |
| 288 | + ).rejects.toThrowError('insertion point'); |
| 289 | + await expect( |
| 290 | + gradlePropertiesTask({ |
| 291 | + configPath: 'path/to/config', |
| 292 | + task: taskInsertAfterNonStrict, |
| 293 | + content, |
| 294 | + packageName: 'test-package', |
| 295 | + }) |
| 296 | + ).resolves.not.toThrowError('insertion point'); |
| 297 | + }); |
| 298 | + it('should throw when block is used', async () => { |
| 299 | + const content = ` |
| 300 | +test1; |
| 301 | +test3; |
| 302 | +`; |
| 303 | + const taskInsertBefore: GradlePropertiesTaskType = { |
| 304 | + task: 'gradle_properties', |
| 305 | + actions: [ |
| 306 | + { |
| 307 | + block: 'test', |
| 308 | + before: 'random', |
| 309 | + append: 'test2;', |
| 310 | + strict: true, |
| 311 | + }, |
| 312 | + ], |
| 313 | + }; |
| 314 | + await expect( |
| 315 | + gradlePropertiesTask({ |
| 316 | + configPath: 'path/to/config', |
| 317 | + task: taskInsertBefore, |
| 318 | + content, |
| 319 | + packageName: 'test-package', |
| 320 | + }) |
| 321 | + ).rejects.toThrowError('block is not supported'); |
| 322 | + }); |
| 323 | + it('should work when gradleProperties does not exist', async () => { |
| 324 | + const task: GradlePropertiesTaskType = { |
| 325 | + task: 'gradle_properties', |
| 326 | + actions: [ |
| 327 | + { |
| 328 | + prepend: 'test2;', |
| 329 | + }, |
| 330 | + ], |
| 331 | + }; |
| 332 | + |
| 333 | + await expect( |
| 334 | + runTask({ |
| 335 | + configPath: 'path/to/config', |
| 336 | + task: task, |
| 337 | + packageName: 'test-package', |
| 338 | + }) |
| 339 | + ).resolves.toBeUndefined(); |
| 340 | + }); |
| 341 | + }); |
| 342 | +}); |
0 commit comments