Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions __tests__/flashlight/ps-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const execMock = jest.fn();
jest.mock('@actions/exec', () => ({ exec: (...a: unknown[]) => execMock(...a) }));

import * as core from '@actions/core';
import { psVersionFromImageTag, readPsVersionFromContainer, resolvePsVersion } from '../../src/flashlight/ps-version';
import { execFileSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { psVersionFromImageTag, readPsVersionFromContainer, readSourceVersionPhp, resolvePsVersion } from '../../src/flashlight/ps-version';

type ExecOpts = { listeners?: { stdout?: (b: Buffer) => void } };

Expand Down Expand Up @@ -45,7 +49,7 @@ describe('readPsVersionFromContainer', () => {
expect(execMock).toHaveBeenCalledTimes(1);
});

it('falls back on AppKernel when PS_VERSION is missing', async () => {
it('falls back on the shop sources when PS_VERSION is missing', async () => {
execMock
.mockImplementationOnce(execPrinting(''))
.mockImplementationOnce(execPrinting('8.1.7'));
Expand All @@ -63,6 +67,38 @@ describe('readPsVersionFromContainer', () => {
});
});

// The snippet itself, run with the local php on the shapes of the real files
// (checked in the 1.7.8.11, 8.1.7, 8.2.8, 9.0.0 and 9.2.0 Flashlight images).
const hasPhp = (() => { try { execFileSync('php', ['-v'], { stdio: 'ignore' }); return true; } catch { return false; } })();
(hasPhp ? describe : describe.skip)('readSourceVersionPhp', () => {
function shop(files: Record<string, string>): string {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ps-src-'));
for (const [f, body] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, f)), { recursive: true });
fs.writeFileSync(path.join(root, f), body);
}
return root;
}
const run = (root: string): string => execFileSync('php', ['-r', readSourceVersionPhp(root)]).toString();

it('reads src/Core/Version.php (8.0 and later), where AppKernel only refers to it', () => {
const root = shop({
'src/Core/Version.php': "<?php\nclass Version\n{\n public const VERSION = '8.2.8';\n}\n",
'app/AppKernel.php': "<?php\nclass AppKernel\n{\n const VERSION = Version::VERSION;\n}\n",
});
expect(run(root)).toBe('8.2.8');
});

it('reads app/AppKernel.php on 1.7', () => {
const root = shop({ 'app/AppKernel.php': "<?php\nclass AppKernel\n{\n const VERSION = '1.7.8.11';\n}\n" });
expect(run(root)).toBe('1.7.8.11');
});

it('prints nothing when neither declares it', () => {
expect(run(shop({}))).toBe('');
});
});

describe('resolvePsVersion', () => {
it('takes the version from the tag without asking the shop', async () => {
await expect(resolvePsVersion({ imageTag: '8.2.8-nginx', composePath: '/c' })).resolves.toBe('8.2.8');
Expand Down
18 changes: 14 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126624,6 +126624,7 @@ var __importStar = (this && this.__importStar) || (function () {
})();
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.psVersionFromImageTag = psVersionFromImageTag;
exports.readSourceVersionPhp = readSourceVersionPhp;
exports.readPsVersionFromContainer = readPsVersionFromContainer;
exports.resolvePsVersion = resolvePsVersion;
const core = __importStar(__nccwpck_require__(37484));
Expand Down Expand Up @@ -126654,8 +126655,17 @@ async function execInShop(composePath, command) {
return output.trim().split('\n').pop()?.trim() ?? '';
}
const isBareVersion = (v) => v !== '' && psVersionFromImageTag(v) === v;
// Fallback for an image without PS_VERSION: releases up to 9.1 declare it in AppKernel.
const READ_APPKERNEL_PHP = `echo preg_match("/const VERSION = '([0-9.]+)'/", (string) @file_get_contents('/var/www/html/app/AppKernel.php'), $m) ? $m[1] : '';`;
/**
* PHP code (for `php -r`) printing the version declared in the shop's sources,
* '' when none is found. Fallback for an image without PS_VERSION.
* From 8.0 the literal sits in src/Core/Version.php
* (`public const VERSION = '8.2.8';`), and AppKernel only refers to it
* (`const VERSION = Version::VERSION;`); 1.7 declares it in AppKernel.
*/
function readSourceVersionPhp(root = '/var/www/html') {
const files = ['src/Core/Version.php', 'app/AppKernel.php'].map(f => `'${root}/${f}'`).join(', ');
return `foreach ([${files}] as $f) { if (preg_match("/const VERSION = '([0-9.]+)'/", (string) @file_get_contents($f), $m)) { echo $m[1]; break; } }`;
}
/**
* Read the version from the running shop, for a tag that does not carry it.
* Flashlight images declare it in their PS_VERSION environment variable
Expand All @@ -126665,8 +126675,8 @@ async function readPsVersionFromContainer(composePath) {
const fromEnv = await execInShop(composePath, ['printenv', 'PS_VERSION']);
if (isBareVersion(fromEnv))
return fromEnv;
const fromKernel = await execInShop(composePath, ['php', '-r', READ_APPKERNEL_PHP]);
return isBareVersion(fromKernel) ? fromKernel : null;
const fromSources = await execInShop(composePath, ['php', '-r', readSourceVersionPhp()]);
return isBareVersion(fromSources) ? fromSources : null;
}
/**
* Version given to the library as PRESTAFLOW_PS_VERSION: from the tag, else
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions src/flashlight/ps-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ async function execInShop(composePath: string, command: string[]): Promise<strin

const isBareVersion = (v: string): boolean => v !== '' && psVersionFromImageTag(v) === v;

// Fallback for an image without PS_VERSION: releases up to 9.1 declare it in AppKernel.
const READ_APPKERNEL_PHP =
`echo preg_match("/const VERSION = '([0-9.]+)'/", (string) @file_get_contents('/var/www/html/app/AppKernel.php'), $m) ? $m[1] : '';`;
/**
* PHP code (for `php -r`) printing the version declared in the shop's sources,
* '' when none is found. Fallback for an image without PS_VERSION.
* From 8.0 the literal sits in src/Core/Version.php
* (`public const VERSION = '8.2.8';`), and AppKernel only refers to it
* (`const VERSION = Version::VERSION;`); 1.7 declares it in AppKernel.
*/
export function readSourceVersionPhp(root = '/var/www/html'): string {
const files = ['src/Core/Version.php', 'app/AppKernel.php'].map(f => `'${root}/${f}'`).join(', ');
return `foreach ([${files}] as $f) { if (preg_match("/const VERSION = '([0-9.]+)'/", (string) @file_get_contents($f), $m)) { echo $m[1]; break; } }`;
}

/**
* Read the version from the running shop, for a tag that does not carry it.
Expand All @@ -41,8 +49,8 @@ const READ_APPKERNEL_PHP =
export async function readPsVersionFromContainer(composePath: string): Promise<string | null> {
const fromEnv = await execInShop(composePath, ['printenv', 'PS_VERSION']);
if (isBareVersion(fromEnv)) return fromEnv;
const fromKernel = await execInShop(composePath, ['php', '-r', READ_APPKERNEL_PHP]);
return isBareVersion(fromKernel) ? fromKernel : null;
const fromSources = await execInShop(composePath, ['php', '-r', readSourceVersionPhp()]);
return isBareVersion(fromSources) ? fromSources : null;
}

/**
Expand Down
Loading