Merge pull request #5791 from danielcondemarin/aws-deploy-checkForChanges-TODO

Replace blocking fs.readFileSync with non blocking fs.readFile in checkForChanges.js
This commit is contained in:
Philipp Muens 2019-02-07 11:22:16 +01:00 committed by GitHub
commit bc9cb08bad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 35 deletions

View File

@ -109,23 +109,24 @@ module.exports = {
// create hashes for all the zip files
const zipFiles = globby.sync(['**.zip'], { cwd: serverlessDirPath, dot: true, silent: true });
const zipFilePaths = zipFiles.map((zipFile) => path.join(serverlessDirPath, zipFile));
const zipFileHashes = zipFilePaths.map((zipFilePath) => {
// TODO refactor to be async (use util function to compute checksum async)
const zipFile = fs.readFileSync(zipFilePath);
return crypto.createHash('sha256').update(zipFile).digest('base64');
const readFile = BbPromise.promisify(fs.readFile);
const zipFileHashesPromises = zipFilePaths.map(zipFilePath => readFile(zipFilePath)
.then(zipFile => crypto.createHash('sha256').update(zipFile).digest('base64')));
return BbPromise.all(zipFileHashesPromises).then(zipFileHashes => {
const localHashes = zipFileHashes;
localHashes.push(localCfHash);
if (_.isEqual(remoteHashes.sort(), localHashes.sort())) {
this.serverless.service.provider.shouldNotDeploy = true;
const message = [
'Service files not changed. Skipping deployment...',
].join('');
this.serverless.cli.log(message);
}
});
const localHashes = zipFileHashes;
localHashes.push(localCfHash);
if (_.isEqual(remoteHashes.sort(), localHashes.sort())) {
this.serverless.service.provider.shouldNotDeploy = true;
const message = [
'Service files not changed. Skipping deployment...',
].join('');
this.serverless.cli.log(message);
}
}
return BbPromise.resolve();

View File

@ -278,7 +278,7 @@ describe('checkForChanges', () => {
describe('#checkIfDeploymentIsNecessary()', () => {
let normalizeCloudFormationTemplateStub;
let globbySyncStub;
let readFileSyncStub;
let readFileStub;
beforeEach(() => {
normalizeCloudFormationTemplateStub = sinon
@ -286,22 +286,22 @@ describe('checkForChanges', () => {
.returns();
globbySyncStub = sinon
.stub(globby, 'sync');
readFileSyncStub = sinon
.stub(fs, 'readFileSync')
.returns();
readFileStub = sinon
.stub(fs, 'readFile')
.yields(null, undefined);
});
afterEach(() => {
normalizeFiles.normalizeCloudFormationTemplate.restore();
globby.sync.restore();
fs.readFileSync.restore();
fs.readFile.restore();
});
it('should resolve if no input is provided', () => expect(awsDeploy
.checkIfDeploymentIsNecessary()).to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.not.have.been.called;
expect(globbySyncStub).to.not.have.been.called;
expect(readFileSyncStub).to.not.have.been.called;
expect(readFileStub).to.not.have.been.called;
expect(awsDeploy.serverless.cli.log).to.not.have.been.called;
})
);
@ -313,7 +313,7 @@ describe('checkForChanges', () => {
.to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.not.have.been.called;
expect(globbySyncStub).to.not.have.been.called;
expect(readFileSyncStub).to.not.have.been.called;
expect(readFileStub).to.not.have.been.called;
expect(awsDeploy.serverless.cli.log).to.not.have.been.called;
});
});
@ -333,7 +333,7 @@ describe('checkForChanges', () => {
.to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.have.been.calledOnce;
expect(globbySyncStub).to.have.been.calledOnce;
expect(readFileSyncStub).to.have.been.calledOnce;
expect(readFileStub).to.have.been.calledOnce;
expect(awsDeploy.serverless.cli.log).to.not.have.been.called;
expect(normalizeCloudFormationTemplateStub).to.have.been.calledWithExactly(
awsDeploy.serverless.service.provider.compiledCloudFormationTemplate
@ -346,7 +346,7 @@ describe('checkForChanges', () => {
silent: true,
}
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/my-service.zip')
);
expect(awsDeploy.serverless.service.provider.shouldNotDeploy).to.equal(undefined);
@ -367,7 +367,7 @@ describe('checkForChanges', () => {
.to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.have.been.calledOnce;
expect(globbySyncStub).to.have.been.calledOnce;
expect(readFileSyncStub).to.have.been.calledOnce;
expect(readFileStub).to.have.been.calledOnce;
expect(awsDeploy.serverless.cli.log).to.not.have.been.called;
expect(normalizeCloudFormationTemplateStub).to.have.been.calledWithExactly(
awsDeploy.serverless.service.provider.compiledCloudFormationTemplate
@ -380,7 +380,7 @@ describe('checkForChanges', () => {
silent: true,
}
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/my-service.zip')
);
expect(awsDeploy.serverless.service.provider.shouldNotDeploy).to.equal(undefined);
@ -403,7 +403,7 @@ describe('checkForChanges', () => {
.to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.have.been.calledOnce;
expect(globbySyncStub).to.have.been.calledOnce;
expect(readFileSyncStub).to.have.been.calledTwice;
expect(readFileStub).to.have.been.calledTwice;
expect(awsDeploy.serverless.cli.log).to.not.have.been.called;
expect(normalizeCloudFormationTemplateStub).to.have.been.calledWithExactly(
awsDeploy.serverless.service.provider.compiledCloudFormationTemplate
@ -416,10 +416,10 @@ describe('checkForChanges', () => {
silent: true,
}
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/func1.zip')
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/func2.zip')
);
expect(awsDeploy.serverless.service.provider.shouldNotDeploy).to.equal(undefined);
@ -440,7 +440,7 @@ describe('checkForChanges', () => {
.to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.have.been.calledOnce;
expect(globbySyncStub).to.have.been.calledOnce;
expect(readFileSyncStub).to.have.been.calledOnce;
expect(readFileStub).to.have.been.calledOnce;
expect(awsDeploy.serverless.cli.log).to.have.been.calledOnce;
expect(normalizeCloudFormationTemplateStub).to.have.been.calledWithExactly(
awsDeploy.serverless.service.provider.compiledCloudFormationTemplate
@ -453,7 +453,7 @@ describe('checkForChanges', () => {
silent: true,
}
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/my-service.zip')
);
expect(awsDeploy.serverless.service.provider.shouldNotDeploy).to.equal(true);
@ -477,7 +477,7 @@ describe('checkForChanges', () => {
.to.be.fulfilled.then(() => {
expect(normalizeCloudFormationTemplateStub).to.have.been.calledOnce;
expect(globbySyncStub).to.have.been.calledOnce;
expect(readFileSyncStub).to.have.been.calledTwice;
expect(readFileStub).to.have.been.calledTwice;
expect(awsDeploy.serverless.cli.log).to.have.been.calledOnce;
expect(normalizeCloudFormationTemplateStub).to.have.been.calledWithExactly(
awsDeploy.serverless.service.provider.compiledCloudFormationTemplate
@ -490,10 +490,10 @@ describe('checkForChanges', () => {
silent: true,
}
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/func1.zip')
);
expect(readFileSyncStub).to.have.been.calledWithExactly(
expect(readFileStub).to.have.been.calledWith(
path.join('my-service/.serverless/func2.zip')
);
expect(awsDeploy.serverless.service.provider.shouldNotDeploy).to.equal(true);