2020-03-07 19:40:24 +03:00
|
|
|
const path = require('path')
|
|
|
|
const process = require('process')
|
|
|
|
|
2020-03-10 03:27:54 +03:00
|
|
|
const mockFixtureDir = path.resolve(__dirname, '../fixtures')
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2020-03-10 03:27:54 +03:00
|
|
|
module.exports.fixtureDir = mockFixtureDir
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2020-07-09 16:40:31 +03:00
|
|
|
function mockResolvePath(basePath, dir) {
|
|
|
|
return dir && path.isAbsolute(dir) ?
|
|
|
|
dir :
|
|
|
|
path.resolve(basePath, dir)
|
2020-03-30 05:41:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-10 03:27:54 +03:00
|
|
|
module.exports.initJest = (mockFixture) => {
|
2020-03-12 19:42:38 +03:00
|
|
|
jest.setTimeout(720000)
|
2020-03-07 19:40:24 +03:00
|
|
|
jest.mock('helpers/non-webpack-require', () => {
|
|
|
|
return path => {
|
|
|
|
const value = require('fs').readFileSync(path).toString()
|
|
|
|
if (path.endsWith('.json')) {
|
|
|
|
return JSON.parse(value)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
})
|
2020-03-10 03:27:54 +03:00
|
|
|
|
|
|
|
jest.mock('helpers/app-paths', () => {
|
|
|
|
const path = require('path')
|
|
|
|
const appDir = path.join(mockFixtureDir, mockFixture)
|
|
|
|
const tauriDir = path.join(appDir, 'src-tauri')
|
|
|
|
return {
|
|
|
|
appDir,
|
|
|
|
tauriDir,
|
|
|
|
resolve: {
|
2020-03-30 05:41:45 +03:00
|
|
|
app: dir => mockResolvePath(appDir, dir),
|
|
|
|
tauri: dir => mockResolvePath(tauriDir, dir)
|
2020-03-10 03:27:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
|
|
|
|
2020-03-30 05:41:45 +03:00
|
|
|
module.exports.startServer = (onSuccess) => {
|
2020-03-07 19:40:24 +03:00
|
|
|
const http = require('http')
|
2020-03-30 05:41:45 +03:00
|
|
|
|
|
|
|
const responses = {
|
|
|
|
writeFile: null,
|
|
|
|
readFile: null,
|
|
|
|
writeFileWithDir: null,
|
|
|
|
readFileWithDir: null,
|
|
|
|
readDir: null,
|
|
|
|
readDirWithDir: null,
|
|
|
|
copyFile: null,
|
|
|
|
copyFileWithDir: null,
|
|
|
|
createDir: null,
|
|
|
|
createDirWithDir: null,
|
|
|
|
removeDir: null,
|
|
|
|
removeDirWithDir: null,
|
|
|
|
renameFile: null,
|
|
|
|
renameFileWithDir: null,
|
|
|
|
removeFile: null,
|
|
|
|
renameFileWithDir: null,
|
|
|
|
listen: null
|
|
|
|
}
|
2020-07-09 16:40:31 +03:00
|
|
|
|
|
|
|
function addResponse(response) {
|
2020-03-30 05:41:45 +03:00
|
|
|
responses[response.cmd] = true
|
|
|
|
if (!Object.values(responses).some(c => c === null)) {
|
|
|
|
server.close(onSuccess)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:40:24 +03:00
|
|
|
const app = http.createServer((req, res) => {
|
|
|
|
// Set CORS headers
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
res.setHeader('Access-Control-Request-Method', '*')
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', '*')
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2020-03-30 05:41:45 +03:00
|
|
|
let body = ''
|
|
|
|
req.on('data', chunk => {
|
|
|
|
body += chunk.toString()
|
|
|
|
})
|
2020-03-07 19:40:24 +03:00
|
|
|
if (req.url === '/reply') {
|
2020-03-30 05:41:45 +03:00
|
|
|
req.on('end', () => {
|
|
|
|
const json = JSON.parse(body)
|
|
|
|
addResponse(json)
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
2020-03-07 19:40:24 +03:00
|
|
|
})
|
2020-03-30 05:41:45 +03:00
|
|
|
}
|
|
|
|
if (req.url === '/error') {
|
2020-03-07 19:40:24 +03:00
|
|
|
req.on('end', () => {
|
2020-03-30 05:41:45 +03:00
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
|
|
|
throw new Error(body)
|
2020-03-07 19:40:24 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const port = 7000
|
|
|
|
const server = app.listen(port)
|
2020-07-09 16:40:31 +03:00
|
|
|
return {
|
|
|
|
server,
|
|
|
|
responses
|
|
|
|
}
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|