import * as path from 'path' ; import { PackageJsonLookup } from '../PackageJsonLookup' ; import { IPackageJson , INodePackageJson } from '../IPackageJson' ; import { FileConstants } from '../Constants' ; describe ( 'PackageJsonLookup' , ( ) => { describe ( 'basic tests' , ( ) => { test ( '' , ( ) => { expect ( PackageJsonLookup . loadOwnPackageJson ( __dirname ) . name ) . toEqual ( '@rushstack/node-core-library' ) ; } ) ; test ( 'tryLoadPackageJsonFor() test' , ( ) => { const packageJsonLookup = new PackageJsonLookup ( ) ; const sourceFilePath = path . join ( __dirname , './test-data/example-package' ) ; const packageJson = packageJsonLookup . tryLoadPackageJsonFor ( sourceFilePath ) ; expect ( packageJson ) . toBeDefined ( ) ; if ( packageJson ) { expect ( packageJson . name ) . toEqual ( 'example-package' ) ; expect ( packageJson . version ) . toEqual ( '1.0.0' ) ; expect ( packageJson ) . not . toHaveProperty ( 'nonstandardField' ) ; } } ) ; test ( 'tryLoadNodePackageJsonFor() test package with no version' , ( ) => { const packageJsonLookup = new PackageJsonLookup ( ) ; const sourceFilePath = path . join ( __dirname , './test-data/example-package-no-version' ) ; const packageJson = packageJsonLookup . tryLoadNodePackageJsonFor ( sourceFilePath ) ; expect ( packageJson ) . toBeDefined ( ) ; if ( packageJson ) { expect ( packageJson . name ) . toEqual ( 'example-package' ) ; expect ( packageJson . version ) . not . toBeDefined ( ) ; expect ( packageJson ) . not . toHaveProperty ( 'nonstandardField' ) ; } } ) ; test ( 'tryGetPackageFolderFor() test' , ( ) => { const packageJsonLookup = new PackageJsonLookup ( ) ; const sourceFilePath = path . join ( __dirname , './test-data/example-package/src/ExampleFile.txt' ) ; const foundFolder = packageJsonLookup . tryGetPackageFolderFor ( sourceFilePath ) ; expect ( foundFolder ) . toBeDefined ( ) ; expect ( foundFolder ! . search ( /[\\/]example-package$/i ) ) . toBeGreaterThan ( 0 ) ; const foundFile = packageJsonLookup . tryGetPackageJsonFilePathFor ( sourceFilePath ) ; expect ( foundFile ) . toEqual ( path . join ( foundFolder || '' , FileConstants . PackageJson ) ) ; } ) ; } ) ; } ) ;