serverless/test/unit/lib/utils/getEnsureArtifact.test.js
2021-01-12 11:53:37 +01:00

46 lines
1.4 KiB
JavaScript

'use strict';
const { expect } = require('chai');
const ensureArtifact = require('../../../../lib/utils/ensureArtifact');
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const crypto = require('crypto');
describe('#ensureArtifact', () => {
const testArtifactName = `test-${crypto.randomBytes(2).toString('hex')}`;
let testArtifactPath;
let invokedCount = 0;
const generateFunc = async (cachePath) => {
testArtifactPath = path.resolve(cachePath, testArtifactName);
++invokedCount;
await fs.promises.writeFile(testArtifactPath, '');
};
it('Should generate artifact if missing', async () => {
await ensureArtifact(testArtifactName, generateFunc);
const exists = await fse.pathExists(testArtifactPath);
expect(exists).to.be.true;
});
it('Should generate only on first access', async () => {
await ensureArtifact(testArtifactName, generateFunc);
expect(invokedCount).to.equal(1);
});
it('Should not generate, if generated in past', async () => {
ensureArtifact.delete(testArtifactName);
await ensureArtifact(testArtifactName, generateFunc);
expect(invokedCount).to.equal(1);
});
it('Should return cache path', async () => {
const cachePath = await ensureArtifact(testArtifactName, generateFunc);
expect(cachePath).to.include(`.serverless${path.sep}artifacts`);
});
});