globals_arguments.test.ts 1.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import assert from 'assert/strict';

import { parseArgs } from 'common/globals/arguments';

const command = '--port 80 --experiment-id ID --action resume --experiments-directory DIR --log-level error';
const expected = {
    port: 80,
    experimentId: 'ID',
    action: 'resume',
    experimentsDirectory: 'DIR',
    logLevel: 'error',
    foreground: false,
    urlPrefix: '',

    mode: '',
    dispatcherPipe: undefined,
};

function testGoodShort(): void {
    const args = parseArgs(command.split(' '));
    assert.deepEqual(args, expected);
}

function testGoodLong(): void {
    const cmd = command + ' --url-prefix URL/prefix --foreground true';
    const args = parseArgs(cmd.split(' '));
    const expectedLong = Object.assign({}, expected);
    expectedLong.urlPrefix = 'URL/prefix';
    expectedLong.foreground = true;
    assert.deepEqual(args, expectedLong);
}

function testBadKey(): void {
    const cmd = command + ' --bad 1';
    assert.throws(() => parseArgs(cmd.split(' ')));
}

function testBadPos(): void {
    const cmd = command.replace('--port', 'port');
    assert.throws(() => parseArgs(cmd.split(' ')));
}

function testBadNum(): void {
    const cmd = command.replace('80', '8o');
    assert.throws(() => parseArgs(cmd.split(' ')));
}

function testBadBool(): void {
    const cmd = command + ' --foreground 1';
    assert.throws(() => parseArgs(cmd.split(' ')));
}

function testBadChoice(): void {
    const cmd = command.replace('resume', 'new');
    assert.throws(() => parseArgs(cmd.split(' ')));
}

describe('## globals.arguments ##', () => {
    it('good short', () => testGoodShort());
    it('good long', () => testGoodLong());
    it('bad key arg', () => testBadKey());
    it('bad positional arg', () => testBadPos());
    it('bad number', () => testBadNum());
    it('bad boolean', () => testBadBool());
    it('bad choice', () => testBadChoice());
});