Skip to content

Commit

Permalink
Merge pull request #110 from amtrack/feat/enable-critical-updates
Browse files Browse the repository at this point in the history
feat: implement activating/deactivating critical updates
  • Loading branch information
amtrack authored Mar 4, 2019
2 parents 10e1f01 + 14b72eb commit 8d94c63
Show file tree
Hide file tree
Showing 12 changed files with 515 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@salesforce/command": "^1.2.0",
"json-merge-patch": "^0.2.3",
"p-retry": "^3.0.1",
"multimatch": "^3.0.0",
"puppeteer": "^1.12.2",
"tslib": "^1"
},
Expand All @@ -27,6 +28,7 @@
"mocha": "^6",
"nyc": "^13",
"sfdx-cli": "^6.54.4",
"tmp": "^0.0.33",
"ts-node": "^8",
"typescript": "^3.3.3333"
},
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/company-settings/critical-updates/activate-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "../../schema.json",
"settings": {
"companySettings": {
"criticalUpdates": [
{
"name": "*",
"comment": "Activated by sfdx-browserforce-plugin",
"active": true
}
]
}
}
}
14 changes: 14 additions & 0 deletions src/plugins/company-settings/critical-updates/deactivate-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "../../schema.json",
"settings": {
"companySettings": {
"criticalUpdates": [
{
"name": "*",
"comment": "Deactivated by sfdx-browserforce-plugin",
"active": false
}
]
}
}
}
122 changes: 122 additions & 0 deletions src/plugins/company-settings/critical-updates/index.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { core } from '@salesforce/command';
import * as assert from 'assert';
import * as child from 'child_process';
import { unlink } from 'fs';
import * as path from 'path';
import { file } from 'tmp';
import { promisify } from 'util';
import CriticalUpdates from '.';
const tmpFilePromise = promisify(file);
const fsUnlinkPromise = promisify(unlink);

describe(CriticalUpdates.name, () => {
let activatableCriticalUpdatesAvailable,
deactivatableCriticalUpdatesAvailable = false;
let stateFileActivation, stateFileDeactivation;
before(async () => {
stateFileActivation = await tmpFilePromise('state1.json');
stateFileDeactivation = await tmpFilePromise('state2.json');
});
after(async () => {
await fsUnlinkPromise(stateFileActivation);
await fsUnlinkPromise(stateFileDeactivation);
});
it('should list all activatable critical updates', async function() {
this.timeout(1000 * 90);
this.slow(1000 * 30);
const planActivateCmd = child.spawnSync(path.resolve('bin', 'run'), [
'browserforce:plan',
'-f',
path.resolve(path.join(__dirname, 'activate-all.json')),
'-s',
stateFileActivation
]);
assert.deepEqual(
planActivateCmd.status,
0,
planActivateCmd.output.toString()
);
let state;
try {
state = await core.fs.readJson(stateFileActivation);
} catch (err) {
assert(false, err);
}
assert(state);
assert(state.settings);
assert(state.settings.companySettings);
assert(state.settings.companySettings.criticalUpdates);
if (state.settings.companySettings.criticalUpdates.length) {
activatableCriticalUpdatesAvailable = state.settings.companySettings.criticalUpdates.some(
item => item.active === false
);
}
});
it('should activate all available critical updates', function() {
if (!activatableCriticalUpdatesAvailable) {
this.skip();
}
this.timeout(1000 * 90);
this.slow(1000 * 30);
const activateCmd = child.spawnSync(path.resolve('bin', 'run'), [
'browserforce:apply',
'-f',
path.resolve(path.join(__dirname, 'activate-all.json'))
]);
assert.deepEqual(activateCmd.status, 0, activateCmd.output.toString());
assert(
/changing 'criticalUpdates' to '\[/.test(activateCmd.output.toString()),
activateCmd.output.toString()
);
});
it('should list all deactivatable critical updates', async function() {
this.timeout(1000 * 90);
this.slow(1000 * 30);
const planDeactivateCmd = child.spawnSync(path.resolve('bin', 'run'), [
'browserforce:plan',
'-f',
path.resolve(path.join(__dirname, 'deactivate-all.json')),
'-s',
'/tmp/sfdx-browserforce-plugin.state.json'
]);
assert.deepEqual(
planDeactivateCmd.status,
0,
planDeactivateCmd.output.toString()
);
let state;
try {
state = await core.fs.readJson(
'/tmp/sfdx-browserforce-plugin.state.json'
);
} catch (err) {
assert(false, err);
}
assert(state);
assert(state.settings);
assert(state.settings.companySettings);
assert(state.settings.companySettings.criticalUpdates);
if (state.settings.companySettings.criticalUpdates.length) {
deactivatableCriticalUpdatesAvailable = state.settings.companySettings.criticalUpdates.some(
item => item.active === true
);
}
});
it('should deactivate all deactivatable critical updates', function() {
if (!deactivatableCriticalUpdatesAvailable) {
this.skip();
}
this.timeout(1000 * 90);
this.slow(1000 * 30);
const deactivateCmd = child.spawnSync(path.resolve('bin', 'run'), [
'browserforce:apply',
'-f',
path.resolve(path.join(__dirname, 'deactivate-all.json'))
]);
assert.deepEqual(deactivateCmd.status, 0, deactivateCmd.output.toString());
assert(
/changing 'criticalUpdates' to '\[/.test(deactivateCmd.output.toString()),
deactivateCmd.output.toString()
);
});
});
153 changes: 153 additions & 0 deletions src/plugins/company-settings/critical-updates/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import * as assert from 'assert';
import CriticalUpdates from '.';

