2020-03-07 19:40:24 +03:00
|
|
|
const path = require('path')
|
|
|
|
const fixtureSetup = require('../fixtures/app-test-setup')
|
2020-03-09 23:57:27 +03:00
|
|
|
const distDir = path.resolve(fixtureSetup.fixtureDir, 'app', 'dist')
|
2020-03-07 19:40:24 +03:00
|
|
|
|
|
|
|
function startDevServer() {
|
2020-08-19 05:36:46 +03:00
|
|
|
const http = require('http')
|
|
|
|
const { statSync, createReadStream } = require('fs')
|
|
|
|
const app = http.createServer((req, res) => {
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
if (req.url === '/') {
|
|
|
|
const indexPath = path.join(distDir, 'index.html')
|
|
|
|
const stat = statSync(indexPath)
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'text/html',
|
|
|
|
'Content-Length': stat.size
|
|
|
|
})
|
|
|
|
createReadStream(indexPath).pipe(res)
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
2020-08-19 05:36:46 +03:00
|
|
|
}
|
|
|
|
})
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2020-08-19 05:36:46 +03:00
|
|
|
const port = 7001
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2020-08-19 05:36:46 +03:00
|
|
|
const server = app.listen(port)
|
|
|
|
return {
|
|
|
|
server,
|
|
|
|
url: `http://localhost:${port}`
|
|
|
|
}
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function runDevTest(tauriConfig) {
|
2020-03-09 23:57:27 +03:00
|
|
|
fixtureSetup.initJest('app')
|
2021-01-30 18:15:47 +03:00
|
|
|
const { dev } = require('dist/api/cli')
|
2020-03-07 19:40:24 +03:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
2021-01-30 18:15:47 +03:00
|
|
|
process.chdir(path.join(fixtureSetup.fixtureDir, 'app'))
|
|
|
|
const { promise, pid } = dev({ config: tauriConfig })
|
2020-03-07 19:40:24 +03:00
|
|
|
|
|
|
|
const isRunning = require('is-running')
|
|
|
|
let success = false
|
|
|
|
const checkIntervalId = setInterval(async () => {
|
2021-01-30 18:15:47 +03:00
|
|
|
if (!isRunning(pid) && !success) {
|
2020-08-19 05:36:46 +03:00
|
|
|
const failedCommands = Object.keys(responses)
|
|
|
|
.filter((k) => responses[k] === null)
|
|
|
|
.join(', ')
|
2020-05-05 15:10:39 +03:00
|
|
|
server.close(() => reject("App didn't reply to " + failedCommands))
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
|
|
|
}, 2000)
|
|
|
|
|
2020-05-05 15:10:39 +03:00
|
|
|
const { server, responses } = fixtureSetup.startServer(async () => {
|
2020-03-07 19:40:24 +03:00
|
|
|
success = true
|
|
|
|
clearInterval(checkIntervalId)
|
|
|
|
// wait for the app process to be killed
|
|
|
|
setTimeout(async () => {
|
|
|
|
try {
|
2021-01-30 18:15:47 +03:00
|
|
|
process.kill(pid)
|
2020-03-07 19:40:24 +03:00
|
|
|
} catch {}
|
|
|
|
resolve()
|
|
|
|
}, 2000)
|
|
|
|
})
|
|
|
|
await promise
|
|
|
|
} catch (error) {
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Tauri Dev', () => {
|
|
|
|
const build = {
|
2020-10-18 02:53:24 +03:00
|
|
|
distDir: distDir,
|
|
|
|
withGlobalTauri: true
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const devServer = startDevServer()
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
url
|
|
|
|
${devServer.url}
|
|
|
|
${distDir}
|
|
|
|
`('works with dev pointing to $url', ({ url }) => {
|
|
|
|
const runningDevServer = url.startsWith('http')
|
|
|
|
const promise = runDevTest({
|
|
|
|
build: {
|
|
|
|
...build,
|
|
|
|
devPath: url
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
promise.then(() => {
|
|
|
|
if (runningDevServer) {
|
|
|
|
devServer.server.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return promise
|
|
|
|
})
|
|
|
|
})
|