Repository navigation
test: remove --proxyquire-shim from common, bigquery, datastore, and dns #9639
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
danieljbruce
wants to merge
2
commits into
googleapis:main
from
danieljbruce:fix/remove-proxyquire-shim-core-dns-ds-bq
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import * as Module from 'module'; | ||
| import * as path from 'path'; | ||
|
|
||
| delete require.cache[require.resolve(__filename)]; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type Stubs = Record<string, any>; | ||
| const mod = Module as unknown as { | ||
| prototype: {require: NodeRequire}; | ||
| _resolveFilename: (request: string, parent: NodeModule) => string; | ||
| }; | ||
| const {hasOwnProperty} = Object.prototype; | ||
| const hasOwn = (o: unknown, k: string) => | ||
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); | ||
| const isGlobal = (s: unknown) => | ||
| hasOwn(s, '@global') || hasOwn(s, '@runtimeGlobal'); | ||
| const resolveFrom = (base: string, req: string) => { | ||
| try { | ||
| return require.resolve(req, {paths: [path.dirname(base)]}); | ||
| } catch { | ||
| return req[0] === '.' ? path.resolve(path.dirname(base), req) : req; | ||
| } | ||
| }; | ||
|
|
||
| const parent = module.parent || module; | ||
| let skipCallThru = false; | ||
| let keepCache = true; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| function proxyquire<T = any>(request: string, stubs: Stubs = {}): T { | ||
| const sut = mod._resolveFilename(request, parent); | ||
| const resolved: Stubs = {}; | ||
| let global = false; | ||
| for (const [k, v] of Object.entries(stubs)) { | ||
| if (isGlobal(v)) global = true; | ||
| resolved[resolveFrom(sut, k)] = v; | ||
| } | ||
| const cache = require.cache; | ||
| const keys = global | ||
| ? Object.keys(cache).filter(id => !id.endsWith('.node')) | ||
| : [sut]; | ||
| const saved = keys.map(id => [id, cache[id]] as const); | ||
| for (const id of keys) delete cache[id]; | ||
| const prev = mod.prototype.require; | ||
| const req = function (this: NodeModule, id: string) { | ||
| const isSut = this?.filename === sut; | ||
| if (this?.filename && (isSut || global)) { | ||
| const hit = (isSut || !id.startsWith('.')) && hasOwn(stubs, id); | ||
| const rId = hit ? '' : resolveFrom(this.filename, id); | ||
| if (hit || hasOwn(resolved, rId)) { | ||
| const stub = hit ? stubs[id] : resolved[rId]; | ||
| if (isSut || isGlobal(stub)) { | ||
| if (stub === null) { | ||
| const e = new Error(`Cannot find module '${id}'`); | ||
| throw Object.assign(e, {code: 'MODULE_NOT_FOUND'}); | ||
| } | ||
| const skip = hasOwn(stub, '@noCallThru') | ||
| ? stub['@noCallThru'] | ||
| : skipCallThru; | ||
| if (!skip && stub && ['object', 'function'].includes(typeof stub)) { | ||
| try { | ||
| const real = prev.call(this, id); | ||
| if (real && ['object', 'function'].includes(typeof real)) { | ||
| for (const k of Object.keys(real)) { | ||
| if (!(k in stub)) stub[k] = real[k]; | ||
| } | ||
| } | ||
| } catch { | ||
| // ignore | ||
| } | ||
| } | ||
| return stub; | ||
| } | ||
| } | ||
| } | ||
| return prev.call(this, id); | ||
| }; | ||
| mod.prototype.require = Object.assign(req, prev); | ||
| try { | ||
| return prev.call(parent, request); | ||
| } finally { | ||
| mod.prototype.require = prev; | ||
| for (const id of global ? Object.keys(cache) : [sut]) { | ||
| if (!id.endsWith('.node')) delete cache[id]; | ||
| } | ||
| for (const [id, m] of saved) { | ||
| if (m && (keepCache || id !== sut)) cache[id] = m; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-namespace | ||
| namespace proxyquire { | ||
| export const load = proxyquire; | ||
| export const noCallThru = () => ((skipCallThru = true), proxyquire); | ||
| export const callThru = () => ((skipCallThru = false), proxyquire); | ||
| export const noPreserveCache = () => ((keepCache = false), proxyquire); | ||
| export const preserveCache = () => ((keepCache = true), proxyquire); | ||
| } | ||
|
|
||
| export = proxyquire; | ||
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
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
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
Oops, something went wrong.
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.
The
hasOwnhelper currently checkstypeof o === 'object'. This will returnfalseif the stub is a function (e.g., a constructor or a stubbed class), preventing properties like@globalor@noCallThrufrom being detected on function stubs. Updating the check to includetypeof o === 'function'resolves this issue.