-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
163 lines (145 loc) · 3.82 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
import fs from 'node:fs';
import test from 'ava';
import parseColumns from './index.js';
const fixture1 = fs.readFileSync('fixtures/ps.out', 'utf8');
const fixture2 = fs.readFileSync('fixtures/ps-2.out', 'utf8');
const fixture3 = fs.readFileSync('fixtures/lsof.out', 'utf8');
const fixture4 = fs.readFileSync('fixtures/ps-3.out', 'utf8');
test.after('benchmark', () => {
const count = 30;
let total = 0;
for (let index = 0; index < count; index++) {
const start = Date.now();
parseColumns(fixture3);
total += Date.now() - start;
}
console.log(`${count} iterations: ${total / (1000 * count)}s`);
});
test('parse', t => {
const fixture = parseColumns(fixture1);
t.is(fixture[0].PID, '238');
});
test('headers option', t => {
const fixture = parseColumns(fixture1, {
headers: [
'pid',
'name',
'cmd',
],
});
t.is(fixture[0].pid, '238');
t.truthy(fixture[0].name);
t.truthy(fixture[0].cmd);
});
test('transform option', t => {
const fixture = parseColumns(fixture1, {
transform: (item, header, rowIndex, columnIndex) => {
t.is(typeof rowIndex, 'number');
t.is(typeof columnIndex, 'number');
return header === 'PID' ? Number(item) : item;
},
});
t.is(fixture[0].PID, 238);
});
test('separator option', t => {
const fixture = parseColumns(fixture2, {
separator: '|',
});
t.is(fixture[0].PID, '238');
});
test('differing line lengths', t => {
const fixture = parseColumns(fixture3);
const columns = 'COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME'.split(' ');
t.true(fixture.every(row => Object.keys(row).length === columns.length && columns.every(column => Reflect.has(row, column))));
});
test('separators in values', t => {
const fixture = parseColumns(fixture4);
t.deepEqual(fixture, [
{
PID: '5971',
CMD: 'emacs -nw',
STARTED: 'Oct 29',
},
{
PID: '22678',
CMD: 'emacs -nw foo.js',
STARTED: '13:10:36',
},
{
PID: '28752',
CMD: 'emacs -nw .',
STARTED: 'Oct 28',
},
{
PID: '31236',
CMD: 'emacs -nw fixtures/ps-3.out',
STARTED: '17:10:10',
},
{
PID: '32513',
CMD: 'emacs -nw README.md',
STARTED: 'Oct 28',
},
]);
});
test('handles `df` output', t => {
const data = parseColumns(`
Filesystem Type 1024-blocks Used Available Capacity Mounted on
xx.xxx.xxx.xx:/xxxxxxxxxxx nfs 198640150528 43008 198640107520 1% /run/xo-server/mounts/cbb36e4c-3353-4126-8588-18ba25697403
`);
t.deepEqual(data, [
{
Filesystem: 'xx.xxx.xxx.xx:/xxxxxxxxxxx',
Type: 'nfs',
'1024-blocks': '198640150528',
Used: '43008',
Available: '198640107520',
Capacity: '1%',
'Mounted on': '/run/xo-server/mounts/cbb36e4c-3353-4126-8588-18ba25697403',
},
]);
});
test('handles `df` output with spaces', t => {
const data = parseColumns(`
Filesystem Type 1024-blocks Used Available Capacity Mounted on
/dev/sda1 2 3 4 5 999 ext4 243617788 137765660 105852128 57% /media/foo1 2 3 4 5 999
`);
t.deepEqual(data, [
{
Filesystem: '/dev/sda1 2 3 4 5 999',
Type: 'ext4',
'1024-blocks': '243617788',
Used: '137765660',
Available: '105852128',
Capacity: '57%',
'Mounted on': '/media/foo1 2 3 4 5 999',
},
]);
});
test.failing('handles `df` output with spaces and `headers` option', t => {
const data = parseColumns(`
Filesystem Type 1024-blocks Used Available Capacity Mounted on
/dev/sda1 2 3 4 5 999 ext4 243617788 137765660 105852128 57% /media/foo1 2 3 4 5 999
`, {
headers: [
'filesystem',
'type',
'size',
'used',
'available',
'capacity',
'mountpoint',
],
});
t.deepEqual(data, [
{
filesystem: '/dev/sda1 2 3 4 5 999',
type: 'ext4',
size: '243617788',
used: '137765660',
available: '105852128',
capacity: '57%',
mountpoint: '/media/foo1 2 3 4 5 999',
},
]);
});