UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

709 lines (707 loc) 19.2 kB
"use strict"; /* eslint-disable @typescript-eslint/no-unsafe-call */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const { mockFs } = require('../../mocks/mockAll'); const mock = jest.spyOn(require('../../../utils/stringSplice'), 'stringSplice'); const path_1 = __importDefault(require("path")); const constants_1 = require("../../../constants"); const buildGradleTask_1 = require("../../../tasks/buildGradleTask"); const mockAll_1 = require("../../mocks/mockAll"); describe('buildGradleTask', () => { it('should prepend text into empty body ', async () => { let content = ''; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', append: 'google();', prepend: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google(); } } `); }); it('should prepend text into empty body without block', async () => { let content = ''; const task = { task: 'build_gradle', actions: [ { append: 'google();', prepend: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` google(); `); }); it('should skip insert when ifNotPresent exists', async () => { const content = ` buildscript { ext { jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', ifNotPresent: 'jcenter', append: 'google();', }, { block: 'buildscript.ext', ifNotPresent: 'jcenter', prepend: 'google();', }, ], }; await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(mockAll_1.mockPrompter.log.message).toHaveBeenCalledWith(expect.stringContaining('found existing ')); }); it('should prepend text into partial body ', async () => { let content = ` buildscript {} `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', prepend: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google(); } } `); }); it('should prepend text into existing body ', async () => { let content = ` buildscript { ext { jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', prepend: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google(); jcenter(); } } `); }); it('should append text into existing body ', async () => { let content = ` buildscript { ext { jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', append: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { jcenter(); google(); } } `); }); it('should replace text with existing body ', async () => { let content = ` buildscript { ext { jcenter(); jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', search: 'enter', replace: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google(); jcenter(); } } `); }); it('should append text exactly with existing body ', async () => { let content = ` buildscript { ext { jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript', after: 'ext {', before: '}', search: 'jcenter();', replace: 'jcenter();', prepend: 'google1();', append: 'google3();', exact: true, }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google1();jcenter();google3(); } } `); }); it('should replace all text with existing body ', async () => { let content = ` buildscript { ext { jcenter(); jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', search: { regex: 'jcenter\\(\\);', flags: 'g', }, replace: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google(); google(); } } `); }); it('should replace all text with existing body exactly', async () => { let content = ` buildscript { ext { jcenter(); // some comment jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', search: { regex: 'jcenter\\(\\);', flags: 'g', }, exact: true, replace: 'google();', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` buildscript { ext { google(); // some comment google(); } } `); }); it('should skip replace when ifNotPresent exists', async () => { const content = ` buildscript { ext { jcenter(); } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', ifNotPresent: 'jcenter', search: 'jcenter();', replace: 'google();', }, { block: 'buildscript.ext', ifNotPresent: 'jcenter', search: 'jcenter();', replace: 'google();', }, ], }; await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(mockAll_1.mockPrompter.log.message).toHaveBeenCalledWith(expect.stringContaining('found existing ')); }); it('should insert text after point with comment', async () => { let content = ` buildscript { ext { test1; test3; } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', after: 'test1', prepend: 'test2;', comment: 'test comment', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toContain(`test1; // test comment test2; test3;`); }); it('should insert text when empty', async () => { let content = ` buildscript {} `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', after: 'test1', prepend: 'test2;', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toContain(` buildscript { ext { test2; } } `); }); it('should insert text before point', async () => { let content = ` buildscript { ext { test1; test3; } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', before: { regex: '\n.*?test3;' }, append: 'test2;', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toContain(`test1; test2; test3;`); }); it('should append in non existing deep path correctly', async () => { let content = ` buildscript { ext { debug { test1; test3; } } random { release { test1; test3; } } } `; const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext.release', append: 'test2;', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toContain(` ext { debug { test1; test3; } release { test2; } }`); }); it('should skip if condition not met', async () => { let content = ` buildscript { ext { test1; test3; } } `; const task = { task: 'build_gradle', actions: [ { when: { test: 'random' }, block: 'buildscript.ext', before: { regex: '\n.*?test3;' }, append: 'test2;', }, ], }; content = await (0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).not.toContain(`test1; test2; test3;`); }); it('should throw when insertion point not found with strict', async () => { const content = ` buildscript { ext { test1; test3; } } `; const taskInsertBefore = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', before: 'random', append: 'test2;', strict: true, }, ], }; const taskInsertBeforeNonStrict = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', before: 'random', append: 'test2;', }, ], }; await expect((0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: taskInsertBefore, content, packageName: 'test-package', })).rejects.toThrowError('insertion point'); await expect((0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: taskInsertBeforeNonStrict, content, packageName: 'test-package', })).resolves.not.toThrowError('insertion point'); const taskInsertAfter = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', after: 'random', prepend: 'test2;', strict: true, }, ], }; const taskInsertAfterNonStrict = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', after: 'random', prepend: 'test2;', }, ], }; await expect((0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: taskInsertAfter, content, packageName: 'test-package', })).rejects.toThrowError('insertion point'); await expect((0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: taskInsertAfterNonStrict, content, packageName: 'test-package', })).resolves.not.toThrowError('insertion point'); }); it('should throw when block could not be added', async () => { const content = ''; mock.mockImplementationOnce(content => content); const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', prepend: 'random;', }, ], }; await expect((0, buildGradleTask_1.buildGradleTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', })).rejects.toThrowError('block could not be inserted'); }); describe('runTask', () => { it('should read and write build gradle file', async () => { let content = ` buildscript { ext { test1; test3; } } `; const buildGradlePath = path_1.default.resolve(__dirname, `../../mock-project/android/${constants_1.Constants.BUILD_GRADLE_FILE_NAME}`); mockFs.writeFileSync(buildGradlePath, content); const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', prepend: 'test2;', }, ], }; await (0, buildGradleTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', }); content = mockFs.readFileSync(buildGradlePath); // @ts-ignore expect(content).toContain(task.actions[0].prepend); }); it('should read and write app build gradle file', async () => { let content = ` buildscript { ext { test1; test3; } } `; const buildGradlePath = path_1.default.resolve(__dirname, `../../mock-project/android/app/${constants_1.Constants.BUILD_GRADLE_FILE_NAME}`); mockFs.writeFileSync(buildGradlePath, content); const task = { task: 'build_gradle', location: 'app', actions: [ { block: 'buildscript.ext', prepend: 'test2;', }, ], }; await (0, buildGradleTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', }); content = mockFs.readFileSync(buildGradlePath); // @ts-ignore expect(content).toContain(task.actions[0].prepend); }); it('should throw when build gradle does not exist', async () => { const task = { task: 'build_gradle', actions: [ { block: 'buildscript.ext', prepend: 'test2;', }, ], }; await expect((0, buildGradleTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', })).rejects.toThrowError('build.gradle file not found'); }); }); });