forked from sindresorhus/tempy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
119 lines (102 loc) · 3.18 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
import path from 'path';
import tempDir from 'temp-dir';
import pathExists from 'path-exists';
import touch from 'touch';
import fs from 'fs';
import stream from 'stream';
import test from 'ava';
import tempy from '.';
test('.file()', t => {
t.true(tempy.file().includes(tempDir));
t.false(tempy.file().endsWith('.'));
t.false(tempy.file({extension: undefined}).endsWith('.'));
t.false(tempy.file({extension: null}).endsWith('.'));
t.true(tempy.file({extension: 'png'}).endsWith('.png'));
t.true(tempy.file({extension: '.png'}).endsWith('.png'));
t.false(tempy.file({extension: '.png'}).endsWith('..png'));
t.true(tempy.file({name: 'custom-name.md'}).endsWith('custom-name.md'));
t.throws(() => {
tempy.file({name: 'custom-name.md', extension: '.ext'});
});
t.throws(() => {
tempy.file({name: 'custom-name.md', extension: ''});
});
t.notThrows(() => {
tempy.file({name: 'custom-name.md', extension: undefined});
});
t.notThrows(() => {
tempy.file({name: 'custom-name.md', extension: null});
});
});
test('.file.task()', async t => {
let temporaryFilePath;
t.is(await tempy.file.task(async temporaryFile => {
await touch(temporaryFile);
temporaryFilePath = temporaryFile;
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});
test('.directory()', t => {
const prefix = 'name_';
t.true(tempy.directory().includes(tempDir));
t.true(path.basename(tempy.directory({prefix})).startsWith(prefix));
});
test('.directory.task()', async t => {
let temporaryDirectoryPath;
t.is(await tempy.directory.task(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
return temporaryDirectory;
}), temporaryDirectoryPath);
t.false(await pathExists(temporaryDirectoryPath));
});
test('.write(string)', async t => {
const filePath = await tempy.write('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 tempy.write.task('', async temporaryFile => {
temporaryFilePath = temporaryFile;
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});
test('.write(buffer)', async t => {
const filePath = await tempy.write(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);
const filePath = await tempy.write(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(() => tempy.write(readable), {
instanceOf: Error,
message: 'Catch me if you can!'
});
});
test('.writeSync()', t => {
t.is(fs.readFileSync(tempy.writeSync('unicorn'), 'utf8'), 'unicorn');
});
test('.root', t => {
t.true(tempy.root.length > 0);
t.true(path.isAbsolute(tempy.root));
t.throws(() => {
tempy.root = 'foo';
});
});