From 2c21f19496a43a35e15acbbd1fa034089f633c81 Mon Sep 17 00:00:00 2001 From: chen zhihao <2490284538@qq.com> Date: Fri, 4 Sep 2026 04:42:56 +0800 Subject: [PATCH] Return failure status when playlist checks are offline (#49872) * fix: fail playlist tests when offline * test: move offline playlist check into test suite --- scripts/commands/playlist/test.ts | 12 +++++------- tests/__data__/input/offline_dns.cjs | 3 +++ tests/commands/playlist/test.test.ts | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 tests/__data__/input/offline_dns.cjs diff --git a/scripts/commands/playlist/test.ts b/scripts/commands/playlist/test.ts index 447455b7fb..8bbca3260d 100644 --- a/scripts/commands/playlist/test.ts +++ b/scripts/commands/playlist/test.ts @@ -58,6 +58,7 @@ const rootStorage = new Storage(ROOT_DIR) async function main() { if (await isOffline()) { logger.error(chalk.red('Internet connection is required for the script to work')) + process.exitCode = 1 return } @@ -195,13 +196,10 @@ async function onFinish(error: Error | null | undefined) { process.exit(0) } -async function isOffline() { - return new Promise((resolve, reject) => { - dns.lookup('info.cern.ch', err => { - if (err) resolve(true) - reject(false) - }) - }).catch(() => {}) +async function isOffline(): Promise { + return new Promise(resolve => { + dns.lookup('info.cern.ch', err => resolve(Boolean(err))) + }) } function getColor(stream: Stream): string { diff --git a/tests/__data__/input/offline_dns.cjs b/tests/__data__/input/offline_dns.cjs new file mode 100644 index 0000000000..a26fc10653 --- /dev/null +++ b/tests/__data__/input/offline_dns.cjs @@ -0,0 +1,3 @@ +const dns = require('node:dns') + +dns.lookup = (_hostname, callback) => callback(new Error('offline')) diff --git a/tests/commands/playlist/test.test.ts b/tests/commands/playlist/test.test.ts index 1deeacda75..d53e50a9c8 100644 --- a/tests/commands/playlist/test.test.ts +++ b/tests/commands/playlist/test.test.ts @@ -1,4 +1,5 @@ -import child_process from 'node:child_process' +import child_process, { execSync } from 'node:child_process' +import path from 'node:path' import { pathToFileURL } from 'node:url' import { promisify } from 'node:util' import * as fs from 'fs-extra' @@ -9,6 +10,7 @@ const exec = promisify(child_process.exec) type ExecError = { status: number stdout: string + stderr: string } const ENV_VAR = 'cross-env DATA_DIR=tests/__data__/input/data ROOT_DIR=tests/__data__/output' @@ -55,6 +57,20 @@ describe('playlist:test', () => { }) } }) + + it('fails when the network is unavailable', () => { + const preload = path.resolve('tests/__data__/input/offline_dns.cjs').replaceAll('\\', '/') + const cmd = + `cross-env NODE_OPTIONS="--require=${preload}" npm run playlist:test -- streams/af.m3u` + + try { + execSync(cmd, { encoding: 'utf8' }) + throw new Error('Expected playlist:test to fail offline') + } catch (error) { + const output = `${(error as ExecError).stdout || ''}${(error as ExecError).stderr || ''}` + expect(output).toContain('Internet connection is required for the script to work') + } + }) }) function content(filepath: string) {