Skip to content

Commit e766ad9

Browse files
Add test for --no-cache
1 parent 6adb74c commit e766ad9

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/test/cli.build.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as os from 'os';
1010
import { buildKitOptions, shellExec } from './testUtils';
1111
import { ImageDetails } from '../spec-shutdown/dockerUtils';
1212
import { envListToObj } from '../spec-node/utils';
13+
import * as crypto from 'crypto';
1314

1415
const pkg = require('../../package.json');
1516

@@ -64,6 +65,62 @@ describe('Dev Containers CLI', function () {
6465
});
6566
});
6667

68+
describe('caching', () => {
69+
let buildCommandRerunLog = '';
70+
let buildWithoutCacheLog = '';
71+
this.beforeEach(() => {
72+
buildCommandRerunLog = `${os.tmpdir()}/${crypto.randomUUID()}`;
73+
buildWithoutCacheLog = `${os.tmpdir()}/${crypto.randomUUID()}`;
74+
console.log(`rerun log: ${buildCommandRerunLog}`);
75+
console.log(`no cache log: ${buildWithoutCacheLog}`);
76+
});
77+
78+
this.afterEach(() => {
79+
for (const file in [buildCommandRerunLog, buildWithoutCacheLog]) {
80+
if (fs.existsSync(file)) {
81+
// fs.unlinkSync(file);
82+
}
83+
}
84+
});
85+
86+
it('should not use docker cache for features when `--no-cache` flag is passed', async () => {
87+
// Arrange
88+
const testFolder = `${__dirname}/configs/image-with-features`;
89+
const buildCommand = `${cli} build --workspace-folder ${testFolder}`;
90+
const buildWithoutCacheCommand = `${buildCommand} --no-cache`;
91+
const expectedFeatures = [
92+
'ghcr.io/devcontainers/feature-starter/hello',
93+
'ghcr.io/devcontainers/features/docker-in-docker'
94+
];
95+
96+
// Act
97+
await shellExec(`${buildCommand}`); // initial run of command
98+
await shellExec(`${buildCommand} > ${buildCommandRerunLog} 2>&1`); // rerun command using cache
99+
await shellExec(`${buildWithoutCacheCommand} > ${buildWithoutCacheLog} 2>&1`); // rerun command without cache
100+
101+
// Assert
102+
const buildCommandRerunLogContents = fs.readFileSync(buildCommandRerunLog, 'utf8');
103+
const buildWithoutCacheCommandLogContents = fs.readFileSync(buildWithoutCacheLog, 'utf8');
104+
105+
for (const logContent in [buildCommandRerunLogContents, buildWithoutCacheCommandLogContents]) {
106+
assert.notEqual(logContent, null);
107+
assert.notEqual(logContent, undefined);
108+
assert.notEqual(logContent, '');
109+
}
110+
111+
function countInstances(subject: string, search: string) {
112+
return subject.split(search).length - 1;
113+
}
114+
115+
for (const expectedFeature of expectedFeatures) {
116+
const featureNameInstanceCountInRerunOfBuildCommandLogs = countInstances(buildCommandRerunLogContents, expectedFeature);
117+
const featureNameInstanceCountInBuildWithNoCache = countInstances(buildWithoutCacheCommandLogContents, expectedFeature);
118+
assert.equal(featureNameInstanceCountInRerunOfBuildCommandLogs, 1, 'because a cached build prints features being installed in the image once at the start of the build when they are being resolved');
119+
assert.equal(featureNameInstanceCountInBuildWithNoCache, 2, 'because a non-cached build prints the features being installed in the image twice. Once at the start of the build when resolving the image, then again when installing the features in the image.');
120+
}
121+
});
122+
});
123+
67124
it('should fail with "not found" error when config is not found', async () => {
68125
let success = false;
69126
try {

0 commit comments

Comments
 (0)