Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 361bc1a

Browse files
authored
Merge pull request #148 from volteanu/patch-147
Rename package keys before merging
2 parents 45d437f + 6c402c9 commit 361bc1a

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

compile/packages/index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class OpenWhiskCompilePackages {
1111
this.hooks = {
1212
'before:package:compileEvents': () => BbPromise.bind(this)
1313
.then(this.setup)
14+
.then(this.renameManifestPackages)
1415
.then(this.mergeActionPackages),
1516
'package:compileEvents': this.compilePackages.bind(this),
1617
};
@@ -22,9 +23,25 @@ class OpenWhiskCompilePackages {
2223
this.serverless.service.packages = {};
2324
}
2425

26+
renameManifestPackages() {
27+
if (!this.serverless.service.resources.packages) return;
28+
29+
const manifestPackages = this.serverless.service.resources.packages;
30+
31+
Object.keys(manifestPackages).forEach(packageKey => {
32+
const pack = manifestPackages[packageKey];
33+
34+
if (pack.name && pack.name !== packageKey) {
35+
// move the package under the new name
36+
manifestPackages[pack.name] = pack;
37+
delete manifestPackages[packageKey];
38+
}
39+
})
40+
}
41+
2542
mergeActionPackages() {
26-
const packages = this.getActionPackages();
27-
if (!packages.length) return;
43+
const actionPackages = this.getActionPackages();
44+
if (!actionPackages.length) return;
2845

2946
if (!this.serverless.service.resources) {
3047
this.serverless.service.resources = {};
@@ -34,9 +51,9 @@ class OpenWhiskCompilePackages {
3451
this.serverless.service.resources.packages = {};
3552
}
3653

37-
const manifestPackages = this.serverless.service.resources.packages || {};
54+
const manifestPackages = this.serverless.service.resources.packages;
3855

39-
packages.forEach(pkge => {
56+
actionPackages.forEach(pkge => {
4057
manifestPackages[pkge] = manifestPackages[pkge] || {}
4158
})
4259
}

compile/packages/tests/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ describe('OpenWhiskCompilePackages', () => {
3636
sandbox.restore();
3737
});
3838

39+
describe('#renameManifestPackages()', () => {
40+
it('should rename packages with explicit names', () => {
41+
openwhiskCompilePackages.serverless.service.resources.packages = {
42+
'first' : { name: 'firstchanged', parameters: { hello: 'world first' } },
43+
'second' : { parameters: { hello: 'world second' } }
44+
};
45+
46+
const expected = {
47+
'firstchanged' : { name: 'firstchanged', parameters: { hello: 'world first' } },
48+
'second' : { parameters: { hello: 'world second' } }
49+
};
50+
51+
openwhiskCompilePackages.renameManifestPackages();
52+
expect(openwhiskCompilePackages.serverless.service.resources.packages)
53+
.to.deep.equal(expected);
54+
55+
})
56+
})
57+
3958
describe('#getActionPackages()', () => {
4059
it('should return no package names for functions without name property', () => {
4160
const service = openwhiskCompilePackages.serverless.service;
@@ -140,6 +159,46 @@ describe('OpenWhiskCompilePackages', () => {
140159
.to.deep.equal({ sample: expected })
141160
)).to.eventually.be.fulfilled;
142161
});
162+
163+
it('should merge packages with explicit names', () => {
164+
openwhiskCompilePackages.serverless.service.resources.packages = {
165+
'first' : { name: 'firstchanged', parameters: { hello: 'world first' } },
166+
'second' : { parameters: { hello: 'world second' } }
167+
};
168+
169+
sandbox.stub(openwhiskCompilePackages, 'getActionPackages', () => ['firstchanged', 'second', 'third']);
170+
171+
const expected = {
172+
firstchanged: {
173+
name: 'firstchanged',
174+
overwrite: true,
175+
package: { parameters: [{ key: 'hello', value: 'world first' }] },
176+
namespace: 'testing'
177+
},
178+
second: {
179+
name: 'second',
180+
overwrite: true,
181+
package: { parameters: [{ key: 'hello', value: 'world second' }] },
182+
namespace: 'testing'
183+
},
184+
third: {
185+
name: 'third',
186+
overwrite: true,
187+
package: {},
188+
namespace: 'testing'
189+
}
190+
};
191+
192+
// Simulate hooks
193+
openwhiskCompilePackages.setup();
194+
openwhiskCompilePackages.renameManifestPackages();
195+
openwhiskCompilePackages.mergeActionPackages();
196+
197+
return expect(openwhiskCompilePackages.compilePackages().then(() => {
198+
expect(openwhiskCompilePackages.serverless.service.packages)
199+
.to.deep.equal(expected)
200+
})).to.eventually.be.fulfilled;
201+
});
143202
});
144203
describe('#compilePackage()', () => {
145204
it('should define packages without a body', () => {

0 commit comments

Comments
 (0)