From 5caaa0a7d5538ee934f5c8f5a4ad30b7a2e82d85 Mon Sep 17 00:00:00 2001 From: Rich Hodgkins Date: Sat, 28 Mar 2020 12:08:57 +0000 Subject: [PATCH] test(bucket): failing test for #1133 --- test/bucket.ts | 81 +++++++++++++++++++++++----- test/testdata/dummylargetestfile.txt | 3 ++ 2 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 test/testdata/dummylargetestfile.txt diff --git a/test/bucket.ts b/test/bucket.ts index ce630dddad..be5b0135a7 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -2381,19 +2381,74 @@ describe('Bucket', () => { bucket.upload(textFilepath, options, assert.ifError); }); - it('should force a resumable upload', done => { - const fakeFile = new FakeFile(bucket, 'file-name'); - const options = {destination: fakeFile, resumable: true}; - fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => { - const ws = new stream.Writable(); - ws.write = () => true; - setImmediate(() => { - assert.strictEqual(options_.resumable, options.resumable); - done(); - }); - return ws; - }; - bucket.upload(filepath, options, assert.ifError); + describe('resumable uploads', () => { + const dummyLargeFilepath = path.join( + __dirname, + '../../test/testdata', + 'dummylargetestfile.txt' + ); + const dummyLargeFileStat = require('fs').statSync(dummyLargeFilepath); + // Set size greater than threshold + dummyLargeFileStat.size = 5000001; + + let sandbox: sinon.SinonSandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + + const statStub = sandbox.stub(require('fs'), 'stat'); + statStub + .withArgs(dummyLargeFilepath) + .callsArgWithAsync(1, null, dummyLargeFileStat); + statStub.callThrough(); + }); + + afterEach(() => sandbox.restore()); + + it('should force a resumable upload', done => { + const fakeFile = new FakeFile(bucket, 'file-name'); + const options = {destination: fakeFile, resumable: true}; + fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => { + const ws = new stream.Writable(); + ws.write = () => true; + setImmediate(() => { + assert.strictEqual(options_.resumable, options.resumable); + done(); + }); + return ws; + }; + bucket.upload(filepath, options, assert.ifError); + }); + + it('should not pass resumable option to createWriteStream when file size is greater than minimum resumable threshold', done => { + const fakeFile = new FakeFile(bucket, 'file-name'); + const options = {destination: fakeFile}; + fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => { + const ws = new stream.Writable(); + ws.write = () => true; + setImmediate(() => { + assert.ok(!('resumable' in options_)); + done(); + }); + return ws; + }; + bucket.upload(dummyLargeFilepath, options, assert.ifError); + }); + + it('should prevent resumable when file size is less than minimum resumable threshold', done => { + const fakeFile = new FakeFile(bucket, 'file-name'); + const options = {destination: fakeFile}; + fakeFile.createWriteStream = (options_: CreateWriteStreamOptions) => { + const ws = new stream.Writable(); + ws.write = () => true; + setImmediate(() => { + assert.strictEqual(options_.resumable, false); + done(); + }); + return ws; + }; + bucket.upload(filepath, options, assert.ifError); + }); }); it('should allow overriding content type', done => { diff --git a/test/testdata/dummylargetestfile.txt b/test/testdata/dummylargetestfile.txt new file mode 100644 index 0000000000..c4d4623242 --- /dev/null +++ b/test/testdata/dummylargetestfile.txt @@ -0,0 +1,3 @@ +This is a test file! + +When using fs.stat should be stubbed to return a large file size