1717'use strict' ;
1818
1919import * as assert from 'assert' ;
20- import * as async from 'async' ;
21- const exec = require ( 'methmeth' ) ;
22- import { GoogleAuth } from 'google-auth-library' ;
2320import * as uuid from 'uuid' ;
24-
2521import { Resource , Project } from '../src' ;
2622import { Operation } from '@google-cloud/common' ;
2723
24+ if ( ! process . env . GCLOUD_PROJECT ||
25+ ! process . env . GOOGLE_APPLICATION_CREDENTIALS ) {
26+ throw new Error (
27+ 'the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIAL environment variables must be set to run the system tests 👻' ) ;
28+ }
29+
2830describe ( 'Resource' , ( ) => {
2931 const PREFIX = 'gcloud-tests-' ;
3032 const resource = new Resource ( ) ;
@@ -41,13 +43,9 @@ describe('Resource', () => {
4143
4244 it ( 'should get a list of projects in stream mode' , done => {
4345 let resultsMatched = 0 ;
44-
4546 resource . getProjectsStream ( )
4647 . on ( 'error' , done )
47- . on ( 'data' ,
48- ( ) => {
49- resultsMatched ++ ;
50- } )
48+ . on ( 'data' , ( ) => resultsMatched ++ )
5149 . on ( 'end' , ( ) => {
5250 assert ( resultsMatched > 0 ) ;
5351 done ( ) ;
@@ -72,65 +70,25 @@ describe('Resource', () => {
7270 // - Restore a project
7371 // - Delete a project
7472 describe ( 'lifecycle' , ( ) => {
75- let CAN_RUN_TESTS = true ;
7673 const testProjects : Project [ ] = [ ] ;
77-
7874 const resource = new Resource ( ) ;
79-
8075 const project = resource . project ( generateName ( 'project' ) ) ;
8176
82- before ( done => {
83- const authClient = new GoogleAuth ( ) ;
84-
85- async . series (
86- [
87- ( callback ) => {
88- // See if an auth token exists.
89- authClient . getAccessToken ( )
90- . then ( ( ) => {
91- CAN_RUN_TESTS = true ;
92- callback ( ) ;
93- } )
94- . catch ( e => {
95- CAN_RUN_TESTS = e === null ;
96- callback ( ) ;
97- } ) ;
98- } ,
99- deleteTestProjects ,
100- ] ,
101- ( err ) => {
102- if ( err || ! CAN_RUN_TESTS ) {
103- done ( err ) ;
104- return ;
105- }
106-
107- project . create (
108- ( err : Error , project : Project , operation : Operation ) => {
109- if ( err ) {
110- done ( err ) ;
111- return ;
112- }
113- testProjects . push ( project ) ;
114- operation . on ( 'error' , done ) . on ( 'complete' , ( ) => {
115- done ( ) ;
116- } ) ;
117- } ) ;
118- } ) ;
77+ before ( async ( ) => {
78+ await deleteTestProjects ( ) ;
79+ // TODO(beckwith): The TypeScript types for `create` here aren't correct.
80+ // This should return [Project, Operation, Response], but the default
81+ // signature for `ServiceObject.create` doesn't match. The fix is to
82+ // create an overridden create method on the `Project` object.
83+ // https://github.com/googleapis/nodejs-resource/issues/91
84+ const res = await project . create ( ) ;
85+ testProjects . push ( res [ 0 ] as Project ) ;
86+ return new Promise ( ( resolve , reject ) => {
87+ ( res [ 1 ] as { } as Operation ) . on ( 'error' , reject ) . on ( 'complete' , resolve ) ;
88+ } ) ;
11989 } ) ;
12090
121- beforeEach ( function ( ) {
122- if ( ! CAN_RUN_TESTS ) {
123- this . skip ( ) ;
124- }
125- } ) ;
126-
127- after ( function ( done ) {
128- if ( ! CAN_RUN_TESTS ) {
129- this . skip ( ) ;
130- return ;
131- }
132- deleteTestProjects ( done ) ;
133- } ) ;
91+ after ( async ( ) => deleteTestProjects ( ) ) ;
13492
13593 it ( 'should have created the project' , done => {
13694 project . getMetadata ( ( err , metadata ) => {
@@ -178,33 +136,15 @@ describe('Resource', () => {
178136 } ) ;
179137 } ) ;
180138
181- function deleteTestProjects ( callback : ( err ?: Error ) => void ) {
182- if ( ! CAN_RUN_TESTS ) {
183- callback ( ) ;
184- return ;
185- }
186- async . series (
187- [
188- callback => {
189- async . eachSeries ( testProjects , exec ( 'delete' ) , callback ) ;
190- } ,
191- callback => {
192- resource . getProjects ( ( err , projects ) => {
193- if ( err ) {
194- callback ( err ) ;
195- return ;
196- }
197- const projectsToDelete = projects ! . filter ( project => {
198- const isTestProject = project . id ! . indexOf ( PREFIX ) === 0 ;
199- const deleted =
200- project . metadata . lifecycleState === 'DELETE_REQUESTED' ;
201- return isTestProject && ! deleted ;
202- } ) ;
203- async . each ( projectsToDelete , exec ( 'delete' ) , callback ) ;
204- } ) ;
205- } ,
206- ] ,
207- callback ) ;
139+ async function deleteTestProjects ( ) {
140+ await Promise . all ( testProjects . map ( x => x . delete ( ) ) ) ;
141+ const [ projects ] = await resource . getProjects ( ) ;
142+ const projectsToDelete = projects . filter ( project => {
143+ const isTestProject = project . id ! . indexOf ( PREFIX ) === 0 ;
144+ const deleted = project . metadata . lifecycleState === 'DELETE_REQUESTED' ;
145+ return isTestProject && ! deleted ;
146+ } ) ;
147+ await Promise . all ( projectsToDelete . map ( x => x . delete ( ) ) ) ;
208148 }
209149
210150 function generateName ( resourceType : string ) {
0 commit comments