-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.mjs
More file actions
154 lines (135 loc) · 3.7 KB
/
Copy pathtest.mjs
File metadata and controls
154 lines (135 loc) · 3.7 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
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
import {dirname, join} from 'path';
import {execFile as originalExecFile} from 'child_process';
import {fileURLToPath} from 'url';
import {promisify} from 'util';
import libfaketimeEnv from 'libfaketime-env';
import test from 'tape';
import packageJson from './package.json';
const execFile = promisify(originalExecFile);
const node = process.execPath;
const projectDir = dirname(fileURLToPath(import.meta.url));
const reiwa = join(projectDir, packageJson.bin);
test('The `reiwa` command', async t => {
t.plan(12);
const env = {...process.env, ...await libfaketimeEnv()};
(async () => {
const {stderr, stdout} = await execFile(node, [reiwa], {
env: {
...env,
FAKETIME: '2020-01-01 00:00:00',
TZ: 'Asia/Tokyo'
}
});
t.equal(
stdout,
'2\n',
'should print the current year in the Reiwa period.'
);
t.equal(
stderr,
'',
'should write nothing to the stderr when there is no problem.'
);
})();
(async () => {
try {
await execFile(node, [reiwa, '--unknown'], {
env: {
...env,
FAKETIME: '2019-05-01 00:00:00',
TZ: 'Asia/Tokyo'
}
});
t.fail('Unexpectedly succeeded.');
} catch ({code, stderr, stdout}) {
t.equal(
stdout,
'1\n',
'should print 1 instead of 元 in 2019.'
);
t.equal(
stderr,
'(This program just shows the current year in the Reiwa era and doesn\'t have any options. The provided argument \'--unknown\' is ignored.)\n',
'should show a warning when an extra flag is provided.'
);
t.equal(
code,
9,
'should exit with code 9 when an extra flag is provided.'
);
}
})();
(async () => {
try {
await execFile(node, [reiwa, '--unknown0', '--unknown1'], {
env: {
...env,
FAKETIME: '2019-05-01 00:00:00',
// PGT (Papua New Guinea Time): UTC+10:00, JST+01:00
TZ: 'Pacific/Port_Moresby'
}
});
t.fail('Unexpectedly succeeded.');
} catch ({code, stderr, stdout}) {
t.ok(
/^The current Japanese era is not (?:令和|Reiwa) but (?:平成|Heisei)\.\n$/u.test(stdout),
'should print no year when it\'s not the Reiwa period.'
);
t.equal(
stderr,
'(This program just shows the current year in the Reiwa era and doesn\'t have any options. The provided arguments \'--unknown0\' and \'--unknown1\' are ignored.)\n',
'should show a warning when extra flags are provided.'
);
t.equal(
code,
19,
'should exit with code 19 when it\'s not the Reiwa period.'
);
}
})();
(async () => {
try {
if (process.env.TRAVIS) {
await execFile(join(projectDir, 'node-v12.0.0-linux-x64', 'bin', 'node'), [reiwa], {
env: {
NODE_V8_COVERAGE: process.env.NODE_V8_COVERAGE
}
});
} else {
const image = 'node:12.0.0-alpine';
await execFile('docker', ['pull', image]);
await execFile('docker', [
'run',
'--rm',
`--volume=${projectDir}:${projectDir}`,
`--workdir=${projectDir}`,
'--env',
`NODE_V8_COVERAGE=${process.env.NODE_V8_COVERAGE}`,
image,
'node',
reiwa
]);
}
t.fail('Unexpectedly succeeded.');
} catch ({code, stderr, stdout}) {
t.equal(
code,
1,
'should exit with a fatal signal when the Reiwa era is not supported.'
);
t.ok(
stderr.startsWith('Node.js currently running doesn\'t support the Reiwa era.'),
'should show a message when the Reiwa era is not supported.'
);
t.ok(
stderr.endsWith('Read https://nodejs.org/api/intl.html for more details about internationalization in Node.js.\n'),
'should show a link to the Node.js documentation when the Reiwa era is not supported.'
);
t.equal(
stdout,
'',
'should write nothing to the stdout when the Reiwa era is not supported.'
);
}
})();
});