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) {
|
2020-08-19 05:36:46 +03:00
|
|
|
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) => {
|
2021-04-11 01:09:09 +03:00
|
|
|
jest.setTimeout(1200000)
|
2020-03-07 19:40:24 +03:00
|
|
|
jest.mock('helpers/non-webpack-require', () => {
|
2020-08-19 05:36:46 +03:00
|
|
|
return (path) => {
|
2020-03-07 19:40:24 +03:00
|
|
|
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-08-19 05:36:46 +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
|
2020-08-19 05:36:46 +03:00
|
|
|
if (!Object.values(responses).some((c) => c === null)) {
|
2020-03-30 05:41:45 +03:00
|
|
|
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 = ''
|
2020-08-19 05:36:46 +03:00
|
|
|
req.on('data', (chunk) => {
|
2020-03-30 05:41:45 +03:00
|
|
|
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
|
|
|
}
|