interface TestData {
name: string | string[];
active: boolean;
comment?: string;
}
interface Test {
description: string;
source: TestData[];
target: TestData[];
expected: TestData[];
skip?: boolean;
}

const tests: Test[] = [
{
description: 'should return no change',
source: [
{
name: 'Update 1',
active: true
},
{
name: 'Update 2',
active: true
},
{
name: 'Update 3',
active: true
}
],
target: [
{
name: 'Update 2',
active: true,
comment: 'This is a comment'
}
],
expected: []
},
{
description: 'should match a specific item',
source: [
{
name: 'Update 1',
active: false
},
{
name: 'Update 2',
active: false
},
{
name: 'Update 3',
active: false
}
],
target: [
{
name: 'Update 2',
active: true,
comment: 'This is a comment'
}
],
expected: [
{
name: 'Update 2',
active: true,
comment: 'This is a comment'
}
]
},
{
description: 'should match all inactive items',
source: [
{
name: 'Update 1',
active: true
},
{
name: 'Update 2',
active: false
},
{
name: 'Update 3',
active: true
}
],
target: [
{
name: '*',
active: true,
comment: 'This is a comment'
}
],
expected: [
{
name: 'Update 2',
active: true,
comment: 'This is a comment'
}
]
},
{
description: 'should handle multiple patterns',
source: [
{
name: 'Update 1',
active: false
},
{
name: 'Update 2',
active: false
},
{
name: 'Update 3',
active: false
}
],
target: [
{
name: ['*', '!Update 2'],
active: true,
comment: 'This is a comment'
}
],
expected: [
{
name: 'Update 1',
active: true,
comment: 'This is a comment'
},
{
name: 'Update 3',
active: true,
comment: 'This is a comment'
}
]
}
];

describe('CriticalUpdates', () => {
describe('diff()', () => {
const p = new CriticalUpdates(null, null);
for (const t of tests) {
(t.skip ? it.skip : it)(t.description, () => {
const actual = p.diff(t.source, t.target);
assert.deepEqual(actual, t.expected);
});
}
});
});
Loading

0 comments on commit 8d94c63

Please sign in to comment.