Skip to content

Commit d9bf8ed

Browse files
committed
extract parsing function and add regression tests
1 parent cfe0e26 commit d9bf8ed

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/commands/actors/push.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
getLocalUserInfo,
2626
getLoggedClientOrThrow,
2727
outputJobLog,
28+
parseWaitForFinishMillis,
2829
printJsonToStdout,
2930
} from '../../lib/utils.js';
3031

@@ -190,8 +191,7 @@ export class ActorsPushCommand extends ApifyCommand<typeof ActorsPushCommand> {
190191
buildTag = DEFAULT_BUILD_TAG;
191192
}
192193

193-
const parsedWaitForFinish = this.flags.waitForFinish ? Number.parseInt(this.flags.waitForFinish, 10) : Number.NaN;
194-
const waitForFinishMillis = Number.isFinite(parsedWaitForFinish) ? parsedWaitForFinish * 1000 : undefined;
194+
const waitForFinishMillis = parseWaitForFinishMillis(this.flags.waitForFinish);
195195

196196
// User can override actorId of pushing Actor.
197197
// It causes that we push Actor to this id but attributes in localConfig will remain same.

src/lib/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,3 +840,10 @@ export function shellConfigFile(userHomeDirectory: string, shell: ReturnType<typ
840840
}
841841
}
842842
}
843+
844+
export function parseWaitForFinishMillis(flag: string | undefined): number | undefined {
845+
if (flag === undefined) return undefined;
846+
const parsed = Number.parseInt(flag, 10);
847+
if (!Number.isFinite(parsed) || parsed <= 0) return undefined;
848+
return parsed * 1000;
849+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { parseWaitForFinishMillis } from '../../../src/lib/utils.js';
2+
3+
describe('parseWaitForFinishMillis()', () => {
4+
it('returns undefined when flag is omitted', () => {
5+
expect(parseWaitForFinishMillis(undefined)).toBeUndefined();
6+
});
7+
8+
it('returns undefined for non-numeric input', () => {
9+
expect(parseWaitForFinishMillis('abc')).toBeUndefined();
10+
});
11+
12+
it('returns undefined for zero', () => {
13+
expect(parseWaitForFinishMillis('0')).toBeUndefined();
14+
});
15+
16+
it('returns undefined for negative values', () => {
17+
expect(parseWaitForFinishMillis('-5')).toBeUndefined();
18+
});
19+
20+
it('converts positive seconds to milliseconds', () => {
21+
expect(parseWaitForFinishMillis('30')).toBe(30_000);
22+
});
23+
});

0 commit comments

Comments
 (0)