From f01069087253f5c32a4177484c28121f2584826b Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Mon, 15 Mar 2021 20:10:03 +0530 Subject: [PATCH] test: add serve smoketest (#2502) * chore: add serve test * chore: run serve test --- smoketests/index.js | 1 + .../missing-command-packages/serve.test.js | 127 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 smoketests/missing-command-packages/serve.test.js diff --git a/smoketests/index.js b/smoketests/index.js index 3bf2e59e7d2..6123c8837c7 100644 --- a/smoketests/index.js +++ b/smoketests/index.js @@ -3,6 +3,7 @@ const tests = [ require('./missing-packages/webpack-dev-server.test.js'), require('./missing-packages/webpack.test.js'), require('./missing-command-packages/generator.test.js'), + require('./missing-command-packages/serve.test.js'), ]; (async () => { diff --git a/smoketests/missing-command-packages/serve.test.js b/smoketests/missing-command-packages/serve.test.js new file mode 100644 index 00000000000..e5ce913247b --- /dev/null +++ b/smoketests/missing-command-packages/serve.test.js @@ -0,0 +1,127 @@ +'use strict'; + +const path = require('path'); +const execa = require('execa'); +const { renameSync } = require('fs'); +const stripAnsi = require('strip-ansi'); + +const ROOT = process.env.GITHUB_WORKSPACE ? process.env.GITHUB_WORKSPACE : path.resolve(__dirname, '../../'); +const CLI_ENTRY_PATH = path.resolve(ROOT, './packages/webpack-cli/bin/cli.js'); + +const getPkgPath = (pkg) => { + return path.resolve(ROOT, `./node_modules/@webpack-cli/${pkg}`); +}; + +const swapPkgName = (current, next) => { + console.log(` swapping ${current} with ${next}`); + renameSync(getPkgPath(current), getPkgPath(next)); +}; + +const runTest = () => { + // Simulate package missing + swapPkgName('serve', '.serve'); + + const proc = execa(CLI_ENTRY_PATH, ['serve'], { + cwd: __dirname, + }); + + proc.stdin.setDefaultEncoding('utf-8'); + + proc.stdout.on('data', (chunk) => { + console.log(` stdout: ${chunk.toString()}`); + }); + + return new Promise((resolve) => { + setTimeout(() => { + proc.kill(); + }, 30000); + + const errorMessage = "For using this command you need to install: '@webpack-cli/serve' package"; + + let hasErrorMessage = false, + hasPassed = false; + + proc.stderr.on('data', (chunk) => { + let data = stripAnsi(chunk.toString()); + console.log(` stderr: ${data}`); + + if (data.includes(errorMessage)) { + hasErrorMessage = true; + } + + if (hasErrorMessage) { + hasPassed = true; + proc.kill(); + } + }); + + proc.on('exit', () => { + swapPkgName('.serve', 'serve'); + resolve(hasPassed); + }); + + proc.on('error', () => { + swapPkgName('.serve', 'serve'); + resolve(false); + }); + }); +}; + +const runTestWithHelp = () => { + // Simulate package missing + swapPkgName('serve', '.serve'); + + const proc = execa(CLI_ENTRY_PATH, ['help', 'serve'], { + cwd: __dirname, + }); + + proc.stdin.setDefaultEncoding('utf-8'); + + proc.stdout.on('data', (chunk) => { + console.log(` stdout: ${chunk.toString()}`); + }); + + return new Promise((resolve) => { + setTimeout(() => { + proc.kill(); + }, 30000); + + const logMessage = "For using 'serve' command you need to install '@webpack-cli/serve' package"; + const undefinedLogMessage = "Can't find and load command"; + + let hasLogMessage = false, + hasUndefinedLogMessage = false, + hasPassed = false; + + proc.stderr.on('data', (chunk) => { + let data = stripAnsi(chunk.toString()); + console.log(` stderr: ${data}`); + + if (data.includes(logMessage)) { + hasLogMessage = true; + } + + if (data.includes(undefinedLogMessage)) { + hasUndefinedLogMessage = true; + } + + if (hasLogMessage || hasUndefinedLogMessage) { + hasPassed = true; + proc.kill(); + } + }); + + proc.on('exit', () => { + swapPkgName('.serve', 'serve'); + resolve(hasPassed); + }); + + proc.on('error', () => { + swapPkgName('.serve', 'serve'); + resolve(false); + }); + }); +}; + +module.exports.run = [runTest, runTestWithHelp]; +module.exports.name = 'Missing @webpack-cli/serve';