-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest.js
More file actions
70 lines (60 loc) · 1.89 KB
/
test.js
File metadata and controls
70 lines (60 loc) · 1.89 KB
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
70
import fs from 'node:fs/promises';
import path from 'node:path';
import test from 'ava';
import {shellEnv, shellEnvSync} from './index.js';
test('async', async t => {
const env = await shellEnv();
t.true('HOME' in env);
t.false('' in env);
t.is(env.ZSH_TMUX_AUTOSTARTED, 'true');
t.is(env.ZSH_TMUX_AUTOSTART, 'false');
});
test('sync', t => {
const env = shellEnvSync();
t.true('HOME' in env);
t.false('' in env);
t.is(env.ZSH_TMUX_AUTOSTARTED, 'true');
t.is(env.ZSH_TMUX_AUTOSTART, 'false');
});
test('sync - ignores env alias', async t => {
const temporaryDirectoryPrefix = path.join(process.cwd(), 'temporary-shell-env-');
const temporaryDirectory = await fs.mkdtemp(temporaryDirectoryPrefix);
try {
const bashConfigurationPath = path.join(temporaryDirectory, '.bashrc');
const bashWrapperPath = path.join(temporaryDirectory, 'bash-wrapper.sh');
const bashWrapperContents = [
'#!/bin/sh',
`HOME="${temporaryDirectory}"`,
'export HOME',
'exec /bin/bash "$@"',
'',
].join('\n');
await fs.writeFile(bashConfigurationPath, 'alias env="printf no"\n');
await fs.writeFile(bashWrapperPath, bashWrapperContents);
await fs.chmod(bashWrapperPath, 0o755);
const env = shellEnvSync(bashWrapperPath);
t.is(env.HOME, temporaryDirectory);
} finally {
await fs.rm(temporaryDirectory, {recursive: true, force: true});
}
});
test('async - with custom shell', async t => {
const shell = '/bin/bash';
const env = await shellEnv(shell);
t.true('HOME' in env);
t.false('' in env);
});
test('sync - with custom shell', t => {
const shell = '/bin/bash';
const env = shellEnvSync(shell);
t.true('HOME' in env);
t.false('' in env);
});
test('async - with custom shell throws on non-executable', async t => {
await t.throwsAsync(shellEnv('non-executable'));
});
test('sync - with custom shell throws on non-executable', t => {
t.throws(() => {
shellEnvSync('non-executable');
});
});