forked from sindresorhus/os-locale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
105 lines (87 loc) · 2.64 KB
/
test.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import childProcess from 'child_process';
import test from 'ava';
import requireUncached from 'require-uncached';
const envVars = ['LC_ALL', 'LANGUAGE', 'LANG', 'LC_MESSAGES'];
const expectedFallback = 'en_US';
function unsetEnvVars(cache) {
envVars.forEach(envVar => {
if (process.env[envVar]) {
cache[envVar] = process.env[envVar];
delete process.env[envVar];
}
});
}
function restoreEnvVars(cache) {
envVars.forEach(envVar => {
if (cache[envVar]) {
process.env[envVar] = cache[envVar];
}
});
}
test.cb('async', t => {
t.plan(3);
requireUncached('./')((err, locale) => {
console.log('Locale identifier:', locale);
t.ifError(err);
t.true(locale.length > 1);
t.not(locale.indexOf('_'), -1);
t.end();
});
});
test('sync', t => {
const locale = requireUncached('./').sync();
console.log('Locale identifier:', locale);
t.true(locale.length > 1);
t.not(locale.indexOf('_'), -1);
});
test.cb.serial('async without spawn', t => {
t.plan(3);
const beforeTest = {};
// unset env vars and cache for restoration
unsetEnvVars(beforeTest);
// mock child_process.execFile
beforeTest.childProcessExecFile = childProcess.execFile;
childProcess.execFile = () => {
const args = Array.prototype.slice.call(arguments);
const execFileCb = args[args.length - 1];
if (typeof execFileCb === 'function') {
// passing Error here causes osLocale callback not to be called
execFileCb(null, 'spawn_NOTALLOWED');
}
};
// callback to restore env vars and undo mock
const afterTest = () => {
restoreEnvVars(beforeTest);
childProcess.execFile = beforeTest.childProcessExecFile;
};
// test async method
requireUncached('./')({spawn: false}, (err, locale) => {
console.log('Locale identifier:', locale);
afterTest();
t.ifError(err);
t.is(locale, expectedFallback, 'Locale did not match expected fallback');
t.not(locale, 'spawn_NOTALLOWED', 'Attempted to spawn subprocess');
t.end();
});
});
test.serial('sync without spawn', t => {
const beforeTest = {};
// unset env vars and cache for restoration
unsetEnvVars(beforeTest);
// mock child_process.execFileSync
if (childProcess.execFileSync) {
beforeTest.childProcessExecFileSync = childProcess.execFileSync;
childProcess.execFileSync = () => {
t.false('Attempted to spawn subprocess');
};
}
// test sync method
const locale = requireUncached('./').sync({spawn: false});
console.log('Locale identifier:', locale);
t.is(locale, expectedFallback, 'Locale did not match expected fallback');
// restore env vars and undo mock
restoreEnvVars(beforeTest);
if (beforeTest.childProcessExecFileSync) {
childProcess.execFileSync = beforeTest.childProcessExecFileSync;
}
});