UNPKG

1.86 kBJavaScriptView Raw
1'use strict'
2
3const assert = require('assert')
4const fs = require('fs')
5const makePromise = require('makepromise')
6const wrote = require('../../src/index')
7const WroteContext = require('../context/WroteContext')
8
9function assertFileExists(filepath) {
10 return makePromise(fs.stat, filepath)
11}
12
13const eraseTestSuite = {
14 context: WroteContext,
15 'should erase passed file': (ctx) => {
16 const file = ctx.tempFile
17 return wrote(file)
18 .then((ws) => {
19 return wrote.erase(ws)
20 })
21 .then((ws) => {
22 assert(ws.closed) // if node 6+, assert writable == false
23 assert.equal(ws.path, file)
24 })
25 .then(() => ctx.assertFileDoesNotExist(file))
26 },
27 'should erase temp file': (ctx) => {
28 let file
29 let writeStream
30
31 return wrote()
32 .then((ws) => {
33 writeStream = ws
34 file = writeStream.path
35 return wrote.erase(writeStream)
36 })
37 .then(() => {
38 assert(writeStream.closed)
39 assert.equal(writeStream.path, file)
40 })
41 .then(() => ctx.assertFileDoesNotExist(file))
42 },
43 'should erase file even if stream is closed': (ctx) => {
44 const file = ctx.tempFile
45 let writeStream
46 return wrote(file)
47 .then((ws) => {
48 writeStream = ws
49 return makePromise(writeStream.end.bind(writeStream))
50 })
51 .then(() => {
52 assert(writeStream.closed)
53 assert.equal(writeStream.path, file)
54 })
55 .then(() => assertFileExists(file))
56 .then(() => wrote.erase(writeStream))
57 .then(() => ctx.assertFileDoesNotExist(file))
58 },
59}
60
61module.exports = eraseTestSuite