Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,9 @@ export const outputJobLog = async ({
}

const log = await client.log(logId).get();
process.stderr.write(log!);
if (log != null) {
process.stderr.write(log);
}
return;
}

Expand Down
60 changes: 59 additions & 1 deletion test/local/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { existsSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import process from 'node:process';

import axios from 'axios';

import { ACTOR_JOB_STATUSES } from '@apify/consts';

import { execWithLog } from '../../../src/lib/exec.js';
import { ensureFolderExistsSync } from '../../../src/lib/files.js';
import { inputFileRegExp } from '../../../src/lib/input-key.js';
import { createActZip, downloadAndUnzip, getActorLocalFilePaths } from '../../../src/lib/utils.js';
import { createActZip, downloadAndUnzip, getActorLocalFilePaths, outputJobLog } from '../../../src/lib/utils.js';
import { useTempPath } from '../../__setup__/hooks/useTempPath.js';
import { withRetries } from '../../__setup__/hooks/withRetries.js';

Expand Down Expand Up @@ -104,6 +107,61 @@ describe('Utils', () => {
});
});

describe('outputJobLog()', () => {
const originalNoLogs = process.env.APIFY_NO_LOGS_IN_TESTS;

const makeClientWithLog = (log: string | null | undefined) =>
({
log: () => ({
get: async () => log,
stream: async () => undefined,
}),
}) as never;

const terminalJob = {
id: 'job-id',
status: ACTOR_JOB_STATUSES.SUCCEEDED,
} as never;

beforeEach(() => {
delete process.env.APIFY_NO_LOGS_IN_TESTS;
});

afterEach(() => {
vitest.restoreAllMocks();

if (originalNoLogs === undefined) {
delete process.env.APIFY_NO_LOGS_IN_TESTS;
} else {
process.env.APIFY_NO_LOGS_IN_TESTS = originalNoLogs;
}
});

it('writes an existing terminal job log to stderr', async () => {
const stderrWrite = vitest.spyOn(process.stderr, 'write').mockImplementation(() => true);

await outputJobLog({ job: terminalJob, apifyClient: makeClientWithLog('hello\n') });

expect(stderrWrite).toHaveBeenCalledWith('hello\n');
});

it.each([undefined, null])('does not throw or write when a terminal job log is %s', async (log) => {
const stderrWrite = vitest.spyOn(process.stderr, 'write').mockImplementation(() => true);

await expect(outputJobLog({ job: terminalJob, apifyClient: makeClientWithLog(log) })).resolves.toBeUndefined();

expect(stderrWrite).not.toHaveBeenCalled();
});

it('keeps empty terminal job logs as valid write input', async () => {
const stderrWrite = vitest.spyOn(process.stderr, 'write').mockImplementation(() => true);

await outputJobLog({ job: terminalJob, apifyClient: makeClientWithLog('') });

expect(stderrWrite).toHaveBeenCalledWith('');
});
});

describe('input file regex', () => {
const validFiles = ['INPUT', 'INPUT.json', 'INPUT.bin'];

Expand Down