Skip to content

Commit 82f627d

Browse files
committed
build: create common getStubs function
1 parent 75421d8 commit 82f627d

9 files changed

+59
-176
lines changed

samples/test/common.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
const {google} = require('googleapis');
17+
18+
function getStubs() {
19+
return {
20+
'googleapis': {
21+
google: {
22+
...google,
23+
options: () => {},
24+
auth: {
25+
...google.auth,
26+
GoogleAuth: class {
27+
constructor() {
28+
return {
29+
getClient: async () => {
30+
const client = new google.auth.OAuth2();
31+
client.credentials = {access_token: 'not-a-token'};
32+
return client;
33+
}
34+
}
35+
}
36+
},
37+
},
38+
}
39+
}
40+
};
41+
}
42+
43+
module.exports = { getStubs };

samples/test/test.samples.analyticsReporting.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,13 @@ const {describe, it, afterEach} = require('mocha');
1818
const nock = require('nock');
1919
const proxyquire = require('proxyquire');
2020
const {google} = require('googleapis');
21+
const {getStubs} = require('./common.js');
2122

2223
nock.disableNetConnect();
2324

2425
const baseUrl = 'https://analyticsreporting.googleapis.com';
2526

26-
const stubs = {
27-
'googleapis': {
28-
google: {
29-
...google,
30-
options: () => {},
31-
auth: {
32-
...google.auth,
33-
GoogleAuth: class {
34-
constructor() {
35-
return {
36-
getClient: async () => {
37-
const client = new google.auth.OAuth2();
38-
client.credentials = {access_token: 'not-a-token'};
39-
return client;
40-
}
41-
}
42-
}
43-
},
44-
},
45-
}
46-
}
47-
};
27+
const stubs = getStubs();
4828

