Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(docker): check patchers against the production COPY and RUN

Yudhi Armyndharis68f1ef813e80
diff --git a/scripts/dockerfile-patchers.spec.js b/scripts/dockerfile-patchers.spec.js
index 6ad18f04c..270f16b70 100644
--- a/scripts/dockerfile-patchers.spec.js
+++ b/scripts/dockerfile-patchers.spec.js
@@ -18,25 +18,44 @@ const path = require('path');
const ROOT = path.join(__dirname, '..');
const dockerfile = fs.readFileSync(path.join(ROOT, 'Dockerfile'), 'utf8');
+// The production stage's instructions, comment lines dropped and continuation lines joined. Searching
+// the whole file let a RUN line, a comment or the builder stage stand in for the instruction a test
+// names, so a patcher dropped from the production COPY still passed.
+const production = dockerfile
+ .slice(dockerfile.search(/^FROM .* AS production$/m))
+ .split('\n')
+ .filter(line => !line.trimStart().startsWith('#'))
+ .join('\n')
+ .replace(/\\\n/g, ' ')
+ .split('\n');
+const copied = production.filter(line => line.startsWith('COPY ')).flatMap(line => line.trim().split(/\s+/));
+// Every RUN instruction's words, single-spaced and padded, so a name only matches as a whole word.
+const run = ` ${production
+ .filter(line => line.startsWith('RUN '))
+ .join(' ')
+ .split(/\s+/)
+ .join(' ')} `;
+
const patchers = fs
.readdirSync(__dirname)
.filter(f => f.startsWith('patch-') && f.endsWith('.js') && !f.endsWith('.spec.js'))
.sort();
-test('the fixture finds the patchers at all', () => {
+test('the fixture finds the patchers and the production stage at all', () => {
// Guard the guard: an empty list would make every assertion below vacuously pass.
assert.ok(patchers.length >= 5, `expected several patchers, found ${patchers.length}`);
+ assert.ok(copied.length > 0 && run.trim().length > 0, 'no COPY or RUN instruction found in the production stage');
});
test('every patcher is COPIED into the production stage', () => {
- const missing = patchers.filter(p => !dockerfile.includes(`scripts/${p}`));
+ const missing = patchers.filter(p => !copied.includes(`scripts/${p}`));
assert.deepEqual(missing, [], `not copied into the image: ${missing.join(', ')}`);
});
test('every patcher is RUN fatally after npm ci', () => {
// The postinstall hook runs them --best-effort; the explicit run is the real gate, so a shape
// change fails the image build instead of shipping unpatched.
- const missing = patchers.filter(p => !dockerfile.includes(`node scripts/${p}`));
+ const missing = patchers.filter(p => !run.includes(` node scripts/${p} `));
assert.deepEqual(missing, [], `never run in the image build: ${missing.join(', ')}`);
});