-
Notifications
You must be signed in to change notification settings - Fork 171
fix: fix cf-pages gh pkgs sdk install #7638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+225
−23
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
28c2420
fix: fix cf-pages gh pkgs sdk install by storing npm creds in local t…
alfetopito 75f6cf7
chore(i18n): extract i18n strings [automatic]
github-actions[bot] f2f69b0
refactor: remove unnecessary 1 liner fns
alfetopito 52baf6a
test: updated unit test to cover case when script fails
alfetopito 82457b6
test: fixed unit tests broken in parent PR
alfetopito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,171 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { readFileSync } from 'node:fs' | ||
| import { describe, it } from 'node:test' | ||
| import vm from 'node:vm' | ||
|
|
||
| describe('install.js', () => { | ||
| it('passes GitHub Packages auth to pnpm through a temporary npmrc', () => { | ||
| const context = createInstallScriptContext() | ||
|
|
||
| context.run() | ||
|
|
||
| assert.equal(context.execCalls.length, 1) | ||
| const [{ command, options }] = context.execCalls | ||
|
|
||
| assert.equal(command, 'pnpm install --no-frozen-lockfile') | ||
| assert.equal(options.cwd, '/repo') | ||
| assert.equal(options.env.npm_config_userconfig, '/tmp/cowswap-pnpm-test/.npmrc') | ||
| assert.equal(options.env['npm_config_//npm.pkg.github.com/:_authToken'], undefined) | ||
| assert.equal(options.env['npm_config_@cowprotocol:registry'], undefined) | ||
|
|
||
| assertTempNpmrcWrittenAndRemoved(context) | ||
| }) | ||
|
|
||
| it('removes the temporary npmrc when pnpm install fails', () => { | ||
| const failure = new Error('fail') | ||
| const exitError = new Error('process.exit') | ||
| let exitCode | ||
| const context = createInstallScriptContext({ | ||
| execSync() { | ||
| throw failure | ||
| }, | ||
| processExit(code) { | ||
| exitCode = code | ||
| throw exitError | ||
| }, | ||
| }) | ||
| let caughtError | ||
|
|
||
| try { | ||
| context.run() | ||
| } catch (err) { | ||
| caughtError = err | ||
| } | ||
|
|
||
| assert.equal(caughtError, exitError) | ||
| assert.equal(exitCode, 1) | ||
| assertTempNpmrcWrittenAndRemoved(context) | ||
| }) | ||
| }) | ||
|
|
||
| function createInstallScriptContext({ execSync = () => {}, processExit = defaultProcessExit } = {}) { | ||
| const script = readFileSync(new URL('./install.js', import.meta.url), 'utf8') | ||
| const execCalls = [] | ||
| const writes = [] | ||
| const removals = [] | ||
|
|
||
| const fakeFs = { | ||
| existsSync() { | ||
| return false | ||
| }, | ||
| mkdtempSync(prefix) { | ||
| assert.equal(prefix, '/tmp/cowswap-pnpm-') | ||
| return '/tmp/cowswap-pnpm-test' | ||
| }, | ||
| readFileSync() { | ||
| throw new Error('unexpected fs.readFileSync call') | ||
| }, | ||
| readdirSync() { | ||
| throw new Error('unexpected fs.readdirSync call') | ||
| }, | ||
| rmSync(path, options) { | ||
| removals.push({ path, options }) | ||
| }, | ||
| writeFileSync(path, content, options) { | ||
| writes.push({ path, content, options }) | ||
| }, | ||
| } | ||
|
|
||
| const sandbox = { | ||
| __dirname: '/repo/tools/scripts', | ||
| console: { | ||
| error() {}, | ||
| log() {}, | ||
| warn() {}, | ||
| }, | ||
| process: { | ||
| env: { PACKAGE_READ_AUTH_TOKEN: 'secret-token' }, | ||
| exit(code) { | ||
| processExit(code) | ||
| }, | ||
| }, | ||
| require(id) { | ||
| if (id === 'child_process') { | ||
| return { | ||
| execSync(command, options) { | ||
| execCalls.push({ command, options }) | ||
| execSync(command, options) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| if (id === 'fs') return fakeFs | ||
| if (id === 'os') { | ||
| return { | ||
| tmpdir() { | ||
| return '/tmp' | ||
| }, | ||
| } | ||
| } | ||
| if (id === 'path') return pathModule | ||
| if (id === '../../apps/cowswap-frontend/package.json') { | ||
| return { | ||
| dependencies: { | ||
| '@cowprotocol/sdk-bridging': '4.1.2-pr-897-c3d02aa9.0', | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| throw new Error(`unexpected require(${id})`) | ||
| }, | ||
| } | ||
|
|
||
| return { | ||
| execCalls, | ||
| removals, | ||
| run() { | ||
| vm.runInNewContext( | ||
| `(function(require, process, console, __dirname) {\n${script}\n})(require, process, console, __dirname)`, | ||
| sandbox, | ||
| ) | ||
| }, | ||
| writes, | ||
| } | ||
| } | ||
|
|
||
| function defaultProcessExit(code) { | ||
| throw new Error(`unexpected process.exit(${code})`) | ||
| } | ||
|
|
||
| function assertTempNpmrcWrittenAndRemoved({ removals, writes }) { | ||
| assert.equal(writes.length, 1) | ||
| assert.equal(writes[0].path, '/tmp/cowswap-pnpm-test/.npmrc') | ||
| assert.equal( | ||
| writes[0].content, | ||
| '@cowprotocol:registry=https://npm.pkg.github.com\n' + '//npm.pkg.github.com/:_authToken=secret-token\n', | ||
| ) | ||
| assert.equal(writes[0].options.mode, 0o600) | ||
|
|
||
| assert.equal(removals.length, 1) | ||
| assert.equal(removals[0].path, '/tmp/cowswap-pnpm-test') | ||
| assert.equal(removals[0].options.force, true) | ||
| assert.equal(removals[0].options.recursive, true) | ||
| } | ||
|
|
||
| const pathModule = { | ||
| join(...parts) { | ||
| return parts.join('/').replace(/\/+/g, '/') | ||
| }, | ||
| resolve(...parts) { | ||
| const joined = parts.join('/') | ||
| const segments = [] | ||
|
|
||
| for (const segment of joined.split('/')) { | ||
| if (!segment || segment === '.') continue | ||
| if (segment === '..') segments.pop() | ||
| else segments.push(segment) | ||
| } | ||
|
|
||
| return '/' + segments.join('/') | ||
| }, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This had no effect on cf-pages