From 459a6679fe48269c580cb7570ef9111117473390 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Thu, 15 Oct 2026 20:54:38 +0000 Subject: [PATCH 1/2] test: replace proxyquire with sinon in gax, common, error-reporting, translate --- core/packages/gax/package.json | 2 +- core/packages/gax/test/unit/grpc.ts | 35 +++++++------- .../nodejs-googleapis-common/package.json | 2 +- .../test/test.authplus.ts | 25 +++++----- .../test/test.http2.ts | 21 ++++---- handwritten/error-reporting/package.json | 2 +- .../test/unit/google-apis/auth-client.ts | 48 +++++++++---------- packages/google-cloud-translate/package.json | 2 +- packages/google-cloud-translate/test/index.ts | 43 ++++++++++++----- 9 files changed, 97 insertions(+), 83 deletions(-) diff --git a/core/packages/gax/package.json b/core/packages/gax/package.json index 7c637b1ba20a..96e47deed478 100644 --- a/core/packages/gax/package.json +++ b/core/packages/gax/package.json @@ -65,7 +65,7 @@ "scripts": { "docs": "jsdoc -c .jsdoc.js", "pretest": "pnpm run compile", - "test": "node ../../../bin/run-test.cjs --proxyquire-shim build/test/unit", + "test": "node ../../../bin/run-test.cjs build/test/unit", "lint": "gts check src samples test", "clean": "gts clean", "compile": "node ./scripts/copy-protos.js && tsc -p . && cp src/*.json build/src && cp -r test/fixtures build/test && cp -r protos build/", diff --git a/core/packages/gax/test/unit/grpc.ts b/core/packages/gax/test/unit/grpc.ts index 76f4207053b9..cf2662eeb2c1 100644 --- a/core/packages/gax/test/unit/grpc.ts +++ b/core/packages/gax/test/unit/grpc.ts @@ -20,9 +20,8 @@ import assert from 'assert'; import * as path from 'path'; -import proxyquire from 'proxyquire'; import * as sinon from 'sinon'; -import {mkdirSync, writeFileSync} from 'fs'; +import fs, {mkdirSync, writeFileSync} from 'fs'; import * as fsp from 'fs/promises'; import {afterEach, describe, it, beforeEach} from 'mocha'; @@ -613,28 +612,28 @@ describe('grpc', () => { const originPath = path.join('test', 'path', 'location'); const includePath = path.join('example', 'import.proto'); + afterEach(() => { + sinon.restore(); + }); + it('should throw an error if a file is not found', () => { - const findIncludePath = proxyquire('../../src/grpc', { - fs: { - existsSync: () => { - return false; - }, - }, - }).GoogleProtoFilesRoot._findIncludePath; - assert.throws(findIncludePath.bind(null, originPath, includePath)); + sinon.stub(fs, 'existsSync').returns(false); + assert.throws( + GoogleProtoFilesRoot._findIncludePath.bind( + null, + originPath, + includePath, + ), + ); }); it('should return the correct resolved import path', () => { const correctPath = path.join('test', 'example', 'import.proto'); - const findIncludePath = proxyquire('../../src/grpc', { - fs: { - existsSync(path: string) { - return path === correctPath; - }, - }, - }).GoogleProtoFilesRoot._findIncludePath; + sinon + .stub(fs, 'existsSync') + .callsFake((p: fs.PathLike) => p === correctPath); assert.strictEqual( - findIncludePath(originPath, includePath), + GoogleProtoFilesRoot._findIncludePath(originPath, includePath), correctPath, ); }); diff --git a/core/packages/nodejs-googleapis-common/package.json b/core/packages/nodejs-googleapis-common/package.json index 5ada806ae27d..a561c40589e5 100644 --- a/core/packages/nodejs-googleapis-common/package.json +++ b/core/packages/nodejs-googleapis-common/package.json @@ -17,7 +17,7 @@ "prebenchmark": "npm run compile", "benchmark": "node build/benchmark/bench.js", "compile": "tsc -p .", - "test": "node ../../../bin/run-test.cjs --proxyquire-shim build/test", + "test": "node ../../../bin/run-test.cjs build/test", "system-test": "node ../../../bin/run-test.cjs build/system-test --timeout 600000", "presystem-test": "npm run compile", "fix": "gts fix", diff --git a/core/packages/nodejs-googleapis-common/test/test.authplus.ts b/core/packages/nodejs-googleapis-common/test/test.authplus.ts index 9a67b52adac6..a2c5b2aae6d5 100644 --- a/core/packages/nodejs-googleapis-common/test/test.authplus.ts +++ b/core/packages/nodejs-googleapis-common/test/test.authplus.ts @@ -13,23 +13,17 @@ // limitations under the License. import * as assert from 'assert'; -import {describe, it} from 'mocha'; -import * as proxyquire from 'proxyquire'; -import * as AuthPlus from '../src/authplus'; +import {afterEach, describe, it} from 'mocha'; +import * as sinon from 'sinon'; +import {AuthPlus} from '../src/authplus'; describe(__filename, () => { - const AuthyPlus: typeof AuthPlus = proxyquire('../src/authplus', { - 'google-auth-library': { - GoogleAuth: class { - async getClient() { - return {}; - } - }, - }, + afterEach(() => { + sinon.restore(); }); it('should expose auth classes', () => { - const auth = new AuthyPlus.AuthPlus(); + const auth = new AuthPlus(); assert.ok(auth.JWT); assert.ok(auth.Compute); assert.ok(auth.OAuth2); @@ -40,8 +34,13 @@ describe(__filename, () => { }); it('should get a new client', async () => { - const auth = new AuthyPlus.AuthPlus(); + const auth = new AuthPlus(); + const getClientStub = sinon + .stub(auth.GoogleAuth.prototype, 'getClient') + // eslint-disable-next-line @typescript-eslint/no-explicit-any + .resolves({} as any); const client = await auth.getClient(); assert.deepStrictEqual(client, {}); + assert.strictEqual(getClientStub.calledOnce, true); }); }); diff --git a/core/packages/nodejs-googleapis-common/test/test.http2.ts b/core/packages/nodejs-googleapis-common/test/test.http2.ts index 1da040be4074..291deae4b60b 100644 --- a/core/packages/nodejs-googleapis-common/test/test.http2.ts +++ b/core/packages/nodejs-googleapis-common/test/test.http2.ts @@ -14,14 +14,13 @@ import * as assert from 'assert'; import * as coreHttp2 from 'http2'; -import {describe, it, before, beforeEach} from 'mocha'; +import {describe, it, before, beforeEach, after} from 'mocha'; import {gzipSync} from 'zlib'; -import * as proxyquire from 'proxyquire'; import * as sinon from 'sinon'; import {Duplex, Readable} from 'stream'; import {EventEmitter} from 'events'; import {GaxiosResponse} from 'gaxios'; -import * as http2Types from '../src/http2'; +import * as http2 from '../src/http2'; import {GoogleAuth} from '../src'; @@ -59,13 +58,15 @@ describe('http2', () => { return (connectStub || (() => {}))(host); } let connectStub: Function; - let http2: typeof http2Types; + let connectSinonStub: sinon.SinonStub; before(() => { - http2 = proxyquire('../src/http2', { - http2: { - connect: fakeConnect, - }, - }); + connectSinonStub = sinon + .stub(coreHttp2, 'connect') + // eslint-disable-next-line @typescript-eslint/no-explicit-any + .callsFake(fakeConnect as any); + }); + after(() => { + connectSinonStub.restore(); }); let requestStream: Duplex; @@ -153,7 +154,7 @@ describe('http2', () => { requestStream.push(Buffer.from('{}')); requestStream.push(null); await resPromise; - await new Promise(r => setTimeout(r, 500)); + await new Promise(resolve => setTimeout(resolve, 500)); assert.strictEqual(Object.keys(http2.sessions).length, 0); }); diff --git a/handwritten/error-reporting/package.json b/handwritten/error-reporting/package.json index 99be138c9e81..145b0be715b3 100644 --- a/handwritten/error-reporting/package.json +++ b/handwritten/error-reporting/package.json @@ -23,7 +23,7 @@ "lint": "gts check", "presystem-test": "pnpm run compile", "system-test": "MOCHA_THROW_DEPRECATION=false node ../../bin/run-test.cjs build/system-test", - "test": "node ../../bin/run-test.cjs --proxyquire-shim build/test/unit", + "test": "node ../../bin/run-test.cjs build/test/unit", "clean": "gts clean", "compile": "tsc -p .", "fix": "gts fix", diff --git a/handwritten/error-reporting/test/unit/google-apis/auth-client.ts b/handwritten/error-reporting/test/unit/google-apis/auth-client.ts index 712dc4fb95af..205e352301ba 100644 --- a/handwritten/error-reporting/test/unit/google-apis/auth-client.ts +++ b/handwritten/error-reporting/test/unit/google-apis/auth-client.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import * as proxyquire from 'proxyquire'; +import {util} from '@google-cloud/common'; import {describe, beforeEach, afterEach, it} from 'mocha'; import { @@ -20,6 +20,7 @@ import { ConfigurationOptions, Logger, } from '../../../src/configuration'; +import {RequestHandler} from '../../../src/google-apis/auth-client'; import {deepStrictEqual} from '../../util'; function verifyReportedMessage( @@ -28,28 +29,6 @@ function verifyReportedMessage( expectedLogs: {error?: string; info?: string; warn?: string}, done: () => void, ) { - class ServiceStub { - authClient: {}; - request: {}; - constructor() { - this.authClient = { - async getAccessToken() { - if (errToReturn) { - throw errToReturn; - } - return 'some-token'; - }, - }; - this.request = () => {}; - } - } - - const RequestHandler = proxyquire('../../../src/google-apis/auth-client', { - '@google-cloud/common': { - Service: ServiceStub, - }, - }).RequestHandler; - const logs: {error?: string; info?: string; warn?: string} = {}; const logger = { error(text: string) { @@ -72,8 +51,27 @@ function verifyReportedMessage( }, } as {} as Logger; const config2 = new Configuration(config1, logger); - // tslint:disable-next-line:no-unused-expression - new RequestHandler(config2, logger); + const origMakeAuthFactory = util.makeAuthenticatedRequestFactory; + const origRequest = RequestHandler.prototype.request; + util.makeAuthenticatedRequestFactory = (() => ({ + authClient: { + async getAccessToken() { + if (errToReturn) { + throw errToReturn; + } + return 'some-token'; + }, + }, + getCredentials: {}, + })) as unknown as typeof util.makeAuthenticatedRequestFactory; + RequestHandler.prototype.request = (() => {}) as typeof origRequest; + try { + // tslint:disable-next-line:no-unused-expression + new RequestHandler(config2, logger); + } finally { + util.makeAuthenticatedRequestFactory = origMakeAuthFactory; + RequestHandler.prototype.request = origRequest; + } setImmediate(() => { deepStrictEqual(logs, expectedLogs); done(); diff --git a/packages/google-cloud-translate/package.json b/packages/google-cloud-translate/package.json index 328f2a0179e5..b7edd2c81e2d 100644 --- a/packages/google-cloud-translate/package.json +++ b/packages/google-cloud-translate/package.json @@ -44,7 +44,7 @@ "postpack": "minifyProtoJson", "samples-test": "cd samples/ && npm link ../ && npm i && npm test", "system-test": "node ../../bin/run-test.cjs --config ../../.mocharc.cjs --no-parallel build/system-test", - "test": "node ../../bin/run-test.cjs --proxyquire-shim --config ../../.mocharc.cjs build/test" + "test": "node ../../bin/run-test.cjs --config ../../.mocharc.cjs build/test" }, "dependencies": { "@google-cloud/common": "^8.0.1", diff --git a/packages/google-cloud-translate/test/index.ts b/packages/google-cloud-translate/test/index.ts index 8e6d714e057a..3e03bbd748cf 100644 --- a/packages/google-cloud-translate/test/index.ts +++ b/packages/google-cloud-translate/test/index.ts @@ -21,7 +21,7 @@ import * as pfy from '@google-cloud/promisify'; import * as assert from 'assert'; import {after, afterEach, before, beforeEach, describe, it} from 'mocha'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as Module from 'module'; import * as r from 'request'; import * as orig from '../src'; @@ -44,7 +44,7 @@ const fakeUtil = extend({}, util, { makeRequest( opts: DecorateRequestOptions, cfg: MakeRequestConfig, - cb: BodyResponseCallback + cb: BodyResponseCallback, ) { if (makeRequestOverride) { return makeRequestOverride(opts, cfg, cb); @@ -74,13 +74,30 @@ describe('Translate v2', () => { let translate: any; before(() => { - Translate = proxyquire('../src/v2', { + const stubs: Record = { '@google-cloud/common': { util: fakeUtil, Service: FakeService, }, '@google-cloud/promisify': fakePromisify, - }).Translate; + }; + const mod = Module as unknown as {prototype: {require: NodeRequire}}; + const origRequire = mod.prototype.require; + const v2Path = require.resolve('../src/v2'); + const cached = require.cache[v2Path]; + delete require.cache[v2Path]; + mod.prototype.require = Object.assign(function ( + this: NodeModule, + id: string, + ) { + return id in stubs ? stubs[id] : origRequire.call(this, id); + }, origRequire); + try { + Translate = require('../src/v2').Translate; + } finally { + mod.prototype.require = origRequire; + if (cached) require.cache[v2Path] = cached; + } }); beforeEach(() => { @@ -266,7 +283,7 @@ describe('Translate v2', () => { assert.strictEqual(apiResponse_, apiResponse); assert.deepStrictEqual(apiResponse_, originalApiResponse); done(); - } + }, ); }); }); @@ -314,7 +331,7 @@ describe('Translate v2', () => { assert.strictEqual(languages, null); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -359,7 +376,7 @@ describe('Translate v2', () => { assert.deepStrictEqual(languages, expectedResults); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -407,7 +424,7 @@ describe('Translate v2', () => { from: SOURCE_LANG_CODE, to: TARGET_LANG_CODE, }, - assert.ifError + assert.ifError, ); }); }); @@ -496,7 +513,7 @@ describe('Translate v2', () => { assert.strictEqual(translations, null); assert.strictEqual(resp, apiResponse); done(); - } + }, ); }); }); @@ -531,7 +548,7 @@ describe('Translate v2', () => { assert.deepStrictEqual(translations, expectedResults); assert.strictEqual(resp, apiResponse); done(); - } + }, ); }); @@ -552,7 +569,7 @@ describe('Translate v2', () => { assert.ifError(err); assert.deepStrictEqual(translations, [expectedResults]); done(); - } + }, ); }); }); @@ -584,7 +601,7 @@ describe('Translate v2', () => { FakeService.prototype.request = ( reqOpts: r.Options, - callback: Function + callback: Function, ) => { assert.strictEqual(reqOpts, fakeOptions); callback(); @@ -640,7 +657,7 @@ describe('Translate v2', () => { makeRequestOverride = ( reqOpts: r.Options, options: {}, - callback: Function + callback: Function, ) => { assert.deepStrictEqual(reqOpts, expectedReqOpts); assert.strictEqual(options, translate.options); From 542bf724a0a94e44cec0cea912694067c63a18a2 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Thu, 15 Oct 2026 20:54:40 +0000 Subject: [PATCH 2/2] test: remove --proxyquire-shim from common, bigquery, datastore, and dns --- core/common/package.json | 2 +- core/common/test/proxyquire.ts | 115 ++++++++++++++++++ core/common/test/service-object.ts | 3 +- core/common/test/service.ts | 2 +- core/common/test/util.ts | 3 +- handwritten/bigquery/package.json | 2 +- handwritten/bigquery/test/bigquery.ts | 2 +- handwritten/bigquery/test/dataset.ts | 2 +- handwritten/bigquery/test/job.ts | 2 +- handwritten/bigquery/test/model.ts | 2 +- handwritten/bigquery/test/proxyquire.ts | 115 ++++++++++++++++++ handwritten/bigquery/test/routine.ts | 2 +- handwritten/bigquery/test/rowQueue.ts | 2 +- handwritten/bigquery/test/table.ts | 2 +- handwritten/datastore/package.json | 2 +- .../datastore/system-test/transaction-spy.ts | 2 +- .../client-initialization-testing.ts | 2 +- handwritten/datastore/test/index-class.ts | 2 +- handwritten/datastore/test/index.ts | 2 +- handwritten/datastore/test/proxyquire.ts | 115 ++++++++++++++++++ handwritten/datastore/test/request.ts | 2 +- handwritten/datastore/test/transaction.ts | 2 +- handwritten/google-cloud-dns/package.json | 2 +- handwritten/google-cloud-dns/test/change.ts | 6 +- handwritten/google-cloud-dns/test/index.ts | 18 +-- .../google-cloud-dns/test/proxyquire.ts | 115 ++++++++++++++++++ handwritten/google-cloud-dns/test/record.ts | 2 +- handwritten/google-cloud-dns/test/zone.ts | 44 +++---- 28 files changed, 517 insertions(+), 55 deletions(-) create mode 100644 core/common/test/proxyquire.ts create mode 100644 handwritten/bigquery/test/proxyquire.ts create mode 100644 handwritten/datastore/test/proxyquire.ts create mode 100644 handwritten/google-cloud-dns/test/proxyquire.ts diff --git a/core/common/package.json b/core/common/package.json index 942fc0bde525..77fc610f483f 100644 --- a/core/common/package.json +++ b/core/common/package.json @@ -20,7 +20,7 @@ ], "scripts": { "docs": "jsdoc -c .jsdoc.js", - "test": "node ../../bin/run-test.cjs --proxyquire-shim build/test", + "test": "node ../../bin/run-test.cjs build/test", "prepare": "npm run compile", "pretest": "npm run compile", "compile": "tsc -p .", diff --git a/core/common/test/proxyquire.ts b/core/common/test/proxyquire.ts new file mode 100644 index 000000000000..44ff9f689c12 --- /dev/null +++ b/core/common/test/proxyquire.ts @@ -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; +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(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; diff --git a/core/common/test/service-object.ts b/core/common/test/service-object.ts index 1d3ed35a5fee..e840224c6edc 100644 --- a/core/common/test/service-object.ts +++ b/core/common/test/service-object.ts @@ -20,7 +20,8 @@ import { import * as assert from 'assert'; import {describe, it, beforeEach, afterEach} from 'mocha'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; +// eslint-disable-next-line import/namespace import * as r from 'teeny-request'; import * as sinon from 'sinon'; diff --git a/core/common/test/service.ts b/core/common/test/service.ts index 768e7db4e4a6..74d6adb779c3 100644 --- a/core/common/test/service.ts +++ b/core/common/test/service.ts @@ -15,7 +15,7 @@ import * as assert from 'assert'; import {describe, it, before, beforeEach, after} from 'mocha'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import {Request} from 'teeny-request'; import {AuthClient, GoogleAuth, OAuth2Client} from 'google-auth-library'; diff --git a/core/common/test/util.ts b/core/common/test/util.ts index 6c018afd4d3d..617a02e2c7ee 100644 --- a/core/common/test/util.ts +++ b/core/common/test/util.ts @@ -26,7 +26,8 @@ import { OAuth2Client, } from 'google-auth-library'; import * as nock from 'nock'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; +// eslint-disable-next-line import/namespace import * as r from 'teeny-request'; import * as retryRequest from 'retry-request'; import * as sinon from 'sinon'; diff --git a/handwritten/bigquery/package.json b/handwritten/bigquery/package.json index a810796404c9..032cb571a879 100644 --- a/handwritten/bigquery/package.json +++ b/handwritten/bigquery/package.json @@ -36,7 +36,7 @@ "docs": "jsdoc -c .jsdoc.js", "lint": "gts check", "samples-test": "cd samples/ && pnpm link ../ && pnpm test && cd ../", - "test": "node ../../bin/run-test.cjs --proxyquire-shim build/test", + "test": "node ../../bin/run-test.cjs build/test", "system-test": "node ../../bin/run-test.cjs build/system-test --timeout 600000", "presystem-test": "pnpm run compile", "clean": "gts clean", diff --git a/handwritten/bigquery/test/bigquery.ts b/handwritten/bigquery/test/bigquery.ts index e5bbf0e222a1..72d0743ac9f6 100644 --- a/handwritten/bigquery/test/bigquery.ts +++ b/handwritten/bigquery/test/bigquery.ts @@ -24,7 +24,7 @@ import * as assert from 'assert'; import {describe, it, after, afterEach, before, beforeEach} from 'mocha'; import * as Big from 'big.js'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as sinon from 'sinon'; import * as crypto from 'crypto'; diff --git a/handwritten/bigquery/test/dataset.ts b/handwritten/bigquery/test/dataset.ts index 26bb3ff8a831..cdf1a4e321dd 100644 --- a/handwritten/bigquery/test/dataset.ts +++ b/handwritten/bigquery/test/dataset.ts @@ -22,7 +22,7 @@ import * as pfy from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it, before, beforeEach} from 'mocha'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as _root from '../src'; import {toArray} from '../src/util'; diff --git a/handwritten/bigquery/test/job.ts b/handwritten/bigquery/test/job.ts index afba50a3fc7a..ece2bc1d340e 100644 --- a/handwritten/bigquery/test/job.ts +++ b/handwritten/bigquery/test/job.ts @@ -16,7 +16,7 @@ import {DecorateRequestOptions, util} from '@google-cloud/common'; import * as pfy from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it, beforeEach, afterEach, before} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as sinon from 'sinon'; import {BigQuery} from '../src/bigquery'; diff --git a/handwritten/bigquery/test/model.ts b/handwritten/bigquery/test/model.ts index 49c59a5cec4b..b07ab763e644 100644 --- a/handwritten/bigquery/test/model.ts +++ b/handwritten/bigquery/test/model.ts @@ -15,7 +15,7 @@ import * as assert from 'assert'; import {describe, it, before, beforeEach, afterEach} from 'mocha'; import * as sinon from 'sinon'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as pfy from '@google-cloud/promisify'; import {EventEmitter} from 'events'; import {JobOptions} from '../src/job'; diff --git a/handwritten/bigquery/test/proxyquire.ts b/handwritten/bigquery/test/proxyquire.ts new file mode 100644 index 000000000000..44ff9f689c12 --- /dev/null +++ b/handwritten/bigquery/test/proxyquire.ts @@ -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; +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(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; diff --git a/handwritten/bigquery/test/routine.ts b/handwritten/bigquery/test/routine.ts index d1cdb0966ac4..dac2db92b637 100644 --- a/handwritten/bigquery/test/routine.ts +++ b/handwritten/bigquery/test/routine.ts @@ -17,7 +17,7 @@ import * as pfy from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it, before, beforeEach} from 'mocha'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as _root from '../src'; diff --git a/handwritten/bigquery/test/rowQueue.ts b/handwritten/bigquery/test/rowQueue.ts index f53dbf011485..11b0f8c971c0 100644 --- a/handwritten/bigquery/test/rowQueue.ts +++ b/handwritten/bigquery/test/rowQueue.ts @@ -16,7 +16,7 @@ import {util} from '@google-cloud/common'; import * as assert from 'assert'; import {describe, it, before, beforeEach, afterEach} from 'mocha'; import {Duplex, Stream} from 'stream'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as sinon from 'sinon'; import * as q from '../src/rowQueue'; import * as t from '../src/table'; diff --git a/handwritten/bigquery/test/table.ts b/handwritten/bigquery/test/table.ts index f3e59d7f3d9a..b17c506a65ed 100644 --- a/handwritten/bigquery/test/table.ts +++ b/handwritten/bigquery/test/table.ts @@ -26,7 +26,7 @@ import {describe, it, afterEach, beforeEach, before, after} from 'mocha'; import * as Big from 'big.js'; import {EventEmitter} from 'events'; import * as extend from 'extend'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as sinon from 'sinon'; import * as stream from 'stream'; import * as crypto from 'crypto'; diff --git a/handwritten/datastore/package.json b/handwritten/datastore/package.json index a031adee220d..7271a5d9d22f 100644 --- a/handwritten/datastore/package.json +++ b/handwritten/datastore/package.json @@ -38,7 +38,7 @@ "presystem-test": "npm run compile", "system-test": "node ../../bin/run-test.cjs build/system-test --timeout 600000", "pretest": "npm run compile", - "test": "node ../../bin/run-test.cjs --proxyquire-shim build/test", + "test": "node ../../bin/run-test.cjs build/test", "precompile": "gts clean" }, "dependencies": { diff --git a/handwritten/datastore/system-test/transaction-spy.ts b/handwritten/datastore/system-test/transaction-spy.ts index 23ea7a69524c..fb4763aa0ea8 100644 --- a/handwritten/datastore/system-test/transaction-spy.ts +++ b/handwritten/datastore/system-test/transaction-spy.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import * as proxyquire from 'proxyquire'; +import * as proxyquire from '../test/proxyquire'; import {DatastoreRequest} from '../src'; import {RequestCallback, RequestConfig} from '../src/request'; diff --git a/handwritten/datastore/test/gapic-mocks/client-initialization-testing.ts b/handwritten/datastore/test/gapic-mocks/client-initialization-testing.ts index a3a16ef7dc30..4c4809b69789 100644 --- a/handwritten/datastore/test/gapic-mocks/client-initialization-testing.ts +++ b/handwritten/datastore/test/gapic-mocks/client-initialization-testing.ts @@ -21,7 +21,7 @@ import { DatastoreOptions, } from '../../src'; import * as assert from 'assert'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from '../proxyquire'; import {Callback, CallOptions} from 'google-gax'; import * as protos from '../../src/protos'; import * as ds from '../../src'; diff --git a/handwritten/datastore/test/index-class.ts b/handwritten/datastore/test/index-class.ts index 3754e4672748..1f75cb436b38 100644 --- a/handwritten/datastore/test/index-class.ts +++ b/handwritten/datastore/test/index-class.ts @@ -15,7 +15,7 @@ import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; import {before, beforeEach, describe, it} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as ds from '../src'; diff --git a/handwritten/datastore/test/index.ts b/handwritten/datastore/test/index.ts index 03fedde213ce..8545834c59cb 100644 --- a/handwritten/datastore/test/index.ts +++ b/handwritten/datastore/test/index.ts @@ -15,7 +15,7 @@ import * as assert from 'assert'; import {before, beforeEach, after, afterEach, describe, it} from 'mocha'; import * as gax from 'google-gax'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import {PassThrough, Readable} from 'stream'; import * as ds from '../src'; diff --git a/handwritten/datastore/test/proxyquire.ts b/handwritten/datastore/test/proxyquire.ts new file mode 100644 index 000000000000..44ff9f689c12 --- /dev/null +++ b/handwritten/datastore/test/proxyquire.ts @@ -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; +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(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; diff --git a/handwritten/datastore/test/request.ts b/handwritten/datastore/test/request.ts index 97366be73bbe..1db6a3699caf 100644 --- a/handwritten/datastore/test/request.ts +++ b/handwritten/datastore/test/request.ts @@ -18,7 +18,7 @@ import {after, afterEach, before, beforeEach, describe, it} from 'mocha'; import * as extend from 'extend'; import * as gax from 'google-gax'; import * as is from 'is'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import * as sinon from 'sinon'; import {PassThrough, Transform} from 'stream'; diff --git a/handwritten/datastore/test/transaction.ts b/handwritten/datastore/test/transaction.ts index 36366c0d7e02..c322c5f4592f 100644 --- a/handwritten/datastore/test/transaction.ts +++ b/handwritten/datastore/test/transaction.ts @@ -16,7 +16,7 @@ import * as pfy from '@google-cloud/promisify'; import arrify = require('arrify'); import * as assert from 'assert'; import {afterEach, beforeEach, before, describe, it} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import { Datastore, DatastoreOptions, diff --git a/handwritten/google-cloud-dns/package.json b/handwritten/google-cloud-dns/package.json index 9a8744bc31b2..a58a630f0a2c 100644 --- a/handwritten/google-cloud-dns/package.json +++ b/handwritten/google-cloud-dns/package.json @@ -33,7 +33,7 @@ "scripts": { "docs": "jsdoc -c .jsdoc.js", "lint": "gts check", - "test": "node ../../bin/run-test.cjs --proxyquire-shim build/test", + "test": "node ../../bin/run-test.cjs build/test", "samples-test": "pnpm run compile && cd samples/ && pnpm link ../ && pnpm i && pnpm test", "presystem-test": "pnpm run compile", "system-test": "pnpm run compile && node ../../bin/run-test.cjs build/system-test", diff --git a/handwritten/google-cloud-dns/test/change.ts b/handwritten/google-cloud-dns/test/change.ts index 4d37cfd21cd6..7aaa61f9c0fc 100644 --- a/handwritten/google-cloud-dns/test/change.ts +++ b/handwritten/google-cloud-dns/test/change.ts @@ -20,7 +20,7 @@ import { import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it, before, beforeEach} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import {Change} from '../src/change'; let promisified = false; @@ -119,7 +119,7 @@ describe('Change', () => { assert.strictEqual(change, null); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -145,7 +145,7 @@ describe('Change', () => { assert.strictEqual(change_, change); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); diff --git a/handwritten/google-cloud-dns/test/index.ts b/handwritten/google-cloud-dns/test/index.ts index 0c3fd27da05e..1d89ead7255a 100644 --- a/handwritten/google-cloud-dns/test/index.ts +++ b/handwritten/google-cloud-dns/test/index.ts @@ -22,7 +22,7 @@ import * as promisify from '@google-cloud/promisify'; import arrify = require('arrify'); import * as assert from 'assert'; import {describe, it, before, beforeEach} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import {CoreOptions, OptionsWithUri, Response} from 'request'; import {Zone} from '../src'; @@ -135,7 +135,7 @@ describe('DNS', () => { assert.deepStrictEqual( calledWith.packageJson, // eslint-disable-next-line @typescript-eslint/no-var-requires - require('../../package.json') + require('../../package.json'), ); }); @@ -226,7 +226,7 @@ describe('DNS', () => { assert.strictEqual(zone, null); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -265,7 +265,7 @@ describe('DNS', () => { assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); @@ -318,7 +318,7 @@ describe('DNS', () => { err: Error, zones: Zone[], nextQuery: {}, - apiResponse_: Response + apiResponse_: Response, ) => { assert.strictEqual(err, error); assert.strictEqual(zones, null); @@ -326,7 +326,7 @@ describe('DNS', () => { assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -377,7 +377,7 @@ describe('DNS', () => { nextQuery, Object.assign({}, query, { pageToken: apiResponseWithNextPageToken.nextPageToken, - }) + }), ); done(); @@ -391,7 +391,7 @@ describe('DNS', () => { err: Error, zones: Zone[], nextQuery: {}, - apiResponse_: Response + apiResponse_: Response, ) => { assert.ifError(err); assert.strictEqual(zones[0], zone); @@ -399,7 +399,7 @@ describe('DNS', () => { assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); diff --git a/handwritten/google-cloud-dns/test/proxyquire.ts b/handwritten/google-cloud-dns/test/proxyquire.ts new file mode 100644 index 000000000000..44ff9f689c12 --- /dev/null +++ b/handwritten/google-cloud-dns/test/proxyquire.ts @@ -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; +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(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; diff --git a/handwritten/google-cloud-dns/test/record.ts b/handwritten/google-cloud-dns/test/record.ts index e7174c43d546..ffbba1ce547e 100644 --- a/handwritten/google-cloud-dns/test/record.ts +++ b/handwritten/google-cloud-dns/test/record.ts @@ -15,7 +15,7 @@ import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it, before, beforeEach} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import {Record} from '../src'; diff --git a/handwritten/google-cloud-dns/test/zone.ts b/handwritten/google-cloud-dns/test/zone.ts index 07681f51e591..c2a639b6d668 100644 --- a/handwritten/google-cloud-dns/test/zone.ts +++ b/handwritten/google-cloud-dns/test/zone.ts @@ -17,7 +17,7 @@ import * as promisify from '@google-cloud/promisify'; import arrify = require('arrify'); import * as assert from 'assert'; import {describe, it, before, beforeEach} from 'mocha'; -import * as proxyquire from 'proxyquire'; +import * as proxyquire from './proxyquire'; import {CoreOptions, OptionsWithUri, Response} from 'request'; import * as crypto from 'crypto'; @@ -187,7 +187,7 @@ describe('Zone', () => { zone.createChange = ( options: CreateChangeRequest, - callback: Function + callback: Function, ) => { assert.strictEqual(options.add, records); callback(); @@ -215,7 +215,7 @@ describe('Zone', () => { type: crypto.randomUUID(), rrdatas: [crypto.randomUUID(), crypto.randomUUID()], }, - recordJson + recordJson, ); return { @@ -312,7 +312,7 @@ describe('Zone', () => { assert.strictEqual(err, error); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -342,7 +342,7 @@ describe('Zone', () => { assert.strictEqual(change_.metadata, apiResponse); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -390,7 +390,7 @@ describe('Zone', () => { zone.createChange = ( options: CreateChangeRequest, - callback: Function + callback: Function, ) => { assert.deepStrictEqual(options.delete, [recordsToDelete]); callback(); @@ -462,7 +462,7 @@ describe('Zone', () => { it('should delete non-NS and non-SOA records', done => { zone.deleteRecords = ( recordsToDelete: string[], - callback: Function + callback: Function, ) => { assert.deepStrictEqual(recordsToDelete, expectedRecordsToDelete); callback(); @@ -539,7 +539,7 @@ describe('Zone', () => { writeFileOverride = ( path_: string, content: string, - encoding: string + encoding: string, ) => { assert.strictEqual(path_, path); assert.strictEqual(content, expectedZonefileContents); @@ -559,7 +559,7 @@ describe('Zone', () => { path: string, content: string, encoding: string, - callback: Function + callback: Function, ) => { callback(error); }; @@ -579,7 +579,7 @@ describe('Zone', () => { path: string, content: string, encoding: string, - callback: Function + callback: Function, ) => { callback(); }; @@ -648,12 +648,12 @@ describe('Zone', () => { err: Error, changes: Change[], nextQuery: {}, - apiResponse_: Response + apiResponse_: Response, ) => { assert.strictEqual(err, error); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -703,14 +703,14 @@ describe('Zone', () => { err: Error, changes: Change[], nextQuery: {}, - apiResponse_: Response + apiResponse_: Response, ) => { assert.ifError(err); assert.strictEqual(changes[0], change); assert.strictEqual(changes[0].metadata, apiResponse.changes[0]); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); }); @@ -734,12 +734,12 @@ describe('Zone', () => { err: Error, changes: Change[], nextQuery: {}, - apiResponse_: Response + apiResponse_: Response, ) => { assert.strictEqual(err, error); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); }); @@ -795,13 +795,13 @@ describe('Zone', () => { err: Error, records: Record[], nextQuery: {}, - apiResponse_: Response + apiResponse_: Response, ) => { assert.ifError(err); assert.strictEqual(records[0], record); assert.strictEqual(apiResponse_, apiResponse); done(); - } + }, ); it('should not require a query', done => { @@ -866,7 +866,7 @@ describe('Zone', () => { readFileOverride = ( path: string, encoding: string, - callback: Function + callback: Function, ) => { callback(error); }; @@ -897,7 +897,7 @@ describe('Zone', () => { readFileOverride = ( path: string, encoding: string, - callback: Function + callback: Function, ) => { callback(); }; @@ -906,7 +906,7 @@ describe('Zone', () => { it('should add records', done => { zone.addRecords = ( recordsToCreate: FakeRecord[], - callback: Function + callback: Function, ) => { assert.strictEqual(recordsToCreate.length, 1); const recordToCreate = recordsToCreate[0]; @@ -998,7 +998,7 @@ describe('Zone', () => { it('should create a change', done => { zone.createChange = ( options: CreateChangeRequest, - callback: Function + callback: Function, ) => { assert.strictEqual(options.add, recordsToCreate); assert.strictEqual(options.delete, recordsToDelete);