-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
180 lines (138 loc) · 3.74 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
import {Buffer} from 'node:buffer';
import test from 'ava';
import Vinyl from 'vinyl';
import {pEvent} from 'p-event';
import chmod from './index.js';
test('should throw if invalid argument type', t => {
t.throws(() => {
chmod('bad argument');
}, {
message: /Expected `fileMode` to be/,
});
});
test('should chmod files using a number', async t => {
const stream = chmod(0o755);
stream.end(new Vinyl({
stat: {mode: 0o10_0644},
contents: Buffer.from(''),
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode.toString(8), '755');
});
test('should chmod files using an object', async t => {
const stream = chmod({
owner: {read: true, write: true, execute: true},
group: {execute: true},
others: {execute: true},
});
stream.end(new Vinyl({
stat: {mode: 0o10_0644},
contents: Buffer.from(''),
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode & 0o0_7777, 0o755); // eslint-disable-line no-bitwise
});
test('should chmod files using a simple object', async t => {
const stream = chmod({read: false});
stream.end(new Vinyl({
stat: {mode: 0o10_0644},
contents: Buffer.from(''),
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode & 0o0_7777, 0o200); // eslint-disable-line no-bitwise
});
test('should not change folder permissions without directoryMode value', async t => {
const stream = chmod(0o755);
stream.end(new Vinyl({
stat: {
mode: 0o10_0644,
isDirectory: () => true,
},
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode, 0o10_0644);
});
test('should use mode for directories when directoryMode set to true', async t => {
const stream = chmod(0o755, true);
stream.end(new Vinyl({
stat: {
mode: 0o10_0644,
isDirectory: () => true,
},
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode, 0o755);
});
test('should throw if invalid directoryMode argument type', t => {
t.throws(() => {
chmod(undefined, 'bad argument');
}, {
message: /Expected `directoryMode` to be/,
});
});
test('should chmod directories using a number', async t => {
const stream = chmod(undefined, 0o755);
stream.end(new Vinyl({
stat: {
mode: 0o10_0644,
isDirectory: () => true,
},
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode, 0o755);
});
test('should chmod directories using an object', async t => {
const stream = chmod(undefined, {
owner: {read: true, write: true, execute: true},
group: {execute: true},
others: {execute: true},
});
stream.end(new Vinyl({
stat: {
mode: 0o10_0644,
isDirectory: () => true,
},
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode & 0o0_7777, 0o755); // eslint-disable-line no-bitwise
});
test('should handle no stat object', async t => {
const stream = chmod(0o755);
stream.end(new Vinyl({contents: Buffer.from('')}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode, 0o755);
});
test('should use defaultMode if no mode on state object', async t => {
const stream = chmod(0o755);
stream.end(new Vinyl({
stat: {},
contents: Buffer.from(''),
}));
const file = await pEvent(stream, 'data');
t.is(file.stat.mode, 0o755);
});
test('should handle different values for mode and directoryMode', async t => {
t.plan(2);
const stream = chmod(0o755, 0o777);
let checkedDirectory = false;
let checkedFile = false;
stream.write(new Vinyl({
contents: Buffer.from(''),
}));
stream.write(new Vinyl({
stat: {isDirectory: () => true},
}));
stream.on('data', file => {
if (file.isDirectory()) {
t.is(file.stat.mode, 0o777);
checkedDirectory = true;
} else {
t.is(file.stat.mode, 0o755);
checkedFile = true;
}
if (checkedDirectory && checkedFile) {
stream.end();
}
});
await pEvent(stream, 'end');
});