Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
96 changes: 89 additions & 7 deletions scripts/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
import { execSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { mkdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { homedir, tmpdir } from 'node:os';
import { join } from 'node:path';

const ACTIONLINT_VERSION = '1.7.12';
const SHELLCHECK_VERSION = '0.11.0';
const YAMLLINT_VERSION = '1.35.1';
const ACTIONLINT_SHA256 = {
linux_amd64:
'8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8',
darwin_amd64:
'5b44c3bc2255115c9b69e30efc0fecdf498fdb63c5d58e17084fd5f16324c644',
darwin_arm64:
'aba9ced2dee8d27fecca3dc7feb1a7f9a52caefa1eb46f3271ea66b6e0e6953f',
};
const SHELLCHECK_SHA256 = {
'linux.x86_64':
'8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198',
'darwin.x86_64':
'3c89db4edcab7cf1c27bff178882e0f6f27f7afdf54e859fa041fca10febe4c6',
'darwin.aarch64':
'56affdd8de5527894dca6dc3d7e0a99a873b0f004d7aabc30ae407d3f48b0a79',
};

function sanitizePathPart(value) {
return value.replace(/[^A-Za-z0-9._-]/g, '_');
Expand Down Expand Up @@ -46,7 +62,49 @@ export function getLinterTempDir({
return join(baseDir, 'qwen-code-linters', `local-${workspaceHash}`);
}

export function getLinterCacheDir({
env = process.env,
homeDir = homedir(),
} = {}) {
return join(
env.XDG_CACHE_HOME || join(homeDir, '.cache'),
'qwen-code',
'linters',
);
}

export function getCachedArchiveInstaller({
cacheArchive,
localArchive,
expectedSha256,
downloadUrl,
archiveCheck,
extract,
executable,
}) {
return `
set -e
verify_sha256() {
"${process.execPath}" -e 'const {createHash}=require("node:crypto");const {readFileSync}=require("node:fs");process.exit(createHash("sha256").update(readFileSync(process.argv[1])).digest("hex")===process.argv[2]?0:1)' "$1" "$2"
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
}
if ! cp "${cacheArchive}" "${localArchive}" 2>/dev/null \
|| ! verify_sha256 "${localArchive}" "${expectedSha256}"; then
rm -f "${localArchive}"
curl -fsSL --retry 2 --retry-connrefused --connect-timeout 10 --max-time 90 \
-o "${localArchive}" "${downloadUrl}"
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
verify_sha256 "${localArchive}" "${expectedSha256}"
${archiveCheck}
cp "${localArchive}" "${cacheArchive}.$$"
mv -f "${cacheArchive}.$$" "${cacheArchive}"
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
fi
${extract}
test -x "${executable}"
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
`;
}

const TEMP_DIR = getLinterTempDir();
// Share versioned archives; extracted binaries stay job-scoped in TEMP_DIR.
const CACHE_DIR = getLinterCacheDir();
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

function getPlatformArch() {
const platform = process.platform;
Expand Down Expand Up @@ -88,13 +146,30 @@ let lintersCache;
function getLinters() {
if (!lintersCache) {
const platformArch = getPlatformArch();
const actionlintArchive = join(
CACHE_DIR,
`actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz`,
);
const shellcheckArchive = join(
CACHE_DIR,
`shellcheck_${SHELLCHECK_VERSION}_${platformArch.shellcheck}.tar.xz`,
);
const actionlintLocalArchive = join(TEMP_DIR, '.actionlint.tgz');
const shellcheckLocalArchive = join(TEMP_DIR, '.shellcheck.txz');
lintersCache = {
actionlint: {
check: 'command -v actionlint',
installer: `
mkdir -p "${TEMP_DIR}/actionlint"
curl -sSLo "${TEMP_DIR}/.actionlint.tgz" "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz"
tar -xzf "${TEMP_DIR}/.actionlint.tgz" -C "${TEMP_DIR}/actionlint"
mkdir -p "${TEMP_DIR}/actionlint" "${CACHE_DIR}"
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
${getCachedArchiveInstaller({
cacheArchive: actionlintArchive,
localArchive: actionlintLocalArchive,
expectedSha256: ACTIONLINT_SHA256[platformArch.actionlint],
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
downloadUrl: `https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz`,
archiveCheck: `tar -tzf "${actionlintLocalArchive}" >/dev/null`,
extract: `tar -xzf "${actionlintLocalArchive}" -C "${TEMP_DIR}/actionlint"`,
executable: join(TEMP_DIR, 'actionlint', 'actionlint'),
})}
`,
run: `
actionlint \
Expand All @@ -111,9 +186,16 @@ function getLinters() {
shellcheck: {
check: 'command -v shellcheck',
installer: `
mkdir -p "${TEMP_DIR}/shellcheck"
curl -sSLo "${TEMP_DIR}/.shellcheck.txz" "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${platformArch.shellcheck}.tar.xz"
tar -xf "${TEMP_DIR}/.shellcheck.txz" -C "${TEMP_DIR}/shellcheck" --strip-components=1
mkdir -p "${TEMP_DIR}/shellcheck" "${CACHE_DIR}"
${getCachedArchiveInstaller({
cacheArchive: shellcheckArchive,
localArchive: shellcheckLocalArchive,
expectedSha256: SHELLCHECK_SHA256[platformArch.shellcheck],
downloadUrl: `https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${platformArch.shellcheck}.tar.xz`,
archiveCheck: `tar -tf "${shellcheckLocalArchive}" >/dev/null`,
extract: `tar -xf "${shellcheckLocalArchive}" -C "${TEMP_DIR}/shellcheck" --strip-components=1`,
executable: join(TEMP_DIR, 'shellcheck', 'shellcheck'),
})}
`,
run: `
git ls-files | grep -v '^integration-tests/terminal-bench/' | grep -E '^([^.]+|.*\\.(sh|zsh|bash))' | xargs file --mime-type \
Expand Down
94 changes: 93 additions & 1 deletion scripts/tests/lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { execSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import {
chmodSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

// getLinterTempDir joins with the platform separator; compare normalized
// paths so the suite also passes on the Windows gate.
const toPosix = (value) => value.replaceAll(path.sep, '/');

describe('getLinterTempDir', () => {
describe('linter directories', () => {
const originalArgv = process.argv;

beforeEach(() => {
Expand Down Expand Up @@ -69,4 +80,85 @@ describe('getLinterTempDir', () => {
expect(toPosix(second)).toMatch(/\/qwen-code-linters\/local-[a-f0-9]{16}$/);
expect(first).not.toBe(second);
});

it('shares cached downloads across GitHub Actions runs', async () => {
const { getLinterCacheDir } = await import('../lint.js');

const first = getLinterCacheDir({
env: {
XDG_CACHE_HOME: '/runner/cache',
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
GITHUB_RUN_ID: '31583913822',
},
});
const second = getLinterCacheDir({
env: {
XDG_CACHE_HOME: '/runner/cache',
GITHUB_RUN_ID: '31583913823',
},
});

expect(toPosix(first)).toBe('/runner/cache/qwen-code/linters');
expect(second).toBe(first);
});

it.skipIf(process.platform === 'win32')(
'replaces an untrusted cache and reuses the verified archive offline',
async () => {
const { getCachedArchiveInstaller } = await import('../lint.js');
const root = mkdtempSync(path.join(tmpdir(), 'linter-cache-'));

try {
const binDir = path.join(root, 'bin');
const cacheArchive = path.join(root, 'cache', 'tool.tar');
const localArchive = path.join(root, 'job', 'tool.tar');
const executable = path.join(root, 'job', 'tool');
const fixture = path.join(root, 'official.tar');
const curlLog = path.join(root, 'curl.log');
const curl = path.join(binDir, 'curl');
mkdirSync(binDir, { recursive: true });
mkdirSync(path.dirname(cacheArchive), { recursive: true });
mkdirSync(path.dirname(localArchive), { recursive: true });
writeFileSync(cacheArchive, 'validator-passing plant');
writeFileSync(fixture, 'official archive');
writeFileSync(
curl,
'#!/bin/sh\nprintf "download\\n" >> "$CURL_LOG"\nwhile [ "$#" -gt 0 ]; do\n if [ "$1" = "-o" ]; then cp "$FIXTURE_ARCHIVE" "$2"; exit; fi\n shift\ndone\nexit 1\n',
);
chmodSync(curl, 0o755);

const expectedSha256 = createHash('sha256')
.update(readFileSync(fixture))
.digest('hex');
const installer = getCachedArchiveInstaller({
cacheArchive,
localArchive,
expectedSha256,
downloadUrl: 'https://example.invalid/tool.tar',
archiveCheck: 'true',
extract: `cp "${localArchive}" "${executable}" && chmod +x "${executable}"`,
executable,
});
const env = {
...process.env,
PATH: `${binDir}:${process.env.PATH}`,
CURL_LOG: curlLog,
FIXTURE_ARCHIVE: fixture,
};

execSync(installer, { env });
expect(readFileSync(cacheArchive, 'utf8')).toBe('official archive');
expect(readFileSync(executable, 'utf8')).toBe('official archive');
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
expect(readFileSync(curlLog, 'utf8')).toBe('download\n');

rmSync(localArchive);
rmSync(executable);
rmSync(fixture);
execSync(installer, { env });
expect(readFileSync(executable, 'utf8')).toBe('official archive');
expect(readFileSync(curlLog, 'utf8')).toBe('download\n');
} finally {
rmSync(root, { recursive: true, force: true });
}
},
);
});
Loading