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
3 changes: 2 additions & 1 deletion src/commands/actor/set-value.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cachedStdinInput } from '../../entrypoints/_shared.js';
import { APIFY_STORAGE_TYPES, getApifyStorageClient, getDefaultStorageId } from '../../lib/actor.js';
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
Expand Down Expand Up @@ -56,7 +57,7 @@ export class ActorSetValueCommand extends ApifyCommand<typeof ActorSetValueComma
const { contentType = 'application/json; charset=utf-8' } = this.flags;

// NOTE: If user pass value as argument and data on stdin same time. We use the value from argument.
const recordValue = value || process.stdin;
const recordValue = value ?? cachedStdinInput;
const apifyClient = await getApifyStorageClient();
const storeClient = apifyClient.keyValueStore(getDefaultStorageId(APIFY_STORAGE_TYPES.KEY_VALUE_STORE));

Expand Down
32 changes: 32 additions & 0 deletions test/e2e/commands/actor/set-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { access, readFile } from 'node:fs/promises';
import path from 'node:path';

import { runCli } from '../../__helpers__/run-cli.js';
import { createTestActor, removeTestActor, type TestActor } from '../../__helpers__/test-actor.js';

Expand All @@ -23,5 +26,34 @@ describe('[e2e] actor set-value', () => {
cwd: actor.dir,
});
expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0);

const recordPath = path.join(actor.dir, 'storage', 'key_value_stores', 'default', 'MY_KEY.json');
await expect(readFile(recordPath, 'utf-8')).resolves.toBe('{"hello":"world"}');
});

it('deletes a value when no value or stdin is provided', async () => {
const recordPath = path.join(actor.dir, 'storage', 'key_value_stores', 'default', 'DELETE_ME.json');
const setResult = await runCli('apify', ['actor', 'set-value', 'DELETE_ME', '{"exists":true}'], {
cwd: actor.dir,
});
expect(setResult.exitCode, `stderr: ${setResult.stderr}`).toBe(0);
await expect(readFile(recordPath, 'utf-8')).resolves.toBe('{"exists":true}');

const deleteResult = await runCli('apify', ['actor', 'set-value', 'DELETE_ME'], {
cwd: actor.dir,
});
expect(deleteResult.exitCode, `stderr: ${deleteResult.stderr}`).toBe(0);
await expect(access(recordPath)).rejects.toThrow();
});

it('sets a value from stdin', async () => {
const result = await runCli('apify', ['actor', 'set-value', 'FROM_STDIN', '--content-type', 'text/plain'], {
cwd: actor.dir,
stdin: 'VALUE',
});
expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0);

const recordPath = path.join(actor.dir, 'storage', 'key_value_stores', 'default', 'FROM_STDIN.txt');
await expect(readFile(recordPath, 'utf-8')).resolves.toBe('VALUE');
});
});