4929
describe('analyticsReporting samples', () => {
5030
const batchGet = proxyquire('../analyticsReporting/batchGet', stubs);

samples/test/test.samples.blog.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,13 @@ const nock = require('nock');
1818
const {describe, it, afterEach} = require('mocha');
1919
const proxyquire = require('proxyquire');
2020
const {google} = require('googleapis');
21+
const {getStubs} = require('./common.js');
2122

2223
nock.disableNetConnect();
2324

2425
const baseUrl = 'https://blogger.googleapis.com';
2526

26-
const stubs = {
27-
'googleapis': {
28-
google: {
29-
...google,
30-
options: () => {},
31-
auth: {
32-
...google.auth,
33-
GoogleAuth: class {
34-
constructor() {
35-
return {
36-
getClient: async () => {
37-
const client = new google.auth.OAuth2();
38-
client.credentials = {access_token: 'not-a-token'};
39-
return client;
40-
}
41-
}
42-
}
43-
},
44-
},
45-
}
46-
}
47-
};
27+
const stubs = getStubs();
4828

4929
describe('blogger samples', () => {
5030
const insert = proxyquire('../blogger/insert', stubs);

samples/test/test.samples.docs.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const assert = require('assert');
1717
const nock = require('nock');
1818
const {describe, it, afterEach} = require('mocha');
1919
const {google} = require('googleapis');
20+
const {getStubs} = require('./common.js');
2021
const proxyquire = require('proxyquire');
2122

2223
nock.disableNetConnect();
@@ -28,28 +29,7 @@ const samples = {
2829

2930
const baseUrl = 'https://docs.googleapis.com';
3031

31-
const stubs = {
32-
'googleapis': {
33-
google: {
34-
...google,
35-
options: () => {},
36-
auth: {
37-
...google.auth,
38-
GoogleAuth: class {
39-
constructor() {
40-
return {
41-
getClient: async () => {
42-
const client = new google.auth.OAuth2();
43-
client.credentials = {access_token: 'not-a-token'};
44-
return client;
45-
}
46-
}
47-
}
48-
},
49-
},
50-
}
51-
}
52-
};
32+
const stubs = getStubs();
5333

5434
for (const sample of Object.values(samples)) {
5535
sample.runSample = proxyquire(sample.path, stubs);

samples/test/test.samples.drive.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const path = require('path');
2121
const {describe, it, afterEach} = require('mocha');
2222
const proxyquire = require('proxyquire');
2323
const {google} = require('googleapis');
24+
const {getStubs} = require('./common.js');
2425

2526
nock.disableNetConnect();
2627

@@ -32,28 +33,7 @@ const samples = {
3233
quickstart: {path: '../drive/quickstart'},
3334
};
3435

35-
const stubs = {
36-
'googleapis': {
37-
google: {
38-
...google,
39-
options: () => {},
40-
auth: {
41-
...google.auth,
42-
GoogleAuth: class {
43-
constructor() {
44-
return {
45-
getClient: async () => {
46-
const client = new google.auth.OAuth2();
47-
client.credentials = {access_token: 'not-a-token'};
48-
return client;
49-
}
50-
}
51-
}
52-
},
53-
},
54-
}
55-
}
56-
};
36+
const stubs = getStubs();
5737

5838
for (const sample of Object.values(samples)) {
5939
sample.runSample = proxyquire(sample.path, stubs);

samples/test/test.samples.gmail.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const nock = require('nock');
1818
const {describe, it, afterEach} = require('mocha');
1919
const proxyquire = require('proxyquire');
2020
const {google} = require('googleapis');
21+
const {getStubs} = require('./common.js');
2122

2223
nock.disableNetConnect();
2324

@@ -28,28 +29,7 @@ const samples = {
2829
send: {path: '../gmail/send'},
2930
};
3031

31-
const stubs = {
32-
'googleapis': {
33-
google: {
34-
...google,
35-
options: () => {},
36-
auth: {
37-
...google.auth,
38-
GoogleAuth: class {
39-
constructor() {
40-
return {
41-
getClient: async () => {
42-
const client = new google.auth.OAuth2();
43-
client.credentials = {access_token: 'not-a-token'};
44-
return client;
45-
}
46-
}
47-
}
48-
},
49-
},
50-
}
51-
}
52-
};
32+
const stubs = getStubs();
5333

5434
for (const sample of Object.values(samples)) {
5535
sample.runSample = proxyquire(sample.path, stubs);

samples/test/test.samples.sheets.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const nock = require('nock');
1818
const {describe, it, afterEach} = require('mocha');
1919
const proxyquire = require('proxyquire');
2020
const {google} = require('googleapis');
21+
const {getStubs} = require('./common.js');
2122

2223
const baseUrl = 'https://sheets.googleapis.com';
2324

@@ -28,28 +29,7 @@ const samples = {
2829
'insert-column': {path: '../sheets/insert-column'},
2930
};
3031

31-
const stubs = {
32-
'googleapis': {
33-
google: {
34-
...google,
35-
options: () => {},
36-
auth: {
37-
...google.auth,
38-
GoogleAuth: class {
39-
constructor() {
40-
return {
41-
getClient: async () => {
42-
const client = new google.auth.OAuth2();
43-
client.credentials = {access_token: 'not-a-token'};
44-
return client;
45-
}
46-
}
47-
}
48-
},
49-
},
50-
}
51-
}
52-
};
32+
const stubs = getStubs();
5333

5434
for (const sample of Object.values(samples)) {
5535
sample.runSample = proxyquire(sample.path, stubs);

samples/test/test.samples.webmasters.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,15 @@ const nock = require('nock');
1818
const {describe, it, afterEach} = require('mocha');
1919
const proxyquire = require('proxyquire');
2020
const {google} = require('googleapis');
21+
const {getStubs} = require('./common.js');
2122

2223
nock.disableNetConnect();
2324

2425
const samples = {
2526
query: {path: '../webmasters/query'},
2627
};
2728

28-
const stubs = {
29-
'googleapis': {
30-
google: {
31-
...google,
32-
options: () => {},
33-
auth: {
34-
...google.auth,
35-
GoogleAuth: class {
36-
constructor() {
37-
return {
38-
getClient: async () => {
39-
const client = new google.auth.OAuth2();
40-
client.credentials = {access_token: 'not-a-token'};
41-
return client;
42-
}
43-
}
44-
}
45-
},
46-
},
47-
}
48-
}
49-
};
29+
const stubs = getStubs();
5030

5131
for (const sample of Object.values(samples)) {
5232
sample.runSample = proxyquire(sample.path, stubs);

samples/test/test.samples.youtube.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const nock = require('nock');
1919
const path = require('path');
2020
const proxyquire = require('proxyquire');
2121
const {google} = require('googleapis');
22+
const {getStubs} = require('./common.js');
2223

2324
nock.disableNetConnect();
2425

@@ -28,28 +29,7 @@ const samples = {
2829
search: {path: '../youtube/search'},
2930
};
3031

31-
const stubs = {
32-
'googleapis': {
33-
google: {
34-
...google,
35-
options: () => {},
36-
auth: {
37-
...google.auth,
38-
GoogleAuth: class {
39-
constructor() {
40-
return {
41-
getClient: async () => {
42-
const client = new google.auth.OAuth2();
43-
client.credentials = {access_token: 'not-a-token'};
44-
return client;
45-
}
46-
}
47-
}
48-
},
49-
},
50-
}
51-
}
52-
};
32+
const stubs = getStubs();
5333

5434
for (const sample of Object.values(samples)) {
5535
sample.runSample = proxyquire(sample.path, stubs);

0 commit comments

Comments
 (0)