-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
test.js
216 lines (151 loc) · 5.75 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import test from 'ava';
import proxyquireBase from 'proxyquire';
const proxyquire = proxyquireBase.noPreserveCache().noCallThru();
const expectedFallback = 'en-US';
const execaImport = './execa.js';
const noExeca = t => {
const fn = () => t.fail('Execa should not be called');
fn.stdout = async () => t.fail('Execa should not be called');
return fn;
};
const syncExeca = callback => ({
sync: (...args) => ({stdout: callback(...args)}),
});
const asyncExeca = callback => async (...args) => ({stdout: callback(...args)});
const setPlatform = platform => {
Object.defineProperty(process, 'platform', {value: platform});
};
const cache = {};
test.beforeEach(() => {
cache.env = process.env;
cache.platform = process.platform;
process.env = {};
setPlatform('linux');
});
test.afterEach.always(() => {
process.env = cache.env;
setPlatform(cache.platform);
});
/*
We're running tests in serial because we're mutating global state and we need to keep the tests separated.
*/
test.serial('Async retrieve locale from LC_ALL env as 1st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
process.env.LANG = 'en-GB2';
process.env.LC_MESSAGES = 'en-GB3';
process.env.LC_ALL = 'en-GB4';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)})();
t.is(locale, 'en-GB4');
});
test.serial('Async retrieve locale from LC_MESSAGES env as 2st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
process.env.LANG = 'en-GB2';
process.env.LC_MESSAGES = 'en-GB3';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)})();
t.is(locale, 'en-GB3');
});
test.serial('Async retrieve locale from LANG env as 3st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
process.env.LANG = 'en-GB2';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)})();
t.is(locale, 'en-GB2');
});
test.serial('Async retrieve locale from LANGUAGE env as 4st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)})();
t.is(locale, 'en-GB1');
});
test.serial('Sync retrieve locale from LC_ALL env as 1st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
process.env.LANG = 'en-GB2';
process.env.LC_MESSAGES = 'en-GB3';
process.env.LC_ALL = 'en-GB4';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)}).sync();
t.is(locale, 'en-GB4');
});
test.serial('Sync retrieve locale from LC_MESSAGES env as 2st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
process.env.LANG = 'en-GB2';
process.env.LC_MESSAGES = 'en-GB3';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)}).sync();
t.is(locale, 'en-GB3');
});
test.serial('Sync retrieve locale from LANG env as 3st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
process.env.LANG = 'en-GB2';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)}).sync();
t.is(locale, 'en-GB2');
});
test.serial('Sync retrieve locale from LANGUAGE env as 4st priority', async t => {
process.env.LANGUAGE = 'en-GB1';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)}).sync();
t.is(locale, 'en-GB1');
});
test.serial('Async normalises locale', async t => {
process.env.LC_ALL = 'en_GB';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)})();
t.is(locale, 'en-GB');
});
test.serial('Sync normalises locale', async t => {
process.env.LC_ALL = 'en_GB';
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)}).sync();
t.is(locale, 'en-GB');
});
test.serial('Async fallback locale when env variables missing and spawn=false ', async t => {
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)})({spawn: false});
t.is(locale, expectedFallback, 'Locale did not match expected fallback');
});
test.serial('Sync fallback locale when env variables missing and spawn=false ', async t => {
const locale = await proxyquire('./index.js', {[execaImport]: noExeca(t)}).sync({spawn: false});
t.is(locale, expectedFallback, 'Locale did not match expected fallback');
});
test.serial('Async handle darwin locale ', async t => {
setPlatform('darwin');
const execa = asyncExeca(cmd => cmd === 'defaults' ? 'en-GB' : ['en-US', 'en-GB']);
const locale = await proxyquire('./index.js', {[execaImport]: execa})();
t.is(locale, 'en-GB');
});
test.serial('Sync handle darwin locale ', async t => {
setPlatform('darwin');
const execa = syncExeca(cmd => cmd === 'defaults' ? 'en-GB' : ['en-US', 'en-GB']);
const locale = await proxyquire('./index.js', {[execaImport]: execa}).sync();
t.is(locale, 'en-GB');
});
test.serial('Async handle win32 locale ', async t => {
setPlatform('win32');
const execa = asyncExeca(() => 'Locale\n0809\n');
const locale = await proxyquire('./index.js', {[execaImport]: execa})();
t.is(locale, 'en-GB');
});
test.serial('Sync handle win32 locale ', async t => {
setPlatform('win32');
const execa = syncExeca(() => 'Locale\n0809\n');
const locale = await proxyquire('./index.js', {[execaImport]: execa}).sync();
t.is(locale, 'en-GB');
});
test.serial('Async handle linux locale ', async t => {
setPlatform('linux');
const execa = asyncExeca(() => `LANG="en-GB"
LC_COLLATE="en_GB"
LC_CTYPE="UTF-8"
LC_MESSAGES="en_GB"
LC_MONETARY="en_GB"
LC_NUMERIC="en_GB"
LC_TIME="en_GB"
LC_ALL=en_GB`);
const locale = await proxyquire('./index.js', {[execaImport]: execa})();
t.is(locale, 'en-GB');
});
test.serial('Sync handle linux locale ', async t => {
setPlatform('linux');
const execa = syncExeca(() => `LANG="en-GB"
LC_COLLATE="en_GB"
LC_CTYPE="UTF-8"
LC_MESSAGES="en_GB"
LC_MONETARY="en_GB"
LC_NUMERIC="en_GB"
LC_TIME="en_GB"
LC_ALL=en_GB`);
const locale = await proxyquire('./index.js', {[execaImport]: execa}).sync();
t.is(locale, 'en-GB');
});