UNPKG

6.59 kBJavaScriptView Raw
1const assert = require('assert')
2const Catchment = require('catchment')
3const fs = require('fs')
4const makePromise = require('makepromise')
5const { tmpdir } = require('os')
6const { resolve } = require('path')
7const spawnCommand = require('spawncommand')
8const { nodeGte } = require('noddy')
9const {
10 createWritable, write, erase, readDir, readDirStructure, read,
11} = require('../../src/')
12const fixturesStructure = require('../fixtures/expected/read-dir-structure')
13const expectedJSON = require('../fixtures/test.json')
14
15const FIXTURES_DIR = resolve(__dirname, '../fixtures/')
16const FIXTURES_TEST_DIR = resolve(FIXTURES_DIR, 'directory')
17const FIXTURES_TEST_DIR_SOFT_LINK = resolve(FIXTURES_DIR, 'directory-ln')
18
19const TEST_NAME = `wrote-test-${Math.floor(Math.random() * 1e5)}.data`
20
21const createTempFilePath = () => resolve(tmpdir(), TEST_NAME)
22
23async function assertFileDoesNotExist(path) {
24 try {
25 await makePromise(fs.stat, path)
26 throw new Error('should have been rejected')
27 } catch ({ message }) {
28 assert(/^ENOENT: no such file or directory/.test(message))
29 }
30}
31
32async function assertFileExists(path) {
33 await makePromise(fs.stat, path)
34}
35
36async function assertCanWriteFile(path) {
37 const testData = `some-test-data-${Date.now()}`
38 const ws = await createWritable(path)
39 await write(ws, testData)
40 const rs = fs.createReadStream(path)
41
42 const { promise } = new Catchment({ rs })
43 const res = await promise
44 assert.equal(res, testData)
45}
46
47const TEMP_DIR = resolve(__dirname, '../temp')
48const TEST_DIR_NAME = '_tests'
49const TEST_DIR_NOX_NAME = 'no-execute'
50const TEMP_TEST_DIR = resolve(TEMP_DIR, TEST_DIR_NAME)
51const TEMP_NOX_DIR = resolve(TEMP_DIR, TEST_DIR_NOX_NAME)
52
53async function WroteContext() {
54 this.read = read
55 this.expectedJSON = expectedJSON
56 this.JSONpath = resolve(FIXTURES_DIR, 'test.json')
57 this.invalidJSONpath = resolve(FIXTURES_DIR, 'invalid.json')
58
59 Object.assign(this, {
60 TEST_NAME,
61 TEST_DATA: 'some test data for temp file',
62 })
63 this.TEST_DATA_BUFFER = nodeGte('v5.10.0')
64 ? Buffer.from(this.TEST_DATA)
65 : new Buffer(this.TEST_DATA)
66
67 let tempFileWs
68 Object.defineProperties(this, {
69 readDir: {
70 async value(...args) {
71 const res = await readDir(...args)
72 return res
73 },
74 },
75 readDirStructure: {
76 async value(...args) {
77 const res = await readDirStructure(...args)
78 return res
79 },
80 },
81 readFixturesStructure: {
82 async value() {
83 const res = await readDirStructure(FIXTURES_TEST_DIR)
84 return res
85 },
86 },
87 readTempStructure: {
88 async value() {
89 const res = await readDirStructure(TEMP_TEST_DIR)
90 return res
91 },
92 },
93 readTemp: {
94 async value() {
95 const res = await readDir(TEMP_TEST_DIR, true)
96 return res
97 },
98 },
99 readFixtures: {
100 async value() {
101 const res = await readDir(FIXTURES_TEST_DIR, true)
102 return res
103 },
104 },
105 tempFile: {
106 get() {
107 return this._tempFile || createTempFilePath()
108 },
109 },
110 expectedFixturesStructure: {
111 get() { return fixturesStructure },
112 },
113 createTempFileWithData: { value: async () => {
114 const tempFile = createTempFilePath()
115 const ws = await createWritable(tempFile)
116 tempFileWs = ws
117 await write(ws, this.TEST_DATA)
118 this._tempFile = tempFile
119 }},
120 assertFileDoesNotExist: {
121 get: () => assertFileDoesNotExist,
122 },
123 assertFileExists: {
124 get: () => assertFileExists,
125 },
126 assertCanWriteFile: {
127 get: () => assertCanWriteFile,
128 },
129 TEMP_DIR: {
130 get: () => TEMP_DIR,
131 },
132 FIXTURES_TEST_DIR: {
133 get: () => FIXTURES_TEST_DIR,
134 },
135 FIXTURES_TEST_DIR_SOFT_LINK: {
136 get: () => FIXTURES_TEST_DIR_SOFT_LINK,
137 },
138 READ_DIR: {
139 get: () => TEMP_DIR,
140 },
141 TEMP_TEST_DIR: {
142 get: () => {
143 if (!this._TEMP_TEST_DIR) {
144 throw new Error('Temp dir was not created')
145 }
146 return this._TEMP_TEST_DIR
147 },
148 },
149 TEMP_NOX_DIR: {
150 get: () => {
151 if (!this._TEMP_NOX_DIR) {
152 throw new Error('Call makeNoExecutableDirectory to access this property first')
153 }
154 return this._TEMP_NOX_DIR
155 },
156 },
157 makeNoExecutableDirectory: { value: async () => {
158 if (this._TEMP_NOX_DIR) {
159 throw new Error('No executable directory already created')
160 }
161 try {
162 await makePromise(fs.mkdir, [TEMP_NOX_DIR, 0o666])
163 this._TEMP_NOX_DIR = TEMP_NOX_DIR
164 return TEMP_NOX_DIR
165 } catch ({ message }) {
166 if (/EEXIST/.test(message)) {
167 throw new Error('WroteContext: Could not make no executable directory: it already exists')
168 }
169 }
170 }},
171 _destroy: { value: async () => {
172 const promises = []
173 if (this._TEMP_TEST_DIR && !process.env.KEEP_TEMP) {
174 const pc = spawnCommand('rm', ['-rf', this._TEMP_TEST_DIR])
175 promises.push(pc.promise)
176 }
177 if (this._TEMP_NOX_DIR) {
178 const pc2 = spawnCommand('rm', ['-rf', this._TEMP_NOX_DIR])
179 promises.push(pc2.promise)
180 }
181 if (tempFileWs) {
182 const promise = erase(tempFileWs)
183 promises.push(promise)
184 }
185 // remove temp file
186 await Promise.all(promises)
187 }},
188 })
189
190 // always make temp dir available
191 try {
192 const { promise } = spawnCommand('rm', ['-rf', TEMP_TEST_DIR])
193 await promise
194 await makePromise(fs.mkdir, [TEMP_TEST_DIR, 0o777])
195 this._TEMP_TEST_DIR = TEMP_TEST_DIR
196 } catch (err) {
197 if (/EEXIST/.test(err.message)) {
198 throw new Error('WroteContext: Could not make temp test directory: it already exists.')
199 }
200 throw err
201 }
202}
203
204module.exports = WroteContext