Skip to content

Commit

Permalink
refactor: use sinon for mocking fs operations (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Apr 11, 2019
1 parent 9648fac commit b11841e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 296 deletions.
43 changes: 4 additions & 39 deletions src/auth/googleauth.ts
Expand Up @@ -332,17 +332,14 @@ export class GoogleAuth {
// Linux or Mac
const home = process.env['HOME'];
if (home) {
location = this._pathJoin(home, '.config');
location = path.join(home, '.config');
}
}
// If we found the root path, expand it.
if (location) {
location = this._pathJoin(location, 'gcloud');
location =
this._pathJoin(location, 'application_default_credentials.json');
location = this._mockWellKnownFilePath(location);
// Check whether the file exists.
if (!this._fileExists(location)) {
path.join(location, 'gcloud', 'application_default_credentials.json');
if (!fs.existsSync(location)) {
location = null;
}
}
Expand Down Expand Up @@ -388,7 +385,7 @@ export class GoogleAuth {
}

// Now open a read stream on the file, and parse it.
const readStream = this._createReadStream(filePath);
const readStream = fs.createReadStream(filePath);
return this.fromStream(readStream, options);
}

Expand Down Expand Up @@ -508,38 +505,6 @@ export class GoogleAuth {
return false;
}

/**
* Creates a file stream. Allows mocking.
* @api private
*/
_createReadStream(filePath: string) {
return fs.createReadStream(filePath);
}

/**
* Determines whether a file exists. Allows mocking.
* @api private
*/
_fileExists(filePath: string) {
return fs.existsSync(filePath);
}

/**
* Joins two parts of a path. Allows mocking.
* @api private
*/
_pathJoin(item1: string, item2: string) {
return path.join(item1, item2);
}

/**
* Allows mocking of the path to a well-known file.
* @api private
*/
_mockWellKnownFilePath(filePath: string) {
return filePath;
}

/**
* Run the Google Cloud SDK command that prints the default project ID
*/
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/wellKnown.json
@@ -0,0 +1,6 @@
{
"client_id": "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com",
"client_secret": "privatekey",
"refresh_token": "refreshtoken",
"type": "authorized_user"
}

0 comments on commit b11841e

Please sign in to comment.