-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.js
142 lines (120 loc) · 3.86 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
import {Buffer} from 'node:buffer';
import path from 'node:path';
import fs from 'node:fs';
import stream from 'node:stream';
import tempDir from 'temp-dir';
import {pathExists} from 'path-exists';
import touch from 'touch';
import test from 'ava';
import {
temporaryFile,
temporaryFileTask,
temporaryDirectory,
temporaryDirectoryTask,
temporaryWrite,
temporaryWriteTask,
temporaryWriteSync,
rootTemporaryDirectory,
} from './index.js';
test('.file()', t => {
t.true(temporaryFile().includes(tempDir));
t.false(temporaryFile().endsWith('.'));
t.false(temporaryFile({extension: undefined}).endsWith('.'));
t.false(temporaryFile({extension: null}).endsWith('.'));
t.true(temporaryFile({extension: 'png'}).endsWith('.png'));
t.true(temporaryFile({extension: '.png'}).endsWith('.png'));
t.false(temporaryFile({extension: '.png'}).endsWith('..png'));
t.true(temporaryFile({name: 'custom-name.md'}).endsWith('custom-name.md'));
t.throws(() => {
temporaryFile({name: 'custom-name.md', extension: '.ext'});
});
t.throws(() => {
temporaryFile({name: 'custom-name.md', extension: ''});
});
t.notThrows(() => {
temporaryFile({name: 'custom-name.md', extension: undefined});
});
t.notThrows(() => {
temporaryFile({name: 'custom-name.md', extension: null});
});
});
test('.file.task()', async t => {
let temporaryFilePath;
t.is(await temporaryFileTask(async temporaryFile => {
await touch(temporaryFile);
temporaryFilePath = temporaryFile;
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});
test('.task() - cleans up even if callback throws', async t => {
let temporaryDirectoryPath;
await t.throwsAsync(temporaryDirectoryTask(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
throw new Error('Catch me if you can!');
}), {
instanceOf: Error,
message: 'Catch me if you can!',
});
t.false(await pathExists(temporaryDirectoryPath));
});
test('.directory()', t => {
const prefix = 'name_';
t.true(temporaryDirectory().includes(tempDir));
t.true(path.basename(temporaryDirectory({prefix})).startsWith(prefix));
});
test('.directory.task()', async t => {
let temporaryDirectoryPath;
t.is(await temporaryDirectoryTask(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
return temporaryDirectory;
}), temporaryDirectoryPath);
t.false(await pathExists(temporaryDirectoryPath));
});
test('.write(string)', async t => {
const filePath = await temporaryWrite('unicorn', {name: 'test.png'});
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.is(path.basename(filePath), 'test.png');
});
test('.write.task(string)', async t => {
let temporaryFilePath;
t.is(await temporaryWriteTask('', async temporaryFile => {
temporaryFilePath = temporaryFile;
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});
test('.write(buffer)', async t => {
const filePath = await temporaryWrite(Buffer.from('unicorn'));
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
});
test('.write(stream)', async t => {
const readable = new stream.Readable({
read() {},
});
readable.push('unicorn');
readable.push(null); // eslint-disable-line unicorn/no-array-push-push
const filePath = await temporaryWrite(readable);
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
});
test('.write(stream) failing stream', async t => {
const readable = new stream.Readable({
read() {},
});
readable.push('unicorn');
setImmediate(() => {
readable.emit('error', new Error('Catch me if you can!'));
readable.push(null);
});
await t.throwsAsync(temporaryWrite(readable), {
instanceOf: Error,
message: 'Catch me if you can!',
});
});
test('.writeSync()', t => {
t.is(fs.readFileSync(temporaryWriteSync('unicorn'), 'utf8'), 'unicorn');
});
test('.root', t => {
t.true(rootTemporaryDirectory.length > 0);
t.true(path.isAbsolute(rootTemporaryDirectory));
});