-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
170 lines (152 loc) · 4.79 KB
/
index.ts
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
import * as fs from "node:fs";
import * as path from "node:path";
import { glob } from "glob";
import { cases } from "./data";
import {
WASMagic,
WASMagicFlags,
type StdioOverrideFunction,
} from "../../index";
describe("WASMagic", () => {
describe("Default magic file", () => {
let fileExampleList: string[];
let magic: WASMagic;
let magicText: WASMagic;
beforeAll(async () => {
const xbyxRegexp = /[0-9]+x[0-9]+/;
fileExampleList = (
await glob(path.join(__dirname, "../../../vendor/file-examples/**"), {
nodir: true,
})
)
.sort()
.filter((f) => !xbyxRegexp.test(f));
magic = await WASMagic.create();
magicText = await WASMagic.create({
flags: WASMagicFlags.NONE,
});
});
// Dummy test to create test table output
test.skip("Create Table", () => {
const out: string[][] = [];
for (const file of fileExampleList) {
const fileBuf = fs.readFileSync(file);
const mimeType = magic.detect(fileBuf);
const textType = magicText.detect(fileBuf);
out.push([
path.relative(path.resolve(__dirname, "../../../"), file),
mimeType,
textType,
]);
}
console.log(JSON.stringify(out, undefined, 2));
});
test.each(cases)(
"%s identified as %s",
(file, expectedOutput, expectedText) => {
const fileBuf = fs.readFileSync(file);
expect(magic.detect(fileBuf)).toBe(expectedOutput);
expect(magicText.detect(fileBuf)).toBe(expectedText);
},
);
test("WASMagic throws when it fails to load", async () => {
let err: Error | undefined;
try {
await WASMagic.create({
loadDefaultMagicfile: false,
magicFiles: [
// To force a loading failure
Buffer.from("FOOOOOOOBAR"),
],
});
} catch (_err) {
err = _err as Error;
}
expect(err).toBeInstanceOf(Error);
expect(err?.message.startsWith("WASMagic Load Error: ")).toBe(true);
});
});
describe("Specified foobar custom magic file, with default magic file", () => {
let magic: WASMagic;
beforeAll(async () => {
magic = await WASMagic.create({
magicFiles: [fs.readFileSync(path.join(__dirname, "foobar_magic"))],
loadDefaultMagicfile: true,
});
});
test("FOOBAR file type identified as foobarfiletype", () => {
expect(
magic.detect(
Buffer.from(
`FOOBARFILETYPE
Some made up stuff
`,
),
),
).toBe("foobarfiletype");
});
const pngOrJpeg = ["image/png", "image/jpeg"];
test.each(cases.filter((v) => pngOrJpeg.includes(v[1])))(
"%s identified as %s",
(file, expectedOutput) => {
expect(magic.detect(fs.readFileSync(file))).toBe(expectedOutput);
},
);
});
describe("Specified png and jpeg magic files, no default magic file", () => {
let magic: WASMagic;
beforeAll(async () => {
magic = await WASMagic.create({
magicFiles: [
fs.readFileSync(path.resolve(__dirname, "png_magic")),
fs.readFileSync(path.resolve(__dirname, "jpeg_magic")),
],
loadDefaultMagicfile: false,
});
});
const pngOrJpeg = ["image/png", "image/jpeg"];
test.each(cases.filter((v) => pngOrJpeg.includes(v[1])))(
"%s identified as %s",
(file, expectedOutput) => {
expect(magic.detect(fs.readFileSync(file))).toBe(expectedOutput);
},
);
const rtfOrHtml = ["text/rtf", "text/html"];
test.each(cases.filter((v) => rtfOrHtml.includes(v[1])))(
"%s identified as text/plain",
(file) => {
expect(magic.detect(fs.readFileSync(file))).toBe("text/plain");
},
);
});
describe("StdioOverrideFunction", () => {
describe("StdioOverrideFunction fires when WASMagic fails to load", () => {
let stdioType: string | undefined;
let text: string | undefined;
const stdio: StdioOverrideFunction = (_stdioType, _text) => {
stdioType = _stdioType;
text = _text;
};
beforeAll(async () => {
try {
await WASMagic.create({
loadDefaultMagicfile: false,
magicFiles: [
// To force a loading failure
Buffer.from("FOOOOOOOBAR"),
],
stdio,
});
} catch (_err) {
// Ignoring _err
}
});
test("stdioType is a String", () =>
expect(typeof stdioType).toBe("string"));
test("stdioType is stderr", () => expect(stdioType).toBe("stderr"));
test("text is a String", () => expect(typeof text).toBe("string"));
test("text is a known error", () =>
expect(text).toBe("0, 1: Warning: offset `FOOOOOOOBAR' invalid"));
});
});
});