forked from sindresorhus/file-icon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
220 lines (167 loc) · 8.01 KB
/
Copy pathtest.js
File metadata and controls
220 lines (167 loc) · 8.01 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
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
217
218
219
220
import fs from 'node:fs';
import test from 'ava';
import FileType from 'file-type';
import tempy from 'tempy';
import execa from 'execa';
import {fileIconToBuffer, fileIconToFile} from './index.js';
const processID = async app => {
const {stdout} = await execa('pgrep', [app]);
return Number.parseInt(stdout.split('\n')[0], 10);
};
const temporaryFile = () => tempy.file({extension: 'png'});
test('argument validation', async t => {
const finderPID = await processID('Finder');
await t.notThrowsAsync(fileIconToBuffer('Safari'));
await t.notThrowsAsync(fileIconToBuffer(finderPID));
await t.notThrowsAsync(fileIconToBuffer(['Safari', 'Finder']));
await t.notThrowsAsync(fileIconToBuffer(['Safari', finderPID]));
await t.throwsAsync(fileIconToBuffer({}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToBuffer([{}]), {instanceOf: TypeError});
await t.throwsAsync(fileIconToBuffer(['Safari', {}]), {instanceOf: TypeError});
await t.notThrowsAsync(fileIconToFile('Safari', {destination: temporaryFile()}));
await t.notThrowsAsync(fileIconToFile(finderPID, {destination: temporaryFile()}));
await t.notThrowsAsync(fileIconToFile(['Safari', 'Finder'], {destination: [temporaryFile(), temporaryFile()]}));
await t.notThrowsAsync(fileIconToFile(['Safari', finderPID], {destination: [temporaryFile(), temporaryFile()]}));
await t.throwsAsync(fileIconToFile(['Safari']), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari'], {destination: temporaryFile()}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari', 'Finder'], {destination: [temporaryFile()]}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari', 'Finder'], {destination: [temporaryFile(), temporaryFile(), temporaryFile()]}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile({}, {destination: temporaryFile()}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile([{}], {destination: [temporaryFile()]}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari', {}], {destination: [temporaryFile(), temporaryFile()]}), {instanceOf: TypeError});
});
test('app name', async t => {
t.is((await FileType.fromBuffer(await fileIconToBuffer('Safari'))).ext, 'png');
});
test('array of single app name', async t => {
const apps = ['Finder'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 1);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
});
test('array of multiple app names', async t => {
const apps = ['Finder', 'Safari'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 2);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
t.is((await FileType.fromBuffer(buffers[1])).ext, 'png');
});
test('app bundle ID', async t => {
t.is((await FileType.fromBuffer(await fileIconToBuffer('com.apple.Safari'))).ext, 'png');
});
test('array of single app bundle ID', async t => {
const apps = ['com.apple.Finder'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 1);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
});
test('array of multiple app bundle IDs', async t => {
const ids = ['com.apple.Finder', 'com.apple.Safari'];
const buffers = await fileIconToBuffer(ids);
t.is(buffers.length, 2);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
t.is((await FileType.fromBuffer(buffers[1])).ext, 'png');
});
test('app process ID', async t => {
const finderPID = await processID('Finder');
t.is((await FileType.fromBuffer(await fileIconToBuffer(finderPID))).ext, 'png');
});
test('array of single app process ID', async t => {
const finderPID = await processID('Finder');
const pids = [finderPID];
const buffers = await fileIconToBuffer(pids);
t.is(buffers.length, 1);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
});
test('array of multiple app process IDs', async t => {
const apps = ['Finder', 'loginwindow'];
const pids = await Promise.all(apps.map(app => processID(app)));
const buffers = await fileIconToBuffer(pids);
t.is(buffers.length, 2);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
t.is((await FileType.fromBuffer(buffers[1])).ext, 'png');
});
test('array of mixed types', async t => {
const loginwindowPID = await processID('loginwindow');
const apps = ['com.apple.Finder', loginwindowPID, 'Safari'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 3);
t.is((await FileType.fromBuffer(buffers[0])).ext, 'png');
t.is((await FileType.fromBuffer(buffers[1])).ext, 'png');
t.is((await FileType.fromBuffer(buffers[2])).ext, 'png');
});
test('file path', async t => {
t.is((await FileType.fromBuffer(await fileIconToBuffer('/Applications/Safari.app'))).ext, 'png');
});
test('write file single app name', async t => {
const destination = temporaryFile();
await fileIconToFile('Safari', {destination});
const icon = fs.readFileSync(destination);
t.is((await FileType.fromBuffer(icon)).ext, 'png');
});
test('write file array of single app name', async t => {
const destination = [temporaryFile()];
await fileIconToFile(['Safari'], {destination});
t.is(destination.length, 1);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
});
test('write files array of app names', async t => {
const destination = [temporaryFile(), temporaryFile()];
await fileIconToFile(['Safari', 'Finder'], {destination});
t.is(destination.length, 2);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
t.is((await FileType.fromBuffer(fs.readFileSync(destination[1]))).ext, 'png');
});
test('write file single app bundle ID', async t => {
const destination = temporaryFile();
await fileIconToFile('com.apple.Safari', {destination});
const icon = fs.readFileSync(destination);
t.is((await FileType.fromBuffer(icon)).ext, 'png');
});
test('write file array of single app bundle ID', async t => {
const destination = [temporaryFile()];
await fileIconToFile(['com.apple.Safari'], {destination});
t.is(destination.length, 1);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
});
test('write files array of app bundle IDs', async t => {
const destination = [temporaryFile(), temporaryFile()];
await fileIconToFile(['com.apple.Safari', 'com.apple.Finder'], {destination});
t.is(destination.length, 2);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
t.is((await FileType.fromBuffer(fs.readFileSync(destination[1]))).ext, 'png');
});
test('write file single app process ID', async t => {
const destination = temporaryFile();
const pid = await processID('Finder');
await fileIconToFile(pid, {destination});
const icon = fs.readFileSync(destination);
t.is((await FileType.fromBuffer(icon)).ext, 'png');
});
test('write file array of single app process ID', async t => {
const apps = ['Finder'];
const destination = [temporaryFile()];
const pids = await Promise.all(apps.map(app => processID(app)));
await fileIconToFile(pids, {destination});
t.is(destination.length, 1);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
});
test('write files array of app process IDs', async t => {
const apps = ['Finder', 'loginwindow'];
const destination = [temporaryFile(), temporaryFile()];
const pids = await Promise.all(apps.map(app => processID(app)));
await fileIconToFile(pids, {destination});
t.is(destination.length, 2);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
t.is((await FileType.fromBuffer(fs.readFileSync(destination[1]))).ext, 'png');
});
test('write files array of mixed types', async t => {
const loginwindowPID = await processID('loginwindow');
const apps = ['com.apple.Finder', loginwindowPID, 'Safari'];
const destination = [temporaryFile(), temporaryFile(), temporaryFile()];
await fileIconToFile(apps, {destination});
t.is(destination.length, 3);
t.is((await FileType.fromBuffer(fs.readFileSync(destination[0]))).ext, 'png');
t.is((await FileType.fromBuffer(fs.readFileSync(destination[1]))).ext, 'png');
t.is((await FileType.fromBuffer(fs.readFileSync(destination[2]))).ext, 'png');
});