mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
feat(cron): expose run receipts and modularize execution
Return committed trigger IDs and add CLI wait and trigger-specific logs while preserving API, CLI, transaction, deduplication and unknown-result semantics. Move scheduling, lifecycle, settlement, queries and legacy adapters into back/cron; remove obsolete internal import paths and circular dependencies. Apply project formatting across the repository, cover CJS/MJS in formatting hooks, add prettier:check and require formatting checks in AGENTS.md. Validation: backend build; 1398 backend/frontend tests passed (17 platform skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
This commit is contained in:
1 parent
77ece406ac
commit
c5458596c8
346 files changed
+32133
-11556
No files matched your search
@@ -4,7 +4,10 @@ const fs = require('node:fs/promises');
|
||||
const path = require('node:path');
|
||||
const os = require('node:os');
|
||||
const { execFileSync, spawnSync } = require('node:child_process');
|
||||
const { createContext, sourceEnvironment } = require('../../dist/internal/runtime/context');
|
||||
const {
|
||||
createContext,
|
||||
sourceEnvironment,
|
||||
} = require('../../dist/internal/runtime/context');
|
||||
const { executeTask } = require('../../dist/internal/execution/taskRunner');
|
||||
|
||||
test('Shell and CLI agree on isolated script state and account modes', async (t) => {
|
||||
@@ -31,7 +34,14 @@ test('Shell and CLI agree on isolated script state and account modes', async (t)
|
||||
path.join(root, 'static/build'),
|
||||
])
|
||||
await fs.mkdir(directory, { recursive: true });
|
||||
for (const name of ['task.sh', 'task-timeout.sh', 'otask.sh', 'share.sh', 'api.sh', 'env.sh'])
|
||||
for (const name of [
|
||||
'task.sh',
|
||||
'task-timeout.sh',
|
||||
'otask.sh',
|
||||
'share.sh',
|
||||
'api.sh',
|
||||
'env.sh',
|
||||
])
|
||||
await fs.copyFile(
|
||||
path.resolve(__dirname, '../../../shell', name),
|
||||
path.join(context.paths.dir_shell, name),
|
||||
@@ -406,9 +416,12 @@ test('configuration shell options survive the environment bridge for pipeline an
|
||||
assert.ok(env.SHELLOPTS.split(':').includes('pipefail'));
|
||||
assert.ok(!env.SHELLOPTS.split(':').includes('allexport'));
|
||||
// Bash 3.x cannot import BASHOPTS itself; exercise the actual CLI bridge.
|
||||
const execution = await require('../../dist/internal/runtime/process').runProcess(
|
||||
'bash', ['--noprofile', '--norc', '-c', probe], { env, capture: true },
|
||||
);
|
||||
const execution =
|
||||
await require('../../dist/internal/runtime/process').runProcess(
|
||||
'bash',
|
||||
['--noprofile', '--norc', '-c', probe],
|
||||
{ env, capture: true },
|
||||
);
|
||||
assert.equal(execution.code, 0);
|
||||
const modern = execution.stdout;
|
||||
assert.equal(modern, legacy);
|
||||
|
||||
@@ -9,19 +9,38 @@ const { installCliEntrypoints } = require('../../dist/local/entrypoints');
|
||||
test('generated entries treat quotes, newlines and code-like paths as data', async (t) => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-entry-paths-'));
|
||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||
const cliRoot = path.join(root, "CLI '中文\n\");throw new Error('injected');//");
|
||||
const cliRoot = path.join(
|
||||
root,
|
||||
"CLI '中文\n\");throw new Error('injected');//",
|
||||
);
|
||||
const bin = path.join(root, 'bin');
|
||||
await fs.mkdir(path.join(cliRoot, 'dist'), { recursive: true });
|
||||
await fs.writeFile(path.join(cliRoot, 'dist/ql.js'), 'exports.qlMain=async()=>{console.log(JSON.stringify(process.argv.slice(2)));return 7;};');
|
||||
await fs.writeFile(path.join(cliRoot, 'dist/task.js'), 'exports.taskMain=async()=>{throw new Error("secret-value");};');
|
||||
await fs.writeFile(
|
||||
path.join(cliRoot, 'dist/ql.js'),
|
||||
'exports.qlMain=async()=>{console.log(JSON.stringify(process.argv.slice(2)));return 7;};',
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(cliRoot, 'dist/task.js'),
|
||||
'exports.taskMain=async()=>{throw new Error("secret-value");};',
|
||||
);
|
||||
await installCliEntrypoints(bin, cliRoot);
|
||||
const ql = spawnSync(process.execPath, [path.join(bin, 'ql'), '--literal', 'value'], { encoding: 'utf8' });
|
||||
const ql = spawnSync(
|
||||
process.execPath,
|
||||
[path.join(bin, 'ql'), '--literal', 'value'],
|
||||
{ encoding: 'utf8' },
|
||||
);
|
||||
assert.equal(ql.status, 7, ql.stderr);
|
||||
assert.deepEqual(JSON.parse(ql.stdout), ['--literal', 'value']);
|
||||
for (const language of ['en', 'zh']) {
|
||||
const result = spawnSync(process.execPath, [path.join(bin, 'task')], { encoding: 'utf8', env: { QL_LANG: language } });
|
||||
const result = spawnSync(process.execPath, [path.join(bin, 'task')], {
|
||||
encoding: 'utf8',
|
||||
env: { QL_LANG: language },
|
||||
});
|
||||
assert.equal(result.status, 1);
|
||||
assert.equal(result.stderr, language === 'en' ? 'CLI invocation failed.\n' : 'CLI 调用失败。\n');
|
||||
assert.equal(
|
||||
result.stderr,
|
||||
language === 'en' ? 'CLI invocation failed.\n' : 'CLI 调用失败。\n',
|
||||
);
|
||||
assert.doesNotMatch(result.stderr, /secret-value/);
|
||||
}
|
||||
});
|
||||
@@ -5,23 +5,59 @@ const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
|
||||
test('legacy Shell and TS resolve executable paths after selecting the working directory', t => {
|
||||
const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'ql-executable-')));
|
||||
test('legacy Shell and TS resolve executable paths after selecting the working directory', (t) => {
|
||||
const root = fs.realpathSync(
|
||||
fs.mkdtempSync(path.join(os.tmpdir(), 'ql-executable-')),
|
||||
);
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
for (const dir of ['shell/preload', 'shell/lang', 'data/config', 'data/scripts/bin', 'data/scripts/custom', 'data/log', 'static/build', 'bin'])
|
||||
for (const dir of [
|
||||
'shell/preload',
|
||||
'shell/lang',
|
||||
'data/config',
|
||||
'data/scripts/bin',
|
||||
'data/scripts/custom',
|
||||
'data/log',
|
||||
'static/build',
|
||||
'bin',
|
||||
])
|
||||
fs.mkdirSync(path.join(root, dir), { recursive: true });
|
||||
for (const name of ['task.sh', 'task-timeout.sh', 'otask.sh', 'share.sh', 'api.sh', 'env.sh'])
|
||||
fs.copyFileSync(path.resolve(__dirname, '../../../shell', name), path.join(root, 'shell', name));
|
||||
for (const name of [
|
||||
'task.sh',
|
||||
'task-timeout.sh',
|
||||
'otask.sh',
|
||||
'share.sh',
|
||||
'api.sh',
|
||||
'env.sh',
|
||||
])
|
||||
fs.copyFileSync(
|
||||
path.resolve(__dirname, '../../../shell', name),
|
||||
path.join(root, 'shell', name),
|
||||
);
|
||||
for (const name of ['zh.sh', 'en.sh'])
|
||||
fs.copyFileSync(path.resolve(__dirname, '../../../shell/lang', name), path.join(root, 'shell/lang', name));
|
||||
fs.copyFileSync(
|
||||
path.resolve(__dirname, '../../../shell/lang', name),
|
||||
path.join(root, 'shell/lang', name),
|
||||
);
|
||||
fs.symlinkSync(process.execPath, path.join(root, 'bin/node'));
|
||||
fs.writeFileSync(path.join(root, 'bin/pnpm'), '#!/bin/sh\nexit 0\n', { mode: 0o755 });
|
||||
fs.writeFileSync(path.join(root, 'static/build/token.js'), 'process.stdout.write("fixture")');
|
||||
fs.writeFileSync(path.join(root, 'data/config/config.sh'),
|
||||
'no_tee=true\nmy_task() { printf "%s\\n" "$PWD" "$@" > "$TRACE"; }\n');
|
||||
fs.writeFileSync(path.join(root, 'bin/pnpm'), '#!/bin/sh\nexit 0\n', {
|
||||
mode: 0o755,
|
||||
});
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'static/build/token.js'),
|
||||
'process.stdout.write("fixture")',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'data/config/config.sh'),
|
||||
'no_tee=true\nmy_task() { printf "%s\\n" "$PWD" "$@" > "$TRACE"; }\n',
|
||||
);
|
||||
fs.writeFileSync(path.join(root, 'data/config/crontab.list'), '');
|
||||
const script = '#!/bin/sh\nprintf "%s\\n" "$PWD" "$@" > "$TRACE"\n';
|
||||
for (const file of ['data/scripts/bin/tool', 'data/scripts/bin/tool.exe', 'data/scripts/custom/tool', 'bin/path-tool'])
|
||||
for (const file of [
|
||||
'data/scripts/bin/tool',
|
||||
'data/scripts/bin/tool.exe',
|
||||
'data/scripts/custom/tool',
|
||||
'bin/path-tool',
|
||||
])
|
||||
fs.writeFileSync(path.join(root, file), script, { mode: 0o755 });
|
||||
const cases = [
|
||||
['bin/tool', '', 'bin'],
|
||||
@@ -36,56 +72,116 @@ test('legacy Shell and TS resolve executable paths after selecting the working d
|
||||
for (const [program, workDir, directory] of cases) {
|
||||
for (const [label, command, prefix] of [
|
||||
['shell', '/bin/bash', [path.join(root, 'shell/task.sh')]],
|
||||
['ts', process.execPath, [path.resolve(__dirname, '../../dist/runner.js'), '--root', root]],
|
||||
[
|
||||
'ts',
|
||||
process.execPath,
|
||||
[path.resolve(__dirname, '../../dist/runner.js'), '--root', root],
|
||||
],
|
||||
]) {
|
||||
const trace = path.join(root, `${label}.trace`);
|
||||
fs.rmSync(trace, { force: true });
|
||||
const result = spawnSync(command, [...prefix, program, 'first', 'two words'], {
|
||||
env: { PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`, QL_DIR: root, work_dir: workDir, TRACE: trace },
|
||||
encoding: 'utf8', timeout: 15000,
|
||||
});
|
||||
const result = spawnSync(
|
||||
command,
|
||||
[...prefix, program, 'first', 'two words'],
|
||||
{
|
||||
env: {
|
||||
PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`,
|
||||
QL_DIR: root,
|
||||
work_dir: workDir,
|
||||
TRACE: trace,
|
||||
},
|
||||
encoding: 'utf8',
|
||||
timeout: 15000,
|
||||
},
|
||||
);
|
||||
assert.equal(result.status, 0, `${label}/${program}: ${result.stderr}`);
|
||||
assert.equal(fs.readFileSync(trace, 'utf8'),
|
||||
`${path.join(root, 'data/scripts', directory)}\nfirst\ntwo words\n`, `${label}/${program}`);
|
||||
assert.equal(
|
||||
fs.readFileSync(trace, 'utf8'),
|
||||
`${path.join(root, 'data/scripts', directory)}\nfirst\ntwo words\n`,
|
||||
`${label}/${program}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
// A builtin has no executable file. Function arguments must remain literal.
|
||||
for (const args of [[':', 'unused'], ['my_task', '$(touch INJECTED)', '; exit 9']]) {
|
||||
const result = spawnSync(process.execPath,
|
||||
[path.resolve(__dirname, '../../dist/runner.js'), '--root', root, '--json', ...args], {
|
||||
env: { PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`, TRACE: path.join(root, 'literal.trace') },
|
||||
encoding: 'utf8', timeout: 15000,
|
||||
});
|
||||
for (const args of [
|
||||
[':', 'unused'],
|
||||
['my_task', '$(touch INJECTED)', '; exit 9'],
|
||||
]) {
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
path.resolve(__dirname, '../../dist/runner.js'),
|
||||
'--root',
|
||||
root,
|
||||
'--json',
|
||||
...args,
|
||||
],
|
||||
{
|
||||
env: {
|
||||
PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`,
|
||||
TRACE: path.join(root, 'literal.trace'),
|
||||
},
|
||||
encoding: 'utf8',
|
||||
timeout: 15000,
|
||||
},
|
||||
);
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
}
|
||||
assert.equal(fs.readFileSync(path.join(root, 'literal.trace'), 'utf8'),
|
||||
`${path.join(root, 'data/scripts')}\n$(touch INJECTED)\n; exit 9\n`);
|
||||
assert.equal(
|
||||
fs.readFileSync(path.join(root, 'literal.trace'), 'utf8'),
|
||||
`${path.join(root, 'data/scripts')}\n$(touch INJECTED)\n; exit 9\n`,
|
||||
);
|
||||
assert.equal(fs.existsSync(path.join(root, 'data/scripts/INJECTED')), false);
|
||||
fs.writeFileSync(path.join(root, 'data/config/config.sh'),
|
||||
'no_tee=true\nSTATE=before\nmy_task() { export STATE=after; return 7; }\n');
|
||||
fs.writeFileSync(path.join(root, 'data/config/task_after.sh'),
|
||||
'printf "%s:%s" "$STATE" "$_task_exit_code" > "$TRACE.after"\n');
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'data/config/config.sh'),
|
||||
'no_tee=true\nSTATE=before\nmy_task() { export STATE=after; return 7; }\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'data/config/task_after.sh'),
|
||||
'printf "%s:%s" "$STATE" "$_task_exit_code" > "$TRACE.after"\n',
|
||||
);
|
||||
for (const dir of ['', 'custom'])
|
||||
fs.writeFileSync(path.join(root, 'data/scripts', dir, 'show.sh'),
|
||||
'printf "%s" "$PWD" > "$TRACE"\n');
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'data/scripts', dir, 'show.sh'),
|
||||
'printf "%s" "$PWD" > "$TRACE"\n',
|
||||
);
|
||||
for (const scenario of ['function-state', 'hook-directory']) {
|
||||
fs.writeFileSync(path.join(root, 'data/config/task_before.sh'),
|
||||
scenario === 'hook-directory' ? 'work_dir=custom\n' : '');
|
||||
const args = scenario === 'function-state' ? ['my_task'] : ['show.sh', 'now'];
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'data/config/task_before.sh'),
|
||||
scenario === 'hook-directory' ? 'work_dir=custom\n' : '',
|
||||
);
|
||||
const args =
|
||||
scenario === 'function-state' ? ['my_task'] : ['show.sh', 'now'];
|
||||
for (const [label, command, prefix] of [
|
||||
['shell', '/bin/bash', [path.join(root, 'shell/task.sh')]],
|
||||
['ts', process.execPath, [path.resolve(__dirname, '../../dist/runner.js'), '--root', root]],
|
||||
[
|
||||
'ts',
|
||||
process.execPath,
|
||||
[path.resolve(__dirname, '../../dist/runner.js'), '--root', root],
|
||||
],
|
||||
]) {
|
||||
const trace = path.join(root, `${scenario}-${label}`);
|
||||
const result = spawnSync(command, [...prefix, ...args], {
|
||||
env: { PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`, QL_DIR: root, TRACE: trace },
|
||||
encoding: 'utf8', timeout: 15000,
|
||||
env: {
|
||||
PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`,
|
||||
QL_DIR: root,
|
||||
TRACE: trace,
|
||||
},
|
||||
encoding: 'utf8',
|
||||
timeout: 15000,
|
||||
});
|
||||
assert.equal(result.status, scenario === 'function-state' && label === 'ts' ? 7 : 0, result.stderr);
|
||||
assert.equal(
|
||||
result.status,
|
||||
scenario === 'function-state' && label === 'ts' ? 7 : 0,
|
||||
result.stderr,
|
||||
);
|
||||
if (scenario === 'function-state')
|
||||
assert.equal(fs.readFileSync(`${trace}.after`, 'utf8'), 'after:7');
|
||||
else
|
||||
assert.equal(fs.readFileSync(trace, 'utf8'), path.join(root, 'data/scripts/custom'));
|
||||
assert.equal(
|
||||
fs.readFileSync(trace, 'utf8'),
|
||||
path.join(root, 'data/scripts/custom'),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -59,15 +59,21 @@ require(entry).main(JSON.parse(process.argv[2])).then(code => {
|
||||
if (args.includes('--help'))
|
||||
assert.ok(
|
||||
!result.modules.some((module) =>
|
||||
/^remote[/\\](commands[/\\](auth|open|task|subscription)\.js|api[/\\](client|download)\.js|auth[/\\])/.test(module),
|
||||
/^remote[/\\](commands[/\\](auth|open|task|subscription)\.js|api[/\\](client|download)\.js|auth[/\\])/.test(
|
||||
module,
|
||||
),
|
||||
),
|
||||
'Help eagerly loaded command implementation',
|
||||
);
|
||||
if (args[0] === 'task' && !args.includes('--help'))
|
||||
assert.ok(result.modules.includes(path.join('remote', 'commands', 'task.js')));
|
||||
assert.ok(
|
||||
result.modules.includes(path.join('remote', 'commands', 'task.js')),
|
||||
);
|
||||
if (args[0] === 'subscription')
|
||||
assert.ok(
|
||||
result.modules.includes(path.join('remote', 'commands', 'subscription.js')),
|
||||
result.modules.includes(
|
||||
path.join('remote', 'commands', 'subscription.js'),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -10,24 +10,41 @@ function invoke(args) {
|
||||
require('./cli/dist/internal/commands/dispatch').dispatch = async command => ({code:200,data:command});
|
||||
require('./cli/dist/ql').qlMain(JSON.parse(process.argv[1])).then(code=>{process.exitCode=code});`;
|
||||
return spawnSync(process.execPath, ['-e', script, JSON.stringify(args)], {
|
||||
cwd: path.resolve(__dirname, '../../..'), encoding: 'utf8',
|
||||
env: { PATH: process.env.PATH, QL_LANG: 'en' }, timeout: 5000,
|
||||
cwd: path.resolve(__dirname, '../../..'),
|
||||
encoding: 'utf8',
|
||||
env: { PATH: process.env.PATH, QL_LANG: 'en' },
|
||||
timeout: 5000,
|
||||
});
|
||||
}
|
||||
|
||||
test('direct maintenance commands and local aliases accept modern and legacy options identically', () => {
|
||||
for (const [args, name, values, positionals] of [
|
||||
[['update', '--mirror', 'gitee', '--download-only', '--root', '/isolated'], 'update', {mirror:'gitee', 'download-only':true, root:'/isolated'}, []],
|
||||
[['update', 'false', '--root', '/isolated'], 'update', {'download-only':true, root:'/isolated'}, []],
|
||||
[['update', 'true'], 'update', {mirror:'github'}, []],
|
||||
[['reload', 'system', '--root', '/isolated'], 'reload', {target:'system', root:'/isolated'}, []],
|
||||
[['reload', '--target', 'data'], 'reload', {target:'data'}, []],
|
||||
[
|
||||
['update', '--mirror', 'gitee', '--download-only', '--root', '/isolated'],
|
||||
'update',
|
||||
{ mirror: 'gitee', 'download-only': true, root: '/isolated' },
|
||||
[],
|
||||
],
|
||||
[
|
||||
['update', 'false', '--root', '/isolated'],
|
||||
'update',
|
||||
{ 'download-only': true, root: '/isolated' },
|
||||
[],
|
||||
],
|
||||
[['update', 'true'], 'update', { mirror: 'github' }, []],
|
||||
[
|
||||
['reload', 'system', '--root', '/isolated'],
|
||||
'reload',
|
||||
{ target: 'system', root: '/isolated' },
|
||||
[],
|
||||
],
|
||||
[['reload', '--target', 'data'], 'reload', { target: 'data' }, []],
|
||||
[['check'], 'check', {}, []],
|
||||
[['rmlog', '7'], 'rmlog', {}, ['7']],
|
||||
[['start', '--no-startup'], 'start', {'no-startup':true}, []],
|
||||
[['start', '--no-startup'], 'start', { 'no-startup': true }, []],
|
||||
[['repair-config'], 'repair-config', {}, []],
|
||||
[['resetpwd', '--', '-literal-value'], 'resetpwd', {}, ['-literal-value']],
|
||||
[['update', '--root', 'false'], 'update', {root:'false'}, []],
|
||||
[['update', '--root', 'false'], 'update', { root: 'false' }, []],
|
||||
]) {
|
||||
const direct = invoke(['--json', ...args]);
|
||||
const alias = invoke(['--json', 'local', ...args]);
|
||||
@@ -37,15 +54,20 @@ test('direct maintenance commands and local aliases accept modern and legacy opt
|
||||
const command = JSON.parse(direct.stdout).data;
|
||||
assert.equal(command.name, `local ${name}`);
|
||||
assert.deepEqual(command.positionals, positionals);
|
||||
for (const [key, value] of Object.entries(values)) assert.equal(command.values[key], value);
|
||||
for (const [key, value] of Object.entries(values))
|
||||
assert.equal(command.values[key], value);
|
||||
}
|
||||
});
|
||||
|
||||
test('invalid maintenance options fail before dispatch; help recommends direct commands', () => {
|
||||
for (const args of [
|
||||
['update', 'invalid'], ['reload', 'invalid'], ['update', 'false', 'extra'],
|
||||
['reload', 'system', '--target', 'data'], ['update', '--unknown'],
|
||||
['update', '--', 'false'], ['rmlog', '-1'],
|
||||
['update', 'invalid'],
|
||||
['reload', 'invalid'],
|
||||
['update', 'false', 'extra'],
|
||||
['reload', 'system', '--target', 'data'],
|
||||
['update', '--unknown'],
|
||||
['update', '--', 'false'],
|
||||
['rmlog', '-1'],
|
||||
]) {
|
||||
const result = invoke(['--json', ...args]);
|
||||
assert.equal(result.status, 2, result.stderr);
|
||||
@@ -62,11 +84,13 @@ test('invalid maintenance options fail before dispatch; help recommends direct c
|
||||
}
|
||||
});
|
||||
|
||||
test('direct and aliased maintenance preserve logs, lifecycle and single configuration evaluation', async t => {
|
||||
test('direct and aliased maintenance preserve logs, lifecycle and single configuration evaluation', async (t) => {
|
||||
const fs = require('node:fs/promises');
|
||||
const os = require('node:os');
|
||||
const http = require('node:http');
|
||||
const exec = require('node:util').promisify(require('node:child_process').execFile);
|
||||
const exec = require('node:util').promisify(
|
||||
require('node:child_process').execFile,
|
||||
);
|
||||
for (const prefix of [[], ['local']]) {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-direct-extra-'));
|
||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||
@@ -77,24 +101,61 @@ test('direct and aliased maintenance preserve logs, lifecycle and single configu
|
||||
reports.push({ url: req.url, data: JSON.parse(body) });
|
||||
res.end(JSON.stringify({ code: 200 }));
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
||||
try {
|
||||
await fs.mkdir(path.join(root, 'data/config'), { recursive: true });
|
||||
await fs.writeFile(path.join(root, 'data/config/config.sh'), 'no_tee=true\nprintf x >> "$QL_DIR/loads"\n');
|
||||
await fs.writeFile(path.join(root, 'data/config/token.json'), JSON.stringify({value:'fixture', expiration:Date.now()/1000+3600}));
|
||||
await fs.writeFile(path.join(root, 'data/config/extra.sh'), 'printf "logged-extra\\n"\n');
|
||||
const result = await exec(process.execPath, [path.resolve(__dirname, '../../dist/ql.js'), ...prefix, 'extra', '--root', root, '--json'], {
|
||||
env: {PATH:process.env.PATH, QlPort:String(server.address().port), ID:'9'}, timeout:10000,
|
||||
});
|
||||
await fs.writeFile(
|
||||
path.join(root, 'data/config/config.sh'),
|
||||
'no_tee=true\nprintf x >> "$QL_DIR/loads"\n',
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(root, 'data/config/token.json'),
|
||||
JSON.stringify({
|
||||
value: 'fixture',
|
||||
expiration: Date.now() / 1000 + 3600,
|
||||
}),
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(root, 'data/config/extra.sh'),
|
||||
'printf "logged-extra\\n"\n',
|
||||
);
|
||||
const result = await exec(
|
||||
process.execPath,
|
||||
[
|
||||
path.resolve(__dirname, '../../dist/ql.js'),
|
||||
...prefix,
|
||||
'extra',
|
||||
'--root',
|
||||
root,
|
||||
'--json',
|
||||
],
|
||||
{
|
||||
env: {
|
||||
PATH: process.env.PATH,
|
||||
QlPort: String(server.address().port),
|
||||
ID: '9',
|
||||
},
|
||||
timeout: 10000,
|
||||
},
|
||||
);
|
||||
assert.equal(await fs.readFile(path.join(root, 'loads'), 'utf8'), 'x');
|
||||
assert.equal(result.stderr, '');
|
||||
const response = JSON.parse(result.stdout);
|
||||
assert.match(response.logPath, /^extra\//);
|
||||
assert.match(await fs.readFile(path.join(root, 'data/log', response.logPath), 'utf8'), /logged-extra/);
|
||||
assert.deepEqual(reports.map(report => report.data.status), ['0', '1']);
|
||||
assert.ok(reports.every(report => report.url === '/open/crons/status'));
|
||||
assert.match(
|
||||
await fs.readFile(
|
||||
path.join(root, 'data/log', response.logPath),
|
||||
'utf8',
|
||||
),
|
||||
/logged-extra/,
|
||||
);
|
||||
assert.deepEqual(
|
||||
reports.map((report) => report.data.status),
|
||||
['0', '1'],
|
||||
);
|
||||
assert.ok(reports.every((report) => report.url === '/open/crons/status'));
|
||||
} finally {
|
||||
await new Promise(resolve => server.close(resolve));
|
||||
await new Promise((resolve) => server.close(resolve));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -5,23 +5,54 @@ const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
|
||||
test('legacy Shell and TS preserve task stdin in each execution mode', t => {
|
||||
test('legacy Shell and TS preserve task stdin in each execution mode', (t) => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-stdin-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
for (const dir of ['shell/preload', 'shell/lang', 'data/config', 'data/scripts', 'data/log', 'static/build', 'bin'])
|
||||
for (const dir of [
|
||||
'shell/preload',
|
||||
'shell/lang',
|
||||
'data/config',
|
||||
'data/scripts',
|
||||
'data/log',
|
||||
'static/build',
|
||||
'bin',
|
||||
])
|
||||
fs.mkdirSync(path.join(root, dir), { recursive: true });
|
||||
for (const name of ['task.sh', 'task-timeout.sh', 'otask.sh', 'share.sh', 'api.sh', 'env.sh'])
|
||||
fs.copyFileSync(path.resolve(__dirname, '../../../shell', name), path.join(root, 'shell', name));
|
||||
for (const name of [
|
||||
'task.sh',
|
||||
'task-timeout.sh',
|
||||
'otask.sh',
|
||||
'share.sh',
|
||||
'api.sh',
|
||||
'env.sh',
|
||||
])
|
||||
fs.copyFileSync(
|
||||
path.resolve(__dirname, '../../../shell', name),
|
||||
path.join(root, 'shell', name),
|
||||
);
|
||||
for (const name of ['zh.sh', 'en.sh'])
|
||||
fs.copyFileSync(path.resolve(__dirname, '../../../shell/lang', name), path.join(root, 'shell/lang', name));
|
||||
fs.copyFileSync(
|
||||
path.resolve(__dirname, '../../../shell/lang', name),
|
||||
path.join(root, 'shell/lang', name),
|
||||
);
|
||||
fs.symlinkSync(process.execPath, path.join(root, 'bin/node'));
|
||||
fs.writeFileSync(path.join(root, 'bin/pnpm'), '#!/bin/sh\nexit 0\n', { mode: 0o755 });
|
||||
fs.writeFileSync(path.join(root, 'static/build/token.js'), 'process.stdout.write("fixture")');
|
||||
fs.writeFileSync(path.join(root, 'bin/pnpm'), '#!/bin/sh\nexit 0\n', {
|
||||
mode: 0o755,
|
||||
});
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'static/build/token.js'),
|
||||
'process.stdout.write("fixture")',
|
||||
);
|
||||
fs.writeFileSync(path.join(root, 'data/config/config.sh'), 'no_tee=true\n');
|
||||
fs.writeFileSync(path.join(root, 'data/config/crontab.list'), '');
|
||||
fs.writeFileSync(path.join(root, 'shell/preload/env.sh'), 'export ACCOUNTS=one\n');
|
||||
fs.writeFileSync(path.join(root, 'data/scripts/read.sh'),
|
||||
'IFS= read -r value\nprintf "value=%s\\n" "$value" > "$TRACE"\n');
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'shell/preload/env.sh'),
|
||||
'export ACCOUNTS=one\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'data/scripts/read.sh'),
|
||||
'IFS= read -r value\nprintf "value=%s\\n" "$value" > "$TRACE"\n',
|
||||
);
|
||||
const env = {
|
||||
PATH: `${path.join(root, 'bin')}:/usr/bin:/bin`,
|
||||
QL_DIR: root,
|
||||
@@ -29,18 +60,32 @@ test('legacy Shell and TS preserve task stdin in each execution mode', t => {
|
||||
no_delay: 'true',
|
||||
};
|
||||
for (const mode of ['normal', 'now', 'desi', 'conc']) {
|
||||
const args = ['read.sh', ...(mode === 'normal' ? [] : [mode]),
|
||||
...(['desi', 'conc'].includes(mode) ? ['ACCOUNTS', '1'] : [])];
|
||||
const args = [
|
||||
'read.sh',
|
||||
...(mode === 'normal' ? [] : [mode]),
|
||||
...(['desi', 'conc'].includes(mode) ? ['ACCOUNTS', '1'] : []),
|
||||
];
|
||||
for (const [label, command, prefix] of [
|
||||
['shell', '/bin/bash', [path.join(root, 'shell/task.sh')]],
|
||||
['ts', process.execPath, [path.resolve(__dirname, '../../dist/runner.js'), '--root', root]],
|
||||
[
|
||||
'ts',
|
||||
process.execPath,
|
||||
[path.resolve(__dirname, '../../dist/runner.js'), '--root', root],
|
||||
],
|
||||
]) {
|
||||
const trace = path.join(root, `${mode}-${label}`);
|
||||
const result = spawnSync(command, [...prefix, ...args], {
|
||||
env: { ...env, TRACE: trace }, input: 'hello\n', encoding: 'utf8', timeout: 15000,
|
||||
env: { ...env, TRACE: trace },
|
||||
input: 'hello\n',
|
||||
encoding: 'utf8',
|
||||
timeout: 15000,
|
||||
});
|
||||
assert.equal(result.status, 0, `${mode}/${label}: ${result.stderr}`);
|
||||
assert.equal(fs.readFileSync(trace, 'utf8'), 'value=hello\n', `${mode}/${label}`);
|
||||
assert.equal(
|
||||
fs.readFileSync(trace, 'utf8'),
|
||||
'value=hello\n',
|
||||
`${mode}/${label}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user