-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add serve test * chore: run serve test
- Loading branch information
1 parent
a78ccfe
commit f010690
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |