33 * We don't use a test framework here since dependencies are awkward.
44 */
55const pkg = 'build_bazel_rules_nodejs/packages/create/npm_package' ;
6- const path = require ( 'path' ) ;
76const fs = require ( 'fs' ) ;
87const { main} = require ( pkg ) ;
98
@@ -12,6 +11,10 @@ function fail(...msg) {
1211 throw new Error ( 'test failed' ) ;
1312}
1413
14+ function read ( path ) {
15+ return fs . readFileSync ( path , { encoding : 'utf-8' } ) ;
16+ }
17+
1518let error , exitCode ;
1619function captureError ( ...msg ) {
1720 error = error + '\n' + msg . join ( ' ' ) ;
@@ -43,31 +46,35 @@ const projFiles = fs.readdirSync('some_project');
4346if ( ! projFiles . indexOf ( '.bazelrc' ) < 0 ) {
4447 fail ( 'project should have .bazelrc' ) ;
4548}
46- let wkspContent = fs . readFileSync ( 'some_project/WORKSPACE' , { encoding : 'utf-8' } ) ;
49+ let wkspContent = read ( 'some_project/WORKSPACE' ) ;
4750if ( wkspContent . indexOf ( 'npm_install(' ) < 0 ) {
4851 fail ( 'should use npm by default' ) ;
4952}
5053// TODO: run bazel in the new directory to verify a build works
5154
5255exitCode = main ( [ 'configure_pkgMgr' , '--packageManager=yarn' ] , captureError ) ;
5356if ( exitCode != 0 ) fail ( 'should be success' ) ;
54- wkspContent = fs . readFileSync ( 'configure_pkgMgr/WORKSPACE' , { encoding : 'utf-8' } ) ;
57+ wkspContent = read ( 'configure_pkgMgr/WORKSPACE' ) ;
5558if ( wkspContent . indexOf ( 'yarn_install(' ) < 0 ) {
5659 fail ( 'should use yarn when requested' ) ;
5760}
5861
5962process . env [ '_' ] = '/usr/bin/yarn' ;
6063exitCode = main ( [ 'default_to_yarn' ] ) ;
6164if ( exitCode != 0 ) fail ( 'should be success' ) ;
62- wkspContent = fs . readFileSync ( 'default_to_yarn/WORKSPACE' , { encoding : 'utf-8' } ) ;
65+ wkspContent = read ( 'default_to_yarn/WORKSPACE' ) ;
6366if ( wkspContent . indexOf ( 'yarn_install(' ) < 0 ) {
64- fail ( 'should use yarn by default' )
67+ fail ( 'should use yarn by default' ) ;
6568}
6669// TODO: run bazel in the new directory to verify a build works
6770
6871exitCode = main ( [ '--typescript' , 'with_ts' ] , captureError ) ;
6972if ( exitCode != 0 ) fail ( 'should be success' ) ;
70- let pkgContent = fs . readFileSync ( 'with_ts/package.json' , { encoding : 'utf-8' } ) ;
73+ let pkgContent = read ( 'with_ts/package.json' ) ;
7174if ( pkgContent . indexOf ( '"@bazel/typescript": "latest"' ) < 0 ) {
7275 fail ( 'should install @bazel/typescript dependency' , pkgContent ) ;
7376}
77+ wkspContent = read ( 'with_ts/WORKSPACE' ) ;
78+ if ( wkspContent . indexOf ( 'ts_setup_workspace(' ) < 0 ) {
79+ fail ( 'should install extra TS repositories' ) ;
80+ }
0 commit comments