From 7e2854007aaa3903e8f967282fe7659d03bd58a8 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sun, 29 Mar 2020 23:41:45 -0300 Subject: [PATCH] test(e2e) add FS API tests (#521) * fix(tauri.js) update e2e test * test(e2e) add FS API tests * fix(tauri.js) lint errors * fix(tauri) clippy checks * fix(test) use " instead of ' --- cli/tauri.js/bin/tauri-build.js | 18 +++-- cli/tauri.js/bin/tauri-dev.js | 16 ++-- cli/tauri.js/jest.config.js | 3 - cli/tauri.js/package.json | 4 +- cli/tauri.js/src/helpers/app-paths.ts | 10 ++- cli/tauri.js/src/helpers/tauri-config.ts | 13 ++- .../test/jest/__tests__/build.spec.js | 5 +- .../test/jest/__tests__/template.spec.js | 2 +- .../test/jest/fixtures/app-test-setup.js | 62 +++++++++++--- .../test/jest/fixtures/app/dist/index.html | 80 +++++++++++++++++-- cli/tauri.js/test/jest/fixtures/app/index.js | 54 ------------- .../jest/fixtures/app/src-tauri/src/main.rs | 9 ++- tauri/src/endpoints/dialog.rs | 4 +- 13 files changed, 179 insertions(+), 101 deletions(-) delete mode 100644 cli/tauri.js/test/jest/fixtures/app/index.js diff --git a/cli/tauri.js/bin/tauri-build.js b/cli/tauri.js/bin/tauri-build.js index 0d83d4787..96163a35a 100644 --- a/cli/tauri.js/bin/tauri-build.js +++ b/cli/tauri.js/bin/tauri-build.js @@ -23,11 +23,15 @@ if (argv.help) { process.exit(0) } -const build = require('../dist/api/build') +async function run () { + const build = require('../dist/api/build') -build({ - ctx: { - debug: argv.debug, - target: argv.target - } -}) + await build({ + ctx: { + debug: argv.debug, + target: argv.target + } + }).promise +} + +run() diff --git a/cli/tauri.js/bin/tauri-dev.js b/cli/tauri.js/bin/tauri-dev.js index 3a8cfe126..b350da305 100644 --- a/cli/tauri.js/bin/tauri-dev.js +++ b/cli/tauri.js/bin/tauri-dev.js @@ -20,10 +20,14 @@ if (argv.help) { process.exit(0) } -const dev = require('../dist/api/dev') +async function run () { + const dev = require('../dist/api/dev') -dev({ - ctx: { - exitOnPanic: argv['exit-on-panic'] - } -}) + await dev({ + ctx: { + exitOnPanic: argv['exit-on-panic'] + } + }).promise +} + +run() diff --git a/cli/tauri.js/jest.config.js b/cli/tauri.js/jest.config.js index 495e8c369..0c2d8f085 100644 --- a/cli/tauri.js/jest.config.js +++ b/cli/tauri.js/jest.config.js @@ -28,9 +28,6 @@ module.exports = { '/test/jest/__tests__/**/*.spec.js', '/test/jest/__tests__/**/*.test.js' ], - testPathIgnorePatterns: [ - '(build|dev).spec.js' - ], moduleFileExtensions: ['ts', 'js', 'json'], moduleNameMapper: { '^~/(.*)$': '/$1', diff --git a/cli/tauri.js/package.json b/cli/tauri.js/package.json index d8775b450..0d3c0e779 100644 --- a/cli/tauri.js/package.json +++ b/cli/tauri.js/package.json @@ -12,10 +12,10 @@ "scripts": { "build": "webpack --progress", "build-release": "yarn build --display none --progress false", - "test": "jest --runInBand --no-cache", + "test": "jest --runInBand --no-cache --testPathIgnorePatterns=\"(build|dev)\"", "pretest": "yarn build", "prepublishOnly": "yarn build-release", - "test:mac-local": "jest --runInBand", + "test:local": "jest --runInBand", "lint": "eslint --ext ts ./src/**/*.ts", "lint-fix": "eslint --fix --ext ts ./src/**/*.ts", "lint:lockfile": "lockfile-lint --path yarn.lock --type yarn --validate-https --allowed-hosts npm yarn", diff --git a/cli/tauri.js/src/helpers/app-paths.ts b/cli/tauri.js/src/helpers/app-paths.ts index 080fc0a0a..da117ddbc 100644 --- a/cli/tauri.js/src/helpers/app-paths.ts +++ b/cli/tauri.js/src/helpers/app-paths.ts @@ -3,6 +3,12 @@ import { join, normalize, resolve, sep } from 'path' import logger from './logger' const warn = logger('tauri', 'red') +function resolvePath(basePath: string, dir: string): string { + return dir.startsWith('/') || /^\S:/g.test(dir) + ? dir + : resolve(basePath, dir) +} + const getAppDir = (): string => { let dir = process.cwd() let count = 0 @@ -24,8 +30,8 @@ const appDir = getAppDir() const tauriDir = resolve(appDir, 'src-tauri') const resolveDir = { - app: (dir: string) => resolve(appDir, dir), - tauri: (dir: string) => resolve(tauriDir, dir) + app: (dir: string) => resolvePath(appDir, dir), + tauri: (dir: string) => resolvePath(tauriDir, dir) } export { appDir, tauriDir, resolveDir as resolve } diff --git a/cli/tauri.js/src/helpers/tauri-config.ts b/cli/tauri.js/src/helpers/tauri-config.ts index f94ce4242..63eeb5af6 100644 --- a/cli/tauri.js/src/helpers/tauri-config.ts +++ b/cli/tauri.js/src/helpers/tauri-config.ts @@ -1,5 +1,4 @@ import { existsSync } from 'fs-extra' -import { resolve } from 'path' import { TauriConfig } from 'types' import merge from 'webpack-merge' import logger from '../helpers/logger' @@ -58,13 +57,19 @@ const getTauriConfig = (cfg: Partial): TauriConfig => { const runningDevServer = config.build.devPath && config.build.devPath.startsWith('http') if (!runningDevServer) { - config.build.devPath = resolve(appPaths.tauriDir, config.build.devPath) + config.build.devPath = appPaths.resolve.tauri(config.build.devPath) + process.env.TAURI_DIST_DIR = appPaths.resolve.app(config.build.devPath) } if (config.build.distDir) { - config.build.distDir = resolve(appPaths.tauriDir, config.build.distDir) + config.build.distDir = appPaths.resolve.tauri(config.build.distDir) + process.env.TAURI_DIST_DIR = appPaths.resolve.app(config.build.distDir) + } + + if (!process.env.TAURI_DIST_DIR) { + error("Couldn't resolve the dist dir. Make sure you have `devPath` or `distDir` under tauri.conf.json > build") + process.exit(1) } - process.env.TAURI_DIST_DIR = appPaths.resolve.app(config.build.distDir) process.env.TAURI_DIR = appPaths.tauriDir process.env.TAURI_CONFIG = JSON.stringify(config) diff --git a/cli/tauri.js/test/jest/__tests__/build.spec.js b/cli/tauri.js/test/jest/__tests__/build.spec.js index 1ed7bdc5c..c78a3c73d 100644 --- a/cli/tauri.js/test/jest/__tests__/build.spec.js +++ b/cli/tauri.js/test/jest/__tests__/build.spec.js @@ -13,6 +13,9 @@ function runBuildTest(tauriConfig) { let success = false const server = fixtureSetup.startServer(() => { success = true + try { + process.kill(appPid) + } catch {} // wait for the app process to be killed setTimeout(resolve, 2000) }) @@ -37,7 +40,7 @@ function runBuildTest(tauriConfig) { reject("App didn't reply") }) } - }, 2500) + }, 15000) } catch (error) { reject(error) } diff --git a/cli/tauri.js/test/jest/__tests__/template.spec.js b/cli/tauri.js/test/jest/__tests__/template.spec.js index 6ad1b9846..5253248e2 100644 --- a/cli/tauri.js/test/jest/__tests__/template.spec.js +++ b/cli/tauri.js/test/jest/__tests__/template.spec.js @@ -16,7 +16,7 @@ describe('[CLI] tauri.js template', () => { const init = require('api/init') init({ directory: process.cwd(), - force: true, + force: 'all', tauriPath: resolve(__dirname, '../../../../..') }) diff --git a/cli/tauri.js/test/jest/fixtures/app-test-setup.js b/cli/tauri.js/test/jest/fixtures/app-test-setup.js index b165b6d54..d94e1edc4 100644 --- a/cli/tauri.js/test/jest/fixtures/app-test-setup.js +++ b/cli/tauri.js/test/jest/fixtures/app-test-setup.js @@ -5,6 +5,12 @@ const mockFixtureDir = path.resolve(__dirname, '../fixtures') module.exports.fixtureDir = mockFixtureDir +function mockResolvePath (basePath, dir) { + return dir.startsWith('/') || /^\S:/g.test(dir) + ? dir + : path.resolve(basePath, dir) +} + module.exports.initJest = (mockFixture) => { jest.setTimeout(720000) jest.mock('helpers/non-webpack-require', () => { @@ -25,8 +31,8 @@ module.exports.initJest = (mockFixture) => { appDir, tauriDir, resolve: { - app: dir => path.join(appDir, dir), - tauri: dir => path.join(tauriDir, dir) + app: dir => mockResolvePath(appDir, dir), + tauri: dir => mockResolvePath(tauriDir, dir) } } }) @@ -34,8 +40,35 @@ module.exports.initJest = (mockFixture) => { jest.spyOn(process, 'exit').mockImplementation(() => {}) } -module.exports.startServer = (onReply) => { +module.exports.startServer = (onSuccess) => { const http = require('http') + + 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 + } + function addResponse (response) { + responses[response.cmd] = true + if (!Object.values(responses).some(c => c === null)) { + server.close(onSuccess) + } + } + const app = http.createServer((req, res) => { // Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*') @@ -50,16 +83,23 @@ module.exports.startServer = (onReply) => { } if (req.method === 'POST') { + let body = '' + req.on('data', chunk => { + body += chunk.toString() + }) if (req.url === '/reply') { - let body = '' - req.on('data', chunk => { - body += chunk.toString() - }) req.on('end', () => { - expect(JSON.parse(body)).toStrictEqual({ - msg: 'TEST' - }) - server.close(onReply) + const json = JSON.parse(body) + addResponse(json) + res.writeHead(200) + res.end() + }) + } + if (req.url === '/error') { + req.on('end', () => { + res.writeHead(200) + res.end() + throw new Error(body) }) } } diff --git a/cli/tauri.js/test/jest/fixtures/app/dist/index.html b/cli/tauri.js/test/jest/fixtures/app/dist/index.html index 3952d556c..91bdf2214 100644 --- a/cli/tauri.js/test/jest/fixtures/app/dist/index.html +++ b/cli/tauri.js/test/jest/fixtures/app/dist/index.html @@ -2,22 +2,92 @@ diff --git a/cli/tauri.js/test/jest/fixtures/app/index.js b/cli/tauri.js/test/jest/fixtures/app/index.js deleted file mode 100644 index 05d17c7ae..000000000 --- a/cli/tauri.js/test/jest/fixtures/app/index.js +++ /dev/null @@ -1,54 +0,0 @@ -const express = require('express') -const cors = require('cors') -const app = express() -app.use(cors()) -app.use(express.json()) -const port = 7000 -let appPid - -app.post('/reply', (req, res) => { - if (req.body && req.body.msg !== 'TEST') { - throw new Error(`unexpected reply ${JSON.stringify(req.body)}`) - } - console.log('App event replied') - exit(0) -}) - -const server = app.listen(port, () => console.log(`Test listening on port ${port}!`)) - -const exit = code => { - server.close() - process.kill(appPid) - process.exit(code) -} - -const path = require('path') -const dist = path.resolve(__dirname, 'dist') - -const build = require('../cli/tauri.js/dist/api/build') -build({ - build: { - devPath: dist - }, - ctx: { - debug: true - }, - tauri: { - embeddedServer: { - active: true - } - } -}).then(() => { - const spawn = require('../cli/tauri.js/dist/helpers/spawn').spawn - const artifactPath = path.resolve(__dirname, 'src-tauri/target/debug/app') - appPid = spawn( - process.platform === 'win32' ? `${artifactPath}.exe` : artifactPath.replace('debug/app', 'debug/./app'), - [], - null - ) - - // if it didn't reply, throw an error - setTimeout(() => { - throw new Error("App didn't reply") - }, 2000) -}) diff --git a/cli/tauri.js/test/jest/fixtures/app/src-tauri/src/main.rs b/cli/tauri.js/test/jest/fixtures/app/src-tauri/src/main.rs index 76df7c51f..b5e6ddcda 100644 --- a/cli/tauri.js/test/jest/fixtures/app/src-tauri/src/main.rs +++ b/cli/tauri.js/test/jest/fixtures/app/src-tauri/src/main.rs @@ -6,16 +6,18 @@ extern crate serde_json; fn main() { tauri::AppBuilder::new() - .setup(|_webview| { + .setup(|_webview, _| { let handle = _webview.handle(); tauri::event::listen(String::from("hello"), move |_| { - tauri::event::emit(&handle, String::from("reply"), "{ msg: 'TEST' }".to_string()); + tauri::event::emit(&handle, String::from("reply"), Some("{ msg: 'TEST' }".to_string())); }); }) .invoke_handler(|webview, arg| { use cmd::Cmd::*; match serde_json::from_str(arg) { - Err(_) => {} + Err(e) => { + Err(e.to_string()) + } Ok(command) => { match command { // definitions for your custom commands from Cmd here @@ -23,6 +25,7 @@ fn main() { webview.exit(); } } + Ok(()) } } }) diff --git a/tauri/src/endpoints/dialog.rs b/tauri/src/endpoints/dialog.rs index 0195384ee..0b68261ea 100644 --- a/tauri/src/endpoints/dialog.rs +++ b/tauri/src/endpoints/dialog.rs @@ -27,7 +27,7 @@ pub fn open( select(options.filter, options.default_path) }; response - .map(|r| map_response(r)) + .map(map_response) .map_err(|e| crate::ErrorKind::Dialog(e.to_string()).into()) }, callback, @@ -45,7 +45,7 @@ pub fn save( webview, move || { save_file(options.filter, options.default_path) - .map(|r| map_response(r)) + .map(map_response) .map_err(|e| crate::ErrorKind::Dialog(e.to_string()).into()) }, callback,