shell_utils.test.ts 1.65 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import assert from 'assert/strict';
import fs from 'fs';
import fsPromises from 'fs/promises';
import os from 'os';
import path from 'path';

import { createScriptFile } from 'common/shellUtils';

let tempDir: string | null = null;
const asciiScript = 'echo hello\necho world';
const unicodeScript = 'echo 你好\necho 世界';

/* test cases */

async function testAscii(): Promise<void> {
    const file = path.join(tempDir!, 'test1.ps1');
    await createScriptFile(file, asciiScript);
    const script = await fsPromises.readFile(file, { encoding: 'utf8' });
    assert.equal(script, asciiScript);
}

async function testUnicode(): Promise<void> {
    const file = path.join(tempDir!, 'test2.ps1');
    await createScriptFile(file, unicodeScript);
    const script = await fsPromises.readFile(file, { encoding: 'utf8' });
    assert.equal(script, '\uFEFF' + unicodeScript);
}

async function testBash(): Promise<void> {
    const file = path.join(tempDir!, 'test.sh');
    await createScriptFile(file, unicodeScript);
    const script = await fsPromises.readFile(file, { encoding: 'utf8' });
    assert.equal(script, unicodeScript);
}

/* environment */

function beforeHook() {
    tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nni-ut-'));
}

function afterHook() {
    if (tempDir) {
        fs.rmSync(tempDir, { force: true, recursive: true });
    }
}

/* register */

describe('## common.shell_utils ##', () => {
    before(beforeHook);

    it('powershell ascii', () => testAscii());
    it('powershell unicode', () => testUnicode());
    it('bash unicode', () => testBash());

    after(afterHook);
});