1414 * limitations under the License.
1515 */
1616
17- import * as assert from 'assert' ;
18- import * as path from 'path' ;
17+ import * as check from 'post-install-check' ;
1918
20- import { globP , mkdirP , ncpP , rimrafP , spawnP , tmpDirP , writeFileP } from './utils' ;
21-
22- const INDEX_TS = 'index.ts' ;
23- const INDEX_JS = 'index.js' ;
24-
25- const TS_CODE_ARRAY : CodeSample [ ] = [
19+ const TS_CODE_SAMPLES : check . CodeSample [ ] = [
2620 {
2721 code : `import * as loggingWinston from '@google-cloud/logging-winston';
2822new loggingWinston.LoggingWinston();` ,
29- description : 'imports the module using * syntax'
23+ description : 'imports the module using * syntax' ,
24+ dependencies : [ 'winston@2' ] ,
25+ devDependencies : [ '@types/winston@2' ]
3026 } ,
3127 {
3228 code : `import {LoggingWinston} from '@google-cloud/logging-winston';
3329new LoggingWinston();` ,
34- description : 'imports the module with {} syntax'
30+ description : 'imports the module with {} syntax' ,
31+ dependencies : [ 'winston@2' ] ,
32+ devDependencies : [ '@types/winston@2' ]
3533 } ,
3634 {
3735 code : `import {LoggingWinston} from '@google-cloud/logging-winston';
@@ -40,7 +38,10 @@ new LoggingWinston({
4038 service: 'some service'
4139 }
4240});` ,
43- description : 'imports the module and starts with a partial `serviceContext`'
41+ description :
42+ 'imports the module and starts with a partial `serviceContext`' ,
43+ dependencies : [ 'winston@2' ] ,
44+ devDependencies : [ '@types/winston@2' ]
4445 } ,
4546 {
4647 code : `import {LoggingWinston} from '@google-cloud/logging-winston';
@@ -52,7 +53,9 @@ new LoggingWinston({
5253 }
5354});` ,
5455 description :
55- 'imports the module and starts with a complete `serviceContext`'
56+ 'imports the module and starts with a complete `serviceContext`' ,
57+ dependencies : [ 'winston@2' ] ,
58+ devDependencies : [ '@types/winston@2' ]
5659 } ,
5760 {
5861 code : `import {LoggingWinston} from '@google-cloud/logging-winston';
@@ -64,16 +67,20 @@ new LoggingWinston({
6467 version: 'some-version'
6568 }
6669});` ,
67- description : 'imports the module with a prefix and labels specified'
70+ description : 'imports the module with a prefix and labels specified' ,
71+ dependencies : [ 'winston@2' ] ,
72+ devDependencies : [ '@types/winston@2' ]
6873 }
6974] ;
7075
71- const JS_CODE_ARRAY : CodeSample [ ] = [
76+ const JS_CODE_SAMPLES : check . CodeSample [ ] = [
7277 {
7378 code :
7479 `const LoggingWinston = require('@google-cloud/logging-winston').LoggingWinston;
7580new LoggingWinston();` ,
76- description : 'requires the module using Node 4+ syntax'
81+ description : 'requires the module using Node 4+ syntax' ,
82+ dependencies : [ 'winston@2' ] ,
83+ devDependencies : [ ]
7784 } ,
7885 {
7986 code :
@@ -84,7 +91,9 @@ new LoggingWinston({
8491 }
8592});` ,
8693 description :
87- 'requires the module and starts with a partial `serviceContext`'
94+ 'requires the module and starts with a partial `serviceContext`' ,
95+ dependencies : [ 'winston@2' ] ,
96+ devDependencies : [ ]
8897 } ,
8998 {
9099 code :
@@ -97,7 +106,9 @@ new LoggingWinston({
97106 }
98107});` ,
99108 description :
100- 'requires the module and starts with a complete `serviceContext`'
109+ 'requires the module and starts with a complete `serviceContext`' ,
110+ dependencies : [ 'winston@2' ] ,
111+ devDependencies : [ ]
101112 } ,
102113 {
103114 code :
@@ -110,112 +121,11 @@ new LoggingWinston({
110121 version: 'some-version'
111122 }
112123});` ,
113- description : 'imports the module with a prefix and labels specified'
124+ description : 'imports the module with a prefix and labels specified' ,
125+ dependencies : [ 'winston@2' ] ,
126+ devDependencies : [ ]
114127 }
115128] ;
116129
117- const TIMEOUT_MS = 2 * 60 * 1000 ;
118-
119- const DEBUG = false ;
120- function log ( txt : string ) : void {
121- if ( DEBUG ) {
122- console . log ( txt ) ;
123- }
124- }
125-
126- const stdio = DEBUG ? 'inherit' : 'ignore' ;
127-
128- interface CodeSample {
129- code : string ;
130- description : string ;
131- }
132-
133- describe ( 'Installation' , ( ) => {
134- let installDir : string | undefined ;
135- before ( async ( ) => {
136- const tgz = await globP ( `${ process . cwd ( ) } /*.tgz` ) ;
137- assert . deepStrictEqual (
138- tgz . length , 0 ,
139- `Expected zero tgz files in the current working directory before ` +
140- `running the test but found files: ${ tgz . map ( file => {
141- const parts = file . split ( path . sep ) ;
142- return parts [ parts . length - 1 ] ;
143- } ) } `) ;
144- } ) ;
145-
146- beforeEach ( async function ( ) {
147- this . timeout ( TIMEOUT_MS ) ;
148- // This script assumes that you don't already have a TGZ file
149- // in your current working directory.
150- installDir = await tmpDirP ( ) ;
151- log ( `Using installation directory: ${ installDir } ` ) ;
152- await spawnP ( 'npm' , [ 'install' ] , { stdio} , log ) ;
153- await spawnP ( 'npm' , [ 'run' , 'compile' ] , { stdio} , log ) ;
154- await spawnP ( 'npm' , [ 'pack' ] , { stdio} , log ) ;
155- const tgz = await globP ( `${ process . cwd ( ) } /*.tgz` ) ;
156- if ( tgz . length !== 1 ) {
157- throw new Error (
158- `Expected 1 tgz file in current directory, but found ${ tgz . length } ` ) ;
159- }
160- await spawnP ( 'npm' , [ 'init' , '-y' ] , { cwd : installDir , stdio} , log ) ;
161- await spawnP (
162- 'npm' , [ 'install' , 'typescript' , '@types/node' , tgz [ 0 ] ] ,
163- { cwd : installDir , stdio} , log ) ;
164- } ) ;
165-
166- afterEach ( async function ( ) {
167- this . timeout ( TIMEOUT_MS ) ;
168- if ( installDir ) {
169- await rimrafP ( installDir ) ;
170- }
171- } ) ;
172-
173- describe ( 'When used with Typescript code' , ( ) => {
174- TS_CODE_ARRAY . forEach ( ( sample ) => {
175- it ( `should install and work with code that ${ sample . description } ` ,
176- async function ( ) {
177- this . timeout ( TIMEOUT_MS ) ;
178- assert ( installDir ) ;
179- const srcDir = path . join ( installDir ! , 'src' ) ;
180- await mkdirP ( srcDir ) ;
181- await spawnP (
182- 'npm' , [ 'install' , '--save' , 'winston' ] ,
183- { cwd : installDir , stdio} , log ) ;
184- await spawnP (
185- 'npm' , [ 'install' , '--save-dev' , '@types/winston' ] ,
186- { cwd : installDir , stdio} , log ) ;
187- await writeFileP ( path . join ( srcDir , INDEX_TS ) , sample . code , 'utf-8' ) ;
188- await spawnP (
189- 'npm' , [ 'install' , '--save-dev' , 'gts' , 'typescript@2.x' ] ,
190- { cwd : installDir , stdio} , log ) ;
191- await spawnP (
192- 'gts' , [ 'init' , '--yes' ] , { cwd : installDir , stdio} , log ) ;
193- await spawnP (
194- 'npm' , [ 'run' , 'compile' ] , { cwd : installDir , stdio} , log ) ;
195- const buildDir = path . join ( installDir ! , 'build' ) ;
196- await spawnP (
197- 'node' , [ path . join ( buildDir , 'src' , INDEX_JS ) ] ,
198- { cwd : installDir , stdio} , log ) ;
199- } ) ;
200- } ) ;
201- } ) ;
202-
203- describe ( 'When used with Javascript code' , ( ) => {
204- JS_CODE_ARRAY . forEach ( ( sample ) => {
205- it ( `should install and work with code that ${ sample . description } ` ,
206- async function ( ) {
207- this . timeout ( TIMEOUT_MS ) ;
208- assert ( installDir ) ;
209- await spawnP (
210- 'npm' , [ 'install' , '--save' , 'winston' ] ,
211- { cwd : installDir , stdio} , log ) ;
212- await spawnP (
213- 'npm' , [ 'install' , '--save-dev' , '@types/winston' ] ,
214- { cwd : installDir , stdio} , log ) ;
215- await writeFileP (
216- path . join ( installDir ! , INDEX_JS ) , sample . code , 'utf-8' ) ;
217- await spawnP ( 'node' , [ INDEX_JS ] , { cwd : installDir , stdio} , log ) ;
218- } ) ;
219- } ) ;
220- } ) ;
221- } ) ;
130+ check . testInstallation (
131+ TS_CODE_SAMPLES , JS_CODE_SAMPLES , { timeout : 2 * 60 * 1000 } ) ;
0 commit comments