Skip to content

Commit

Permalink
chore: use sinon to mock in some cases (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Feb 26, 2018
1 parent faf494a commit 1848552
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 75 deletions.
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/nock": "^9.1.2",
"@types/node": "^9.4.6",
"@types/pify": "^3.0.0",
"@types/sinon": "^4.1.3",
"@types/tmp": "0.0.33",
"clang-format": "^1.2.2",
"codecov": "^3.0.0",
Expand All @@ -52,6 +53,7 @@
"opn": "^5.2.0",
"pify": "^3.0.0",
"prettier": "^1.10.2",
"sinon": "^4.4.2",
"source-map-support": "^0.5.3",
"tmp": "0.0.33",
"typescript": "~2.7.0"
Expand Down
65 changes: 24 additions & 41 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,16 @@ export class GoogleAuth {
}

private getDefaultProjectIdAsync(): Promise<string|null> {
// In implicit case, supports three environments. In order of precedence,
// the implicit environments are:
//
// * GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variable
// * GOOGLE_APPLICATION_CREDENTIALS JSON file
// * Get default service project from
// ``$ gcloud beta auth application-default login``
// * Google App Engine application ID (Not implemented yet)
// * Google Compute Engine project ID (from metadata server) (Not
// implemented yet)

if (this._cachedProjectId) {
return Promise.resolve(this._cachedProjectId);
}

// In implicit case, supports three environments. In order of precedence,
// the implicit environments are:
// - GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variable
// - GOOGLE_APPLICATION_CREDENTIALS JSON file
// - Cloud SDK: `gcloud config config-helper --format json`
// - GCE project ID from metadata server)
if (!this._getDefaultProjectIdPromise) {
this._getDefaultProjectIdPromise =
new Promise(async (resolve, reject) => {
Expand All @@ -179,25 +174,6 @@ export class GoogleAuth {
return this._getDefaultProjectIdPromise;
}

/**
* Run the Google Cloud SDK command that prints the default project ID
*/
_getSDKDefaultProjectId():
Promise<{stdout: string | null, stderr: string|null}> {
// TODO: make this a proper async function
return new Promise((resolve, reject) => {
exec(
'gcloud config config-helper --format json',
(err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({stdout, stderr});
}
});
});
}

/**
* Obtains the default service-level credentials for the application.
* @param callback Optional callback.
Expand Down Expand Up @@ -555,19 +531,26 @@ export class GoogleAuth {
}

/**
* Loads the default project of the Google Cloud SDK.
* @api private
* Run the Google Cloud SDK command that prints the default project ID
*/
private async getDefaultServiceProjectId(): Promise<string|null> {
try {
const r = await this._getSDKDefaultProjectId();
if (r.stdout) {
return JSON.parse(r.stdout).configuration.properties.core.project;
}
} catch (e) {
// Ignore any errors
}
return null;
return new Promise<string|null>(resolve => {
exec(
'gcloud config config-helper --format json',
(err, stdout, stderr) => {
if (!err && stdout) {
try {
const projectId =
JSON.parse(stdout).configuration.properties.core.project;
resolve(projectId);
return;
} catch (e) {
// ignore errors
}
}
resolve(null);
});
});
}

/**
Expand Down
Loading

0 comments on commit 1848552

Please sign in to comment.