Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate packaging issue #6244

Merged
merged 4 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/plugins/package/lib/packageService.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ module.exports = {
params.include.forEach((pattern) => { params.include.forEach((pattern) => {
patterns.push(pattern); patterns.push(pattern);
}); });

// NOTE: please keep this order of concatenating the include params // NOTE: please keep this order of concatenating the include params
// rather than doing it the other way round! // rather than doing it the other way round!
// see https://github.com/serverless/serverless/pull/5825 for more information // see https://github.com/serverless/serverless/pull/5825 for more information
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/package/lib/packageService.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const serverlessConfigFileUtils = require('../../../../lib/utils/getServerlessCo
// Configure chai // Configure chai
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai')); chai.use(require('sinon-chai'));
const expect = require('chai').expect; const { expect } = require('chai');


describe('#packageService()', () => { describe('#packageService()', () => {
let serverless; let serverless;
Expand Down Expand Up @@ -516,6 +516,7 @@ describe('#packageService()', () => {
])); ]));
}); });
}); });

describe('#packageLayer()', () => { describe('#packageLayer()', () => {
const exclude = ['test-exclude']; const exclude = ['test-exclude'];
const include = ['test-include']; const include = ['test-include'];
Expand Down
34 changes: 18 additions & 16 deletions lib/plugins/package/lib/zipService.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,27 +82,29 @@ module.exports = {
output.on('error', (err) => reject(err)); output.on('error', (err) => reject(err));
zip.on('error', (err) => reject(err)); zip.on('error', (err) => reject(err));



output.on('open', () => { output.on('open', () => {
zip.pipe(output); zip.pipe(output);


BbPromise.all(files.map(this.getFileContentAndStat.bind(this))).then((contents) => { const normalizedFiles = _.uniq(files.map(file => path.normalize(file)));
_.forEach(_.sortBy(contents, ['filePath']), (file) => {
const name = file.filePath.slice(prefix ? `${prefix}${path.sep}`.length : 0); return BbPromise.all(normalizedFiles.map(this.getFileContentAndStat.bind(this)))
let mode = file.stat.mode; .then((contents) => {
if (filesToChmodPlusX && _.includes(filesToChmodPlusX, name) _.forEach(_.sortBy(contents, ['filePath']), (file) => {
&& file.stat.mode % 2 === 0) { const name = file.filePath.slice(prefix ? `${prefix}${path.sep}`.length : 0);
mode += 1; let mode = file.stat.mode;
} if (filesToChmodPlusX && _.includes(filesToChmodPlusX, name)
zip.append(file.data, { && file.stat.mode % 2 === 0) {
name, mode += 1;
mode, }
date: new Date(0), // necessary to get the same hash when zipping the same content zip.append(file.data, {
name,
mode,
date: new Date(0), // necessary to get the same hash when zipping the same content
});
}); });
});


zip.finalize(); zip.finalize();
}).catch(reject); }).catch(reject);
}); });
}); });
}, },
Expand Down
24 changes: 24 additions & 0 deletions lib/plugins/package/lib/zipService.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1036,6 +1036,30 @@ describe('zipService', () => {
}); });
}); });


it('should include files only once', () => {
params.zipFileName = getTestArtifactFileName('include-outside-working-dir');
serverless.config.servicePath = path.join(serverless.config.servicePath, 'lib');
params.exclude = [
'./**',
];
params.include = [
'.././bin/**',
];

return expect(packagePlugin.zip(params)).to.eventually.be
.equal(path.join(serverless.config.servicePath, '.serverless', params.zipFileName))
.then(artifact => {
const data = fs.readFileSync(artifact);
return expect(zip.loadAsync(data)).to.be.fulfilled;
}).then(unzippedData => {
const unzippedFileData = unzippedData.files;
expect(Object.keys(unzippedFileData).sort()).to.deep.equal([
'bin/binary-444',
'bin/binary-777',
]);
});
});

it('should throw an error if no files are matched', () => { it('should throw an error if no files are matched', () => {
params.exclude = ['**/**']; params.exclude = ['**/**'];
params.include = []; params.include = [];
Expand Down
7 changes: 7 additions & 0 deletions tests/utils/fs/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ function getTmpFilePath(fileName) {
return path.join(getTmpDirPath(), fileName); return path.join(getTmpDirPath(), fileName);
} }


function createTmpDir() {
const dirPath = getTmpDirPath();
fse.ensureDirSync(dirPath);
return dirPath;
}

function createTmpFile(name) { function createTmpFile(name) {
const filePath = getTmpFilePath(name); const filePath = getTmpFilePath(name);
fse.ensureFileSync(filePath); fse.ensureFileSync(filePath);
Expand Down Expand Up @@ -50,6 +56,7 @@ module.exports = {
tmpDirCommonPath, tmpDirCommonPath,
getTmpDirPath, getTmpDirPath,
getTmpFilePath, getTmpFilePath,
createTmpDir,
createTmpFile, createTmpFile,
replaceTextInFile, replaceTextInFile,
readYamlFile, readYamlFile,
Expand Down