@@ -4,6 +4,23 @@ import { expect } from 'chai'
44
55import CachedPackageLocator from '../../../src/core/CachedPackageLocator'
66
7+ function notEmpty ( obj ) {
8+ return Object . keys ( obj ) . length
9+ }
10+
11+ function reduce ( {
12+ dependencies = { } ,
13+ devDependencies = { } ,
14+ peerDependencies = { } ,
15+ optionalDependencies = { } ,
16+ } = { } ) {
17+ if ( [ dependencies , devDependencies , peerDependencies , optionalDependencies ] . some ( notEmpty ) ) {
18+ return { dependencies, devDependencies, peerDependencies, optionalDependencies }
19+ }
20+
21+ return null
22+ }
23+
724describe ( 'CachedPackageLocator.readUpSync()' , function ( ) {
825 let sandbox
926 let packageLocator
@@ -49,18 +66,20 @@ describe('CachedPackageLocator.readUpSync()', function () {
4966 it ( 'should not repeat fs.readFileSync on stored locations' , function ( ) {
5067 fs . readFileSync . withArgs ( '/a/package.json' ) . returns ( withDepsStr )
5168
52- expect ( packageLocator . readUpSync ( context , '/a/b' ) ) . to . deep . equal ( withDeps )
69+ expect ( packageLocator . readUpSync ( context , '/a/b' , false , reduce ) )
70+ . to . deep . equal ( withDeps )
5371 sinon . assert . callCount ( fs . readFileSync , 2 )
54- expect ( packageLocator . readUpSync ( context , '/a' ) ) . to . deep . equal ( withDeps )
72+ expect ( packageLocator . readUpSync ( context , '/a' , false , reduce ) )
73+ . to . deep . equal ( withDeps )
5574 sinon . assert . callCount ( fs . readFileSync , 2 )
5675 expect ( packageLocator . store ) . to . deep . equal ( {
5776 '/a/b/package.json' : null ,
5877 '/a/package.json' : withDeps ,
5978 } )
6079
61- expect ( packageLocator . readUpSync ( context , '/x' ) ) . to . be . undefined
80+ expect ( packageLocator . readUpSync ( context , '/x' , false , reduce ) ) . to . be . undefined
6281 sinon . assert . callCount ( fs . readFileSync , 4 )
63- expect ( packageLocator . readUpSync ( context , '/x' ) ) . to . be . undefined
82+ expect ( packageLocator . readUpSync ( context , '/x' , false , reduce ) ) . to . be . undefined
6483 sinon . assert . callCount ( fs . readFileSync , 4 )
6584 expect ( packageLocator . store ) . to . deep . equal ( {
6685 '/x/package.json' : null ,
@@ -69,9 +88,9 @@ describe('CachedPackageLocator.readUpSync()', function () {
6988 '/package.json' : null ,
7089 } )
7190
72- expect ( packageLocator . readUpSync ( context , '/x/y/z' ) ) . to . be . undefined
91+ expect ( packageLocator . readUpSync ( context , '/x/y/z' , false , reduce ) ) . to . be . undefined
7392 sinon . assert . callCount ( fs . readFileSync , 6 )
74- expect ( packageLocator . readUpSync ( context , '/x/y/z' ) ) . to . be . undefined
93+ expect ( packageLocator . readUpSync ( context , '/x/y/z' , false , reduce ) ) . to . be . undefined
7594 sinon . assert . callCount ( fs . readFileSync , 6 )
7695 expect ( packageLocator . store ) . to . deep . equal ( {
7796 '/x/y/z/package.json' : null ,
@@ -82,9 +101,9 @@ describe('CachedPackageLocator.readUpSync()', function () {
82101 '/package.json' : null ,
83102 } )
84103
85- expect ( packageLocator . readUpSync ( context , '/x/w' ) ) . to . be . undefined
104+ expect ( packageLocator . readUpSync ( context , '/x/w' , false , reduce ) ) . to . be . undefined
86105 sinon . assert . callCount ( fs . readFileSync , 7 )
87- expect ( packageLocator . readUpSync ( context , '/x/w' ) ) . to . be . undefined
106+ expect ( packageLocator . readUpSync ( context , '/x/w' , false , reduce ) ) . to . be . undefined
88107 sinon . assert . callCount ( fs . readFileSync , 7 )
89108
90109 expect ( packageLocator . store ) . to . deep . equal ( {
@@ -100,7 +119,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
100119
101120 it ( 'should only store and return dependency fields' , function ( ) {
102121 fs . readFileSync . withArgs ( '/package.json' ) . returns ( withDepsExtraFieldsStr )
103- expect ( packageLocator . readUpSync ( context , '/' ) ) . to . deep . equal ( withDeps )
122+ expect ( packageLocator . readUpSync ( context , '/' , false , reduce ) )
123+ . to . deep . equal ( withDeps )
104124 expect ( packageLocator . store ) . to . deep . equal ( {
105125 '/package.json' : withDeps ,
106126 } )
@@ -109,7 +129,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
109129
110130 it ( 'should locate first available' , function ( ) {
111131 fs . readFileSync . withArgs ( '/a/b/package.json' ) . returns ( withDepsStr )
112- expect ( packageLocator . readUpSync ( context , '/a/b' ) ) . to . deep . equal ( withDeps )
132+ expect ( packageLocator . readUpSync ( context , '/a/b' , false , reduce ) )
133+ . to . deep . equal ( withDeps )
113134 expect ( packageLocator . store ) . to . deep . equal ( {
114135 '/a/b/package.json' : withDeps ,
115136 } )
@@ -118,7 +139,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
118139
119140 it ( 'should locate last available' , function ( ) {
120141 fs . readFileSync . withArgs ( '/package.json' ) . returns ( withDepsStr )
121- expect ( packageLocator . readUpSync ( context , '/a/b/c/d/e/f' ) ) . to . deep . equal ( withDeps )
142+ expect ( packageLocator . readUpSync ( context , '/a/b/c/d/e/f' , false , reduce ) )
143+ . to . deep . equal ( withDeps )
122144 expect ( packageLocator . store ) . to . deep . equal ( {
123145 '/a/b/c/d/e/f/package.json' : null ,
124146 '/a/b/c/d/e/package.json' : null ,
@@ -133,7 +155,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
133155
134156 it ( 'should store package.json with empty deps as null' , function ( ) {
135157 fs . readFileSync . withArgs ( '/package.json' ) . returns ( '{}' )
136- expect ( packageLocator . readUpSync ( context , '/' ) ) . to . be . undefined
158+ expect ( packageLocator . readUpSync ( context , '/' , false , reduce ) )
159+ . to . be . undefined
137160 expect ( packageLocator . store ) . to . deep . equal ( {
138161 '/package.json' : null ,
139162 } )
@@ -145,9 +168,11 @@ describe('CachedPackageLocator.readUpSync()', function () {
145168 . withArgs ( '/package.json' ) . returns ( withDepsStr )
146169 . withArgs ( '/a/package.json' ) . returns ( withUnexpectedTokenStr )
147170 . withArgs ( '/a/b/package.json' ) . returns ( withUnexpectedEndStr )
148- expect ( packageLocator . readUpSync ( context , '/a' ) ) . to . be . undefined
171+ expect ( packageLocator . readUpSync ( context , '/a' , false ) )
172+ . to . be . undefined
149173 expect ( packageLocator . store ) . to . be . empty
150- expect ( packageLocator . readUpSync ( context , '/a/b/c/d' ) ) . to . be . undefined
174+ expect ( packageLocator . readUpSync ( context , '/a/b/c/d' , false ) )
175+ . to . be . undefined
151176 expect ( packageLocator . store ) . to . deep . equal ( {
152177 '/a/b/c/d/package.json' : null ,
153178 '/a/b/c/package.json' : null ,
@@ -156,7 +181,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
156181 } )
157182
158183 it ( 'should store failed locations as null' , function ( ) {
159- expect ( packageLocator . readUpSync ( context , '/does/not/exist' ) ) . to . be . undefined
184+ expect ( packageLocator . readUpSync ( context , '/does/not/exist' , false ) )
185+ . to . be . undefined
160186 expect ( packageLocator . store ) . to . deep . equal ( {
161187 '/does/not/exist/package.json' : null ,
162188 '/does/not/package.json' : null ,
@@ -167,7 +193,8 @@ describe('CachedPackageLocator.readUpSync()', function () {
167193 } )
168194
169195 it ( 'immediate=true should halt on first failed location' , function ( ) {
170- expect ( packageLocator . readUpSync ( context , '/does/not/exist' , true ) ) . to . be . undefined
196+ expect ( packageLocator . readUpSync ( context , '/does/not/exist' , true ) )
197+ . to . be . undefined
171198 expect ( packageLocator . store ) . to . deep . equal ( {
172199 '/does/not/exist/package.json' : null
173200 } )
0 commit comments