-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.js
39 lines (32 loc) · 1.21 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
import fs from 'node:fs';
import path from 'node:path';
import stream from 'node:stream';
import test from 'ava';
import {stringToUint8Array} from 'uint8array-extras';
import tempWrite from './index.js';
test('tempWrite(string)', async t => {
const filePath = await tempWrite('unicorn', 'test.png');
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.is(path.basename(filePath), 'test.png');
});
test('tempWrite(data)', async t => {
const filePath = await tempWrite(stringToUint8Array('unicorn'), 'test.png');
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
});
test('tempWrite(data, path)', async t => {
const filePath = await tempWrite(stringToUint8Array('unicorn'), 'foo/bar/test.png');
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.regex(filePath, /foo\/bar\/test\.png$/);
});
test('tempWrite(stream)', async t => {
const readable = new stream.Readable({
read() {}, // Noop
});
readable.push('unicorn');
readable.push(null); // eslint-disable-line unicorn/no-array-push-push
const filePath = await tempWrite(readable, 'test.png');
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
});
test('tempWrite.sync()', t => {
t.is(fs.readFileSync(tempWrite.sync('unicorn'), 'utf8'), 'unicorn');
});