-
-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add test for missing packages (#2296)
- Loading branch information
1 parent
caa3348
commit ff2b2b3
Showing
5 changed files
with
177 additions
and
3 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
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,20 @@ | ||
/* eslint-disable node/no-unpublished-require */ | ||
const tests = [require('./missing-packages/webpack-dev-server.test.js'), require('./missing-packages/webpack.test.js')]; | ||
|
||
(async () => { | ||
let isAllPassed = true; | ||
for await (const test of tests) { | ||
console.log(`\nRUN ${test.name}`); | ||
const isPass = await test.run(); | ||
if (!isPass) { | ||
console.log(`FAIL ${test.name}`); | ||
isAllPassed = false; | ||
} else { | ||
console.log(`PASS ${test.name}`); | ||
} | ||
} | ||
if (!isAllPassed) { | ||
process.exit(2); | ||
} | ||
process.exit(0); | ||
})(); |
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,75 @@ | ||
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/${pkg}`); | ||
}; | ||
|
||
const swapPkgName = (current, next) => { | ||
console.log(` swapping ${current} with ${next}`); | ||
renameSync(getPkgPath(current), getPkgPath(next)); | ||
}; | ||
|
||
const runTest = () => { | ||
// Simulate package missing | ||
swapPkgName('webpack-dev-server', '.webpack-dev-server'); | ||
|
||
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(() => { | ||
console.log(' timeout: killing process'); | ||
proc.kill(); | ||
}, 5000); | ||
|
||
const logMessage = "For using 'serve' command you need to install: 'webpack-dev-server' package"; | ||
const prompt = 'Would you like to install'; | ||
let hasLogMessage = false, | ||
hasPrompt = 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(prompt)) { | ||
hasPrompt = true; | ||
} | ||
|
||
if (hasLogMessage && hasPrompt) { | ||
hasPassed = true; | ||
proc.kill(); | ||
} | ||
}); | ||
|
||
proc.on('exit', () => { | ||
swapPkgName('.webpack-dev-server', 'webpack-dev-server'); | ||
resolve(hasPassed); | ||
}); | ||
|
||
proc.on('error', () => { | ||
swapPkgName('.webpack-dev-server', 'webpack-dev-server'); | ||
resolve(false); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.run = runTest; | ||
module.exports.name = 'Missing webpack-dev-server'; |
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,74 @@ | ||
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/${pkg}`); | ||
}; | ||
|
||
const swapPkgName = (current, next) => { | ||
console.log(` swapping ${current} with ${next}`); | ||
renameSync(getPkgPath(current), getPkgPath(next)); | ||
}; | ||
|
||
const runTest = () => { | ||
// Simulate package missing | ||
swapPkgName('webpack', '.webpack'); | ||
|
||
const proc = execa(CLI_ENTRY_PATH, [], { | ||
cwd: __dirname, | ||
}); | ||
|
||
proc.stdin.setDefaultEncoding('utf-8'); | ||
|
||
proc.stdout.on('data', (chunk) => { | ||
console.log(` stdout: ${chunk.toString()}`); | ||
}); | ||
|
||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
proc.kill(); | ||
}, 5000); | ||
|
||
const logMessage = 'It looks like webpack is not installed.'; | ||
const prompt = 'Would you like to install'; | ||
let hasLogMessage = false, | ||
hasPrompt = 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(prompt)) { | ||
hasPrompt = true; | ||
} | ||
|
||
if (hasLogMessage && hasPrompt) { | ||
hasPassed = true; | ||
proc.kill(); | ||
} | ||
}); | ||
|
||
proc.on('exit', () => { | ||
swapPkgName('.webpack', 'webpack'); | ||
resolve(hasPassed); | ||
}); | ||
|
||
proc.on('error', () => { | ||
swapPkgName('.webpack', 'webpack'); | ||
resolve(false); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.run = runTest; | ||
module.exports.name = 'Missing webpack'; |