Skip to content

Commit 69dca3b

Browse files
author
Murat
committed
feat(upgrade): added running upgrade.yml tasks
1 parent 6d70ed5 commit 69dca3b

17 files changed

+756
-321
lines changed

src/__tests__/unit/tasks/buildGradleTask.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,51 @@ buildscript {
469469
test2;
470470
test3;`);
471471
});
472+
it('should append in non existing deep path correctly', async () => {
473+
let content = `
474+
buildscript {
475+
ext {
476+
debug {
477+
test1;
478+
test3;
479+
}
480+
}
481+
random {
482+
release {
483+
test1;
484+
test3;
485+
}
486+
}
487+
}
488+
`;
489+
const task: BuildGradleTaskType = {
490+
type: 'build_gradle',
491+
actions: [
492+
{
493+
block: 'buildscript.ext.release',
494+
append: 'test2;',
495+
},
496+
],
497+
};
498+
499+
content = await buildGradleTask({
500+
configPath: 'path/to/config',
501+
task: task,
502+
content,
503+
packageName: 'test-package',
504+
});
505+
expect(content).toContain(`
506+
ext {
507+
debug {
508+
test1;
509+
test3;
510+
}
511+
512+
release {
513+
test2;
514+
}
515+
}`);
516+
});
472517
it('should skip if condition not met', async () => {
473518
let content = `
474519
buildscript {

src/__tests__/unit/upgrade.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const mockRestoreBackupFiles = jest.spyOn(
1414
require('../../utils/upgrade/restoreBackupFiles'),
1515
'restoreBackupFiles'
1616
);
17+
const mockRunUpgradeTasks = jest.spyOn(
18+
require('../../utils/upgrade/runUpgradeTasks'),
19+
'runUpgradeTasks'
20+
);
1721

1822
import { Constants } from '../../constants';
1923
import { upgrade } from '../../upgrade';
@@ -50,6 +54,45 @@ describe('upgrade', () => {
5054
expect.stringContaining('skipping import from old project')
5155
);
5256
});
57+
it('should execute upgrade.yml', async () => {
58+
mockPrompter.text.mockImplementationOnce(() => '');
59+
mockRunUpgradeTasks.mockResolvedValueOnce({
60+
didRun: true,
61+
completedTaskCount: 1,
62+
});
63+
mockPrompter.log.success.mockClear();
64+
65+
await upgrade();
66+
67+
expect(mockPrompter.log.success).toHaveBeenCalledWith(
68+
expect.stringContaining('task(s) successfully')
69+
);
70+
});
71+
it('should handle upgrade.yml execution error', async () => {
72+
mockPrompter.text.mockImplementationOnce(() => '');
73+
mockRunUpgradeTasks.mockRejectedValueOnce(new Error('test'));
74+
mockPrompter.log.warning.mockClear();
75+
76+
await upgrade();
77+
78+
expect(mockPrompter.log.warning).toHaveBeenCalledWith(
79+
expect.stringContaining('test')
80+
);
81+
});
82+
it('should handle upgrade.yml failed tasks', async () => {
83+
mockPrompter.text.mockImplementationOnce(() => '');
84+
mockRunUpgradeTasks.mockResolvedValueOnce({
85+
didRun: true,
86+
failedTaskCount: 1,
87+
});
88+
mockPrompter.log.warning.mockClear();
89+
90+
await upgrade();
91+
92+
expect(mockPrompter.log.warning).toHaveBeenCalledWith(
93+
expect.stringContaining('task(s) - please complete upgrade manually')
94+
);
95+
});
5396
it('should not run tasks when lock does not exist', async () => {
5497
const spinner = mockPrompter.spinner();
5598
spinner.stop.mockReset();
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
require('../../../mocks/mockAll');
2+
const mockRunTask = jest.spyOn(require('../../../../utils/runTask'), 'runTask');
3+
4+
import path from 'path';
5+
import { getProjectPath } from '../../../../utils/getProjectPath';
6+
import { runUpgradeTasks } from '../../../../utils/upgrade/runUpgradeTasks';
7+
import { mockFs } from '../../../mocks/mockFs';
8+
9+
describe('runUpgradeTasks', () => {
10+
afterEach(() => {
11+
mockRunTask.mockReset();
12+
});
13+
it('should execute upgrade.yml tasks', async () => {
14+
mockRunTask.mockResolvedValueOnce(undefined);
15+
mockFs.writeFileSync(
16+
path.join(getProjectPath(), '.upgrade/upgrade.yml'),
17+
`
18+
env:
19+
value: true
20+
tasks:
21+
- type: app_delegate
22+
label: AppDelegate.mm modification
23+
actions:
24+
- append: test`
25+
);
26+
27+
const result = await runUpgradeTasks();
28+
29+
expect(result.didRun).toBeTruthy();
30+
expect(mockRunTask).toHaveBeenCalledWith({
31+
configPath: path.join(getProjectPath(), '.upgrade/upgrade.yml'),
32+
packageName: 'upgrade.yml',
33+
task: {
34+
type: 'app_delegate',
35+
label: 'AppDelegate.mm modification',
36+
actions: [{ append: 'test' }],
37+
},
38+
});
39+
});
40+
it('should not execute when tasks does not meet condition', async () => {
41+
mockRunTask.mockResolvedValueOnce(undefined);
42+
mockFs.writeFileSync(
43+
path.join(getProjectPath(), '.upgrade/upgrade.yml'),
44+
`
45+
env:
46+
value: true
47+
tasks:
48+
- type: app_delegate
49+
when:
50+
value: false
51+
actions:
52+
- append: test`
53+
);
54+
55+
const result = await runUpgradeTasks();
56+
57+
expect(result.didRun).toBeTruthy();
58+
expect(mockRunTask).not.toHaveBeenCalled();
59+
});
60+
it('should handle not finding upgrade.yml', async () => {
61+
const result = await runUpgradeTasks();
62+
63+
expect(result.didRun).toBeFalsy();
64+
});
65+
it('should handle invalid upgrade.yml', async () => {
66+
mockFs.writeFileSync(
67+
path.join(getProjectPath(), '.upgrade/upgrade.yml'),
68+
'random'
69+
);
70+
const result = await runUpgradeTasks();
71+
72+
expect(result.didRun).toBeFalsy();
73+
});
74+
it('should handle failed tasks', async () => {
75+
mockRunTask.mockRejectedValueOnce(new Error('random'));
76+
mockFs.writeFileSync(
77+
path.join(getProjectPath(), '.upgrade/upgrade.yml'),
78+
`
79+
tasks:
80+
- type: app_delegate
81+
actions:
82+
- append: test`
83+
);
84+
85+
const result = await runUpgradeTasks();
86+
87+
expect(result.didRun).toBeTruthy();
88+
if (result.didRun) {
89+
expect(result.failedTaskCount).toBe(1);
90+
}
91+
});
92+
});

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const Constants = {
22
CURRENT_LOCK_VERSION: 1,
33
LOCK_FILE_NAME: 'integrate-lock.json',
44
UPGRADE_FOLDER_NAME: '.upgrade',
5+
UPGRADE_CONFIG_FILE_NAME: 'upgrade.yml',
56
GIT_FOLDER_NAME: '.git',
67
PACKAGE_JSON_FILE_NAME: 'package.json',
78
APP_DELEGATE_FILE_NAME: 'AppDelegate.mm',

src/getInfo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ export async function getInfo(packageName: string): Promise<void> {
7878

7979
let config: IntegrationConfig | undefined;
8080
if (localPathExists && localConfigPath) {
81-
config = parseConfig(localConfigPath);
82-
} else if (remoteFileContent) config = parseConfigString(remoteFileContent);
81+
config = parseConfig(localConfigPath) as IntegrationConfig;
82+
} else if (remoteFileContent)
83+
config = parseConfigString(remoteFileContent) as IntegrationConfig;
8384
if (config) {
8485
const _config = config;
8586
logSuccess(

src/integrate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export async function integrate(packageName?: string): Promise<void> {
9494
else {
9595
let config: IntegrationConfig;
9696
try {
97-
config = parseConfig(configPath);
97+
config = parseConfig(configPath) as IntegrationConfig;
9898
} catch (e) {
9999
logError(
100100
color.bold(color.bgRed(' error ')) +

src/schema/integrate.schema.json

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,6 @@
576576
}
577577
]
578578
},
579-
"MultiselectOptionValue": {
580-
"type": [
581-
"string",
582-
"number",
583-
"boolean"
584-
]
585-
},
586579
"NotificationServiceTaskType": {
587580
"additionalProperties": false,
588581
"properties": {
@@ -661,6 +654,16 @@
661654
],
662655
"type": "object"
663656
},
657+
"ObjectModifierStrategy": {
658+
"enum": [
659+
"append",
660+
"assign",
661+
"merge",
662+
"merge_concat",
663+
"merge_distinct"
664+
],
665+
"type": "string"
666+
},
664667
"ObjectModifierType": {
665668
"additionalProperties": false,
666669
"properties": {
@@ -672,14 +675,7 @@
672675
"type": "object"
673676
},
674677
"strategy": {
675-
"enum": [
676-
"append",
677-
"assign",
678-
"merge",
679-
"merge_concat",
680-
"merge_distinct"
681-
],
682-
"type": "string"
678+
"$ref": "#/definitions/ObjectModifierStrategy"
683679
},
684680
"when": {
685681
"$ref": "#/definitions/AnyObject"
@@ -690,6 +686,13 @@
690686
],
691687
"type": "object"
692688
},
689+
"OptionValue": {
690+
"type": [
691+
"string",
692+
"number",
693+
"boolean"
694+
]
695+
},
693696
"PlistTaskType": {
694697
"additionalProperties": false,
695698
"properties": {
@@ -949,7 +952,7 @@
949952
"properties": {
950953
"initialValues": {
951954
"items": {
952-
"$ref": "#/definitions/MultiselectOptionValue"
955+
"$ref": "#/definitions/OptionValue"
953956
},
954957
"type": "array"
955958
},
@@ -967,7 +970,7 @@
967970
"type": "string"
968971
},
969972
"value": {
970-
"$ref": "#/definitions/MultiselectOptionValue"
973+
"$ref": "#/definitions/OptionValue"
971974
}
972975
},
973976
"required": [

0 commit comments

Comments
 (0)