-
Notifications
You must be signed in to change notification settings - Fork 907
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Manual] [Backport 1.x] Automates chromedriver version selection for …
…tests (#2990) (#3128) (#3294) * Automates chromedriver version selection for tests (#2990) Signed-off-by: Miki <amoo_miki@yahoo.com> (cherry picked from commit a26fb43) Signed-off-by: Josh Romero <rmerqg@amazon.com> * Make `upgrade_chromedriver.js` work on Node v10 Signed-off-by: Miki <miki@amazon.com> Signed-off-by: Josh Romero <rmerqg@amazon.com> Signed-off-by: Miki <miki@amazon.com> Co-authored-by: Miki <miki@amazon.com> (cherry picked from commit 3dab4c0) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b5ea71b
commit 78308ba
Showing
3 changed files
with
135 additions
and
11 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.ackrc | ||
/.opensearch | ||
/.chromium | ||
/package.json.bak | ||
.DS_Store | ||
.node_binaries | ||
.native_modules | ||
|
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,123 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Upgrades the chromedriver dev-dependency to the one supported by the version of Google Chrome | ||
* installed on the machine. | ||
* | ||
* Usage: node scripts/upgrade_chromedriver.js [--install] | ||
*/ | ||
|
||
/* eslint no-restricted-syntax: 0 */ | ||
const { execSync, spawnSync } = require('child_process'); | ||
const { createReadStream, createWriteStream, unlinkSync, renameSync, existsSync } = require('fs'); | ||
const { createInterface } = require('readline'); | ||
|
||
if (!process.argv.includes(__filename)) { | ||
console.error('Usage: node scripts/upgrade_chromedriver.js [--install]'); | ||
process.exit(1); | ||
} | ||
|
||
const versionCheckCommands = []; | ||
|
||
switch (process.platform) { | ||
case 'win32': | ||
versionCheckCommands.push( | ||
'powershell "(Get-Item \\"$Env:Programfiles/Google/Chrome/Application/chrome.exe\\").VersionInfo.FileVersion"' | ||
); | ||
break; | ||
|
||
case 'darwin': | ||
versionCheckCommands.push( | ||
'/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --version' | ||
); | ||
break; | ||
|
||
default: | ||
versionCheckCommands.push( | ||
...[ | ||
'/usr/bin', | ||
'/usr/local/bin', | ||
'/usr/sbin', | ||
'/usr/local/sbin', | ||
'/opt/bin', | ||
'/usr/bin/X11', | ||
'/usr/X11R6/bin', | ||
].flatMap((loc) => | ||
[ | ||
'google-chrome --version', | ||
'google-chrome-stable --version', | ||
'chromium --version', | ||
'chromium-browser --version', | ||
].map((cmd) => `${loc}/${cmd}`) | ||
) | ||
); | ||
} | ||
|
||
let versionCheckOutput; | ||
versionCheckCommands.some((cmd) => { | ||
try { | ||
console.log(cmd); | ||
versionCheckOutput = (execSync(cmd, { encoding: 'utf8' }) || '').trim(); | ||
return true; | ||
} catch (e) { | ||
console.log('Failed to get version using', cmd); | ||
} | ||
}); | ||
|
||
// Versions 90+ | ||
const versionCheckOutputTokens = (versionCheckOutput || '').match(/(?:^|\s)(9\d|\d{3})\./); | ||
const majorVersion = Array.isArray(versionCheckOutputTokens) && versionCheckOutputTokens[1]; | ||
|
||
if (majorVersion) { | ||
if (process.argv.includes('--install')) { | ||
console.log(`Installing chromedriver@^${majorVersion}`); | ||
|
||
spawnSync(`yarn add --dev chromedriver@^${majorVersion}`, { | ||
stdio: 'inherit', | ||
cwd: process.cwd(), | ||
shell: true, | ||
}); | ||
} else { | ||
console.log(`Upgrading to chromedriver@^${majorVersion}`); | ||
|
||
let upgraded = false; | ||
const writeStream = createWriteStream('package.json.upgrading-chromedriver', { flags: 'w' }); | ||
const rl = createInterface({ | ||
input: createReadStream('package.json'), | ||
crlfDelay: Infinity, | ||
}); | ||
rl.on('line', (line) => { | ||
if (line.includes('"chromedriver": "')) { | ||
line = line.replace( | ||
/"chromedriver":\s*"[~^]?\d[\d.]*\d"/, | ||
`"chromedriver": "^${majorVersion}"` | ||
); | ||
upgraded = true; | ||
} | ||
writeStream.write(line + '\n', 'utf8'); | ||
}); | ||
rl.on('close', () => { | ||
writeStream.end(); | ||
if (upgraded) { | ||
// Remove any previous backups | ||
if (existsSync('package.json.bak')) unlinkSync('package.json.bak'); | ||
|
||
renameSync('package.json', 'package.json.bak'); | ||
renameSync('package.json.upgrading-chromedriver', 'package.json'); | ||
|
||
console.log(`Backed up package.json and updated chromedriver to ${majorVersion}`); | ||
} else { | ||
unlinkSync('package.json.upgrading-chromedriver'); | ||
console.error( | ||
`Failed to update chromedriver to ${majorVersion}. Try adding the \`--install\` switch.` | ||
); | ||
} | ||
}); | ||
} | ||
} else { | ||
console.debug(versionCheckOutput); | ||
console.error(`Failed to extract the version of the installed Google Chrome.`); | ||
} |