Skip to content
Draft
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
20 changes: 11 additions & 9 deletions handwritten/storage/test/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3053,11 +3053,18 @@ describe('Bucket', () => {
it('should destroy the local read stream if write stream fails', done => {
const fakeFile = new File(bucket, 'file-name');
const options = {destination: fakeFile, resumable: false};
const textfilepath = path.join(
getDirName(),
'../../../test/testdata/textfile.txt',
);
const originalCreateReadStream = fs.createReadStream;
let readStream: fs.ReadStream;
sandbox.stub(fs, 'createReadStream').callsFake((path, opts) => {
readStream = originalCreateReadStream(path, opts);
return readStream;
let readStream: fs.ReadStream | undefined;
sandbox.stub(fs, 'createReadStream').callsFake((filePath, opts) => {
const streamInstance = originalCreateReadStream(filePath, opts);
if (filePath === textfilepath) {
readStream = streamInstance;
}
return streamInstance;
});

fakeFile.createWriteStream = () => {
Expand All @@ -3069,11 +3076,6 @@ describe('Bucket', () => {
return ws;
};

const textfilepath = path.join(
getDirName(),
'../../../test/testdata/textfile.txt',
);

bucket.upload(textfilepath, options, (err: Error | null) => {
try {
assert.strictEqual(err!.message, 'write error');
Expand Down
53 changes: 28 additions & 25 deletions handwritten/storage/test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
StorageRequestOptions,
StorageTransport,
} from '../src/storage-transport.js';
import sinon, {createSandbox, stub, spy, restore, useFakeTimers} from 'sinon';
import sinon, {createSandbox} from 'sinon';
import {GoogleAuth} from 'google-auth-library';
import {
FileExceptionMessages,
Expand Down Expand Up @@ -1393,9 +1393,9 @@ describe('File', () => {
const rawResponseStream = new PassThrough();
const gunzipStream = new PassThrough();
createGunzipOverride = () => gunzipStream;
const destroySpy = spy(rawResponseStream, 'destroy');
const gunzipDestroySpy = spy(gunzipStream, 'destroy');
const pipelineSpy = spy();
const destroySpy = sandbox.spy(rawResponseStream, 'destroy');
const gunzipDestroySpy = sandbox.spy(gunzipStream, 'destroy');
const pipelineSpy = sandbox.spy();
pipelineOverride = pipelineSpy;

proxiedFile.storageTransport.makeRequest = sandbox
Expand Down Expand Up @@ -1426,8 +1426,8 @@ describe('File', () => {
it('should destroy rawResponseStream and return early if throughStream is closed', done => {
const proxiedFile = new ProxiedFile(BUCKET, FILE_NAME);
const rawResponseStream = new PassThrough();
const destroySpy = spy(rawResponseStream, 'destroy');
const pipelineSpy = spy();
const destroySpy = sandbox.spy(rawResponseStream, 'destroy');
const pipelineSpy = sandbox.spy();
pipelineOverride = pipelineSpy;

proxiedFile.storageTransport.makeRequest = sandbox
Expand Down Expand Up @@ -1458,8 +1458,8 @@ describe('File', () => {
it('should destroy rawResponseStream and return early if throughStream is already writableEnded', done => {
const proxiedFile = new ProxiedFile(BUCKET, FILE_NAME);
const rawResponseStream = new PassThrough();
const destroySpy = spy(rawResponseStream, 'destroy');
const pipelineSpy = spy();
const destroySpy = sandbox.spy(rawResponseStream, 'destroy');
const pipelineSpy = sandbox.spy();
pipelineOverride = pipelineSpy;

proxiedFile.storageTransport.makeRequest = sandbox
Expand Down Expand Up @@ -1495,8 +1495,8 @@ describe('File', () => {
const rawResponseStream = new PassThrough();
const gunzipStream = new PassThrough();
createGunzipOverride = () => gunzipStream;
const destroySpy = spy(rawResponseStream, 'destroy');
const gunzipDestroySpy = spy(gunzipStream, 'destroy');
const destroySpy = sandbox.spy(rawResponseStream, 'destroy');
const gunzipDestroySpy = sandbox.spy(gunzipStream, 'destroy');

pipelineOverride = (...args: unknown[]) => {
const onComplete = args[args.length - 1] as (
Expand Down Expand Up @@ -2076,24 +2076,27 @@ describe('File', () => {
idempotencyStrategy: IdempotencyStrategy.RetryConditional,
},
},
getRequestInterceptors: stub().returns([
(reqOpts: object) => ({...reqOpts, customOption: 'custom-value'}),
]),
getRequestInterceptors: sandbox
.stub()
.returns([
(reqOpts: object) => ({...reqOpts, customOption: 'custom-value'}),
]),
generation: 123,
encryptionKey: 'test-encryption-key',
kmsKeyName: 'test-kms-key-name',
userProject: 'test-user-project',
instancePreconditionOpts: {ifGenerationMatch: 123},
createResumableUpload: spy(),
createResumableUpload: sandbox.spy(),
};

resumableUploadStub = stub();
resumableUploadStub = sandbox.stub();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).resumableUpload = {createURI: resumableUploadStub};
});

afterEach(() => {
restore();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (global as any).resumableUpload;
});

it('should not require options', () => {
Expand Down Expand Up @@ -2745,7 +2748,7 @@ describe('File', () => {
};

originalSetEncryptionKey = file.setEncryptionKey;
file.setEncryptionKey = stub();
file.setEncryptionKey = sandbox.stub();
});

afterEach(() => {
Expand Down Expand Up @@ -3470,7 +3473,7 @@ describe('File', () => {
let BUCKET: any;

beforeEach(() => {
fakeTimer = useFakeTimers(NOW);
fakeTimer = sandbox.useFakeTimers(NOW);
CONFIG = {
expires: NOW.valueOf() + 2000,
};
Expand Down Expand Up @@ -5751,7 +5754,7 @@ describe('File', () => {
};

const resumableUpload = {
upload: stub().callsFake(opts => {
upload: sandbox.stub().callsFake(opts => {
const bucket = file.bucket;
const storage = bucket.storage;
const authClient = storage.storageTransport.authClient;
Expand Down Expand Up @@ -5780,7 +5783,7 @@ describe('File', () => {
const uploadStream = new PassThrough();

const resumableUpload = {
upload: stub().callsFake(() => {
upload: sandbox.stub().callsFake(() => {
uploadStream.emit('response', resp);
done();
return uploadStream;
Expand All @@ -5801,7 +5804,7 @@ describe('File', () => {
const metadata = {};
const uploadStream = new PassThrough();
const resumableUpload = {
upload: stub().callsFake(() => {
upload: sandbox.stub().callsFake(() => {
uploadStream.emit('metadata', metadata);
setImmediate(() => {
assert.deepStrictEqual(file.metadata, metadata);
Expand All @@ -5821,7 +5824,7 @@ describe('File', () => {
dup.on('complete', done);

const resumableUpload = {
upload: stub().callsFake(() => {
upload: sandbox.stub().callsFake(() => {
const uploadStream = new Transform();
setImmediate(() => {
uploadStream.end();
Expand All @@ -5845,7 +5848,7 @@ describe('File', () => {
};

const resumableUpload = {
upload: stub().callsFake(() => {
upload: sandbox.stub().callsFake(() => {
done();
return uploadStream;
}),
Expand All @@ -5864,7 +5867,7 @@ describe('File', () => {
});

const resumableUpload = {
upload: stub().callsFake(() => {
upload: sandbox.stub().callsFake(() => {
const uploadStream = new Transform();
setImmediate(() => {
uploadStream.emit('progress', progress);
Expand All @@ -5888,7 +5891,7 @@ describe('File', () => {
});

const resumableUpload = {
upload: stub().callsFake(() => {
upload: sandbox.stub().callsFake(() => {
done();
return uploadStream;
}),
Expand Down
6 changes: 3 additions & 3 deletions handwritten/storage/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ describe('Storage', () => {
describe('hmacKey', () => {
let hmacKeyCtor: sinon.SinonSpy;
beforeEach(() => {
hmacKeyCtor = sinon.spy(hmacKeyModule, 'HmacKey');
hmacKeyCtor = sandbox.spy(hmacKeyModule, 'HmacKey');
});

afterEach(() => {
Expand Down Expand Up @@ -499,7 +499,7 @@ describe('Storage', () => {

let hmacKeyCtor: sinon.SinonSpy;
beforeEach(() => {
hmacKeyCtor = sinon.spy(hmacKeyModule, 'HmacKey');
hmacKeyCtor = sandbox.spy(hmacKeyModule, 'HmacKey');
});

afterEach(() => {
Expand Down Expand Up @@ -1210,7 +1210,7 @@ describe('Storage', () => {

let hmacKeyCtor: sinon.SinonSpy;
beforeEach(() => {
hmacKeyCtor = sinon.spy(hmacKeyModule, 'HmacKey');
hmacKeyCtor = sandbox.spy(hmacKeyModule, 'HmacKey');
});

afterEach(() => {
Expand Down
12 changes: 9 additions & 3 deletions handwritten/storage/test/resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3089,14 +3089,20 @@ describe('resumable-upload', () => {
});

describe('exponential back off', () => {
let sandbox: sinon.SinonSandbox;
let clock: sinon.SinonFakeTimers;
let setTimeoutSpy: sinon.SinonSpy;
beforeEach(() => {
clock = sinon.useFakeTimers({toFake: ['setTimeout']});
setTimeoutSpy = sinon.spy(global, 'setTimeout');
sandbox = sinon.createSandbox();
clock = sandbox.useFakeTimers({toFake: ['setTimeout']});
setTimeoutSpy = sandbox.spy(global, 'setTimeout');
});
afterEach(() => {
clock.restore();
sandbox.restore();
});

it('should restore global setTimeout without leaking to the default sinon sandbox', async () => {
setImmediate(restoreStubs);
});

it('should continue uploading after retry count^2 * random', done => {
Expand Down
26 changes: 16 additions & 10 deletions handwritten/storage/test/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('signer', () => {
const NOW = new Date('2019-03-18T00:00:00.999Z');
let fakeTimers: sinon.SinonFakeTimers;

beforeEach(() => (fakeTimers = sinon.useFakeTimers(NOW)));
beforeEach(() => (fakeTimers = sandbox.useFakeTimers(NOW)));
afterEach(() => fakeTimers.restore());

beforeEach(() => {
Expand Down Expand Up @@ -813,11 +813,13 @@ describe('signer', () => {
'x-goog-content-sha256': '76af7efae0d034d1e3335ed1b90f24b6cadf2bf1',
};
CONFIG.file = 'file-name.png';
sinon.stub(signer, 'getCanonicalHeaders').returns('canonical-headers');
sinon
sandbox
.stub(signer, 'getCanonicalHeaders')
.returns('canonical-headers');
sandbox
.stub(signer, 'getCanonicalQueryParams')
.returns('canonical-query');
const getCanonicalRequest = sinon.spy(signer, 'getCanonicalRequest');
const getCanonicalRequest = sandbox.spy(signer, 'getCanonicalRequest');

await signer['getSignedUrlV4'](CONFIG);
const args = getCanonicalRequest.getCall(0).args;
Expand All @@ -831,8 +833,10 @@ describe('signer', () => {
});

it('should compute SHA256 digest in hex on canonical request', async () => {
sinon.stub(signer, 'getCanonicalRequest').returns('canonical-request');
const authClientSign = sinon
sandbox
.stub(signer, 'getCanonicalRequest')
.returns('canonical-request');
const authClientSign = sandbox
.stub(authClient, 'sign')
.resolves('signature');

Expand All @@ -850,8 +854,10 @@ describe('signer', () => {
it('should sign using the `signingEndpoint` when provided', async () => {
const signingEndpoint = 'https://my-endpoint.com';

sinon.stub(signer, 'getCanonicalRequest').returns('canonical-request');
const authClientSign = sinon
sandbox
.stub(signer, 'getCanonicalRequest')
.returns('canonical-request');
const authClientSign = sandbox
.stub(authClient, 'sign')
.resolves('signature');

Expand All @@ -871,7 +877,7 @@ describe('signer', () => {
const credentialScope = `${datestamp}/auto/storage/goog4_request`;
const dateISO = formatAsUTCISO(NOW, true);

const authClientSign = sinon
const authClientSign = sandbox
.stub(authClient, 'sign')
.resolves('signature');

Expand All @@ -888,7 +894,7 @@ describe('signer', () => {
it('rejects with SigningError on signing Error', async () => {
const err = new Error('my-err');
err.stack = 'some-stack-trace';
sinon.stub(authClient, 'sign').rejects(err);
sandbox.stub(authClient, 'sign').rejects(err);

await assert.rejects(() => signer['getSignedUrlV4'](CONFIG), {
name: 'SigningError',
Expand Down
2 changes: 1 addition & 1 deletion handwritten/storage/test/transfer-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {DownloadResponseWithStatus, SkipReason} from '../src/file.js';

describe('Transfer Manager', () => {
const BUCKET_NAME = 'test-bucket';
const STORAGE = sinon.stub(
const STORAGE = sinon.createSandbox().stub(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating an isolated sandbox with sinon.createSandbox() is a great way to prevent test pollution. However, since this sandbox is never restored, it can lead to resource leaks in persistent test runner environments (such as watch mode). Consider declaring the sandbox at the suite level and restoring it in an after hook.

  const transferManagerSandbox = sinon.createSandbox();
  const STORAGE = transferManagerSandbox.stub(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Defining a stubbed STORAGE instance at the module level using sinon.createSandbox().stub(...) without ever restoring it is an anti-pattern. While it isolates the stubs from global sinon.restore() calls, sharing a single stubbed instance across all tests in the file can lead to test pollution if any test modifies the stub's behavior.

Consider instantiating and stubbing STORAGE within a before or beforeEach hook, and restoring the sandbox in after or afterEach to ensure clean test isolation.

new Storage({
retryOptions: {
autoRetry: true,
Expand Down
Loading