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