Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/packages/gax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
35 changes: 17 additions & 18 deletions core/packages/gax/test/unit/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
);
});
Expand Down
2 changes: 1 addition & 1 deletion core/packages/nodejs-googleapis-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 12 additions & 13 deletions core/packages/nodejs-googleapis-common/test/test.authplus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
});
21 changes: 11 additions & 10 deletions core/packages/nodejs-googleapis-common/test/test.http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

Expand Down
2 changes: 1 addition & 1 deletion handwritten/error-reporting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 31 additions & 25 deletions handwritten/error-reporting/test/unit/google-apis/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,31 @@
// 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';
Comment thread
danieljbruce marked this conversation as resolved.

import {
Configuration,
ConfigurationOptions,
Logger,
} from '../../../src/configuration';
import {RequestHandler} from '../../../src/google-apis/auth-client';
import {deepStrictEqual} from '../../util';

const origMakeAuthFactory = util.makeAuthenticatedRequestFactory;
const origRequest = RequestHandler.prototype.request;

function restoreStubs() {
util.makeAuthenticatedRequestFactory = origMakeAuthFactory;
RequestHandler.prototype.request = origRequest;
}

function verifyReportedMessage(
config1: ConfigurationOptions,
errToReturn: Error | null | undefined,
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) {
Expand All @@ -72,9 +59,27 @@ function verifyReportedMessage(
},
} as {} as Logger;
const config2 = new Configuration(config1, logger);
// tslint:disable-next-line:no-unused-expression
new RequestHandler(config2, logger);
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);
} catch (err) {
restoreStubs();
throw err;
}
setImmediate(() => {
restoreStubs();
deepStrictEqual(logs, expectedLogs);
done();
});
Expand All @@ -86,6 +91,7 @@ describe('RequestHandler', () => {
});

afterEach(() => {
restoreStubs();
if (nodeEnv === undefined) {
delete process.env.NODE_ENV;
} else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
},
"pnpm": {
"overrides": {
"gcp-metadata>gaxios": "^8.3.0",
"google-auth-library>gaxios": "^8.3.0",
"gcp-metadata@>=8.0.0>gaxios": "^8.3.0",
"google-auth-library@>=10.0.0>gaxios": "^8.3.0",
"yargs": "^17.7.2"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/google-cloud-translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Loading