4 const assert
= require('assert');
5 const sinon
= require('sinon');
6 const fs
= require('fs');
14 } = require('../../lib/file-scope');
16 describe('File Scope', function () {
20 noentError
= new Error('ENOENT: no such file or directory');
21 Object
.assign(noentError
, {
28 afterEach(function () {
32 describe('FileScopeError', function () {
33 it('covers', function () {
34 const e
= new FileScopeError('beep');
35 assert
.strictEqual(e
.name
, 'FileScopeError');
39 describe('readPackageJSON', function () {
41 beforeEach(function () {
42 filepath
= '/path/to/package.json';
43 sinon
.stub(fs
, 'readFileSync').returns(`{
44 "name": "@example/package",
48 it('covers success', function () {
49 const result
= readPackageJSON(filepath
);
50 assert
.strictEqual(result
.name
, '@example/package');
51 assert
.strictEqual(result
.version
, '3.1.4');
53 it('covers failure', function () {
54 fs
.readFileSync
.throws();
55 const result
= readPackageJSON(filepath
);
56 assert
.strictEqual(result
.name
, '(unknown)');
57 assert
.strictEqual(result
.version
, '(unknown)');
61 describe('pathExists', function () {
63 beforeEach(function () {
64 p
= '/path/to/package';
65 sinon
.stub(fs
, 'statSync').returns();
67 it('returns true when path exists', function () {
68 const result
= pathExists(p
);
69 assert
.strictEqual(result
, true);
71 it('returns false when path does not exist', function () {
72 fs
.statSync
.throws(noentError
);
73 const result
= pathExists(p
);
74 assert
.strictEqual(result
, false);
76 it('raises other error', function () {
77 const expectedError
= new Error('oh no');
78 fs
.statSync
.throws(expectedError
);
79 assert
.throws(() => pathExists(p
), expectedError
);
83 describe('locatePackageBase', function () {
86 beforeEach(function () {
87 filepath
= '/path/to/package/lib/file.js';
88 sinon
.stub(fs
, 'statSync');
91 it('covers unstubbed result', function () {
93 locatePackageBase(__filename
);
96 it('locates the package base', function () {
98 .onCall(0).throws(noentError
)
101 const result
= locatePackageBase(filepath
);
102 assert
.strictEqual(result
, '/path/to/package/');
105 it('cannot locate the package base', function () {
106 fs
.statSync
.throws(noentError
);
107 assert
.throws(() => locatePackageBase(filepath
), FileScopeError
);
110 it('propagates unknown error', function () {
111 const expectedException
= new Error('oh no');
113 .onCall(0).throws(noentError
)
114 .onCall(1).throws(expectedException
)
116 assert
.throws(() => locatePackageBase(filepath
), expectedException
);
118 }); // locatePackageBase
120 describe('defaultOptions', function () {
121 let packageBase
, expected
;
122 beforeEach(function () {
123 packageBase
= '/path/to/package';
124 sinon
.stub(fs
, 'statSync').returns();
127 includePackage: false,
128 includeVersion: false,
130 _errorEncountered: false,
135 it('covers no path', function () {
136 const options
= defaultOptions();
137 assert
.deepStrictEqual(options
, expected
);
139 it('covers default', function () {
140 expected
.includePath
= true;
141 fs
.statSync
.throws(noentError
);
142 const options
= defaultOptions(packageBase
);
143 assert
.deepStrictEqual(options
, expected
);
145 it('covers lib package', function () {
146 expected
.includePath
= true;
147 expected
.includePackage
= true;
148 expected
.includeVersion
= true;
149 expected
.leftTrim
= 4;
150 const options
= defaultOptions(packageBase
);
151 assert
.deepStrictEqual(options
, expected
);
153 it('covers src package', function () {
154 expected
.includePath
= true;
155 expected
.leftTrim
= 4;
156 fs
.statSync
.onCall(0).throws(noentError
);
157 const options
= defaultOptions(packageBase
);
158 assert
.deepStrictEqual(options
, expected
);
160 it('covers error', function () {
161 const expectedError
= new Error('oh no');
162 fs
.statSync
.throws(expectedError
);
163 expected
._errorEncountered
= true;
164 const options
= defaultOptions(packageBase
);
165 assert
.deepStrictEqual(options
, expected
);
167 }); // defaultOptions
169 describe('fileScope', function () {
170 let filepath
, options
, method
;
171 beforeEach(function () {
172 filepath
= '/path/to/package/lib/deep/file.js';
173 sinon
.stub(fs
, 'statSync')
174 .onCall(0).throws(noentError
) // deep
175 .onCall(1).throws(noentError
) // lib
176 .onCall(2).returns() // packageBase
178 sinon
.stub(fs
, 'readFileSync').returns(`{
179 "name": "@example/package",
184 includePackage: false,
185 includeVersion: false,
190 it('defaults', function () {
191 filepath
= '/path/to/package/code/module/file.js';
193 .onCall(3).throws(noentError
) // no lib
194 .onCall(4).throws(noentError
) // no src
196 const result
= fileScope(filepath
)(method
);
197 assert
.strictEqual(result
, 'code/module/file:method');
200 it('everything', function () {
201 options
.includePath
= true;
202 options
.includePackage
= true;
203 options
.includeVersion
= true;
204 const result
= fileScope(filepath
, options
)(method
);
205 assert
.strictEqual(result
, '@example/package@3.1.4:deep/file:method');
208 it('minimal', function () {
209 options
.includePath
= false;
210 options
.includePackage
= false;
211 options
.includeVersion
= false;
212 const result
= fileScope(filepath
, options
)(method
);
213 assert
.strictEqual(result
, 'file:method');
216 it('package only', function () {
217 options
.includePath
= false;
218 options
.includePackage
= true;
219 options
.includeVersion
= false;
220 const result
= fileScope(filepath
, options
)(method
);
221 assert
.strictEqual(result
, '@example/package:file:method');
224 it('covers no package root', function () {
225 options
.includePackage
= true;
226 fs
.statSync
.restore();
227 sinon
.stub(fs
, 'statSync').throws(noentError
);
228 fs
.readFileSync
.throws(noentError
);
229 const result
= fileScope(filepath
, options
)(method
);
230 assert
.strictEqual(result
, '?:(unknown):file:method');
233 it('covers exception while finding package root', function () {
234 const expectedException
= new Error('oh no');
235 options
.includePackage
= true;
236 fs
.statSync
.restore();
237 sinon
.stub(fs
, 'statSync').throws(expectedException
);
238 fs
.readFileSync
.throws(noentError
);
239 const result
= fileScope(filepath
, options
)(method
);
240 assert
.strictEqual(result
, '?:(unknown):file:method');
243 it('handles index.js', function () {
244 filepath
= '/path/to/package/src/deep/index.js';
246 .onCall(3).throws(noentError
) // no lib
247 .onCall(4).returns() // src
249 const result
= fileScope(filepath
)(method
);
250 assert
.strictEqual(result
, 'deep:method');
253 it('handles index.js when including path', function () {
254 filepath
= '/path/to/package/code/folder/deep/index.js';
255 fs
.statSync
.restore();
256 sinon
.stub(fs
, 'statSync')
258 .onCall(3).returns() // packageBase found
262 const result
= fileScope(filepath
)(method
);
263 assert
.strictEqual(result
, 'code/folder/deep:method');
266 it('handles index.js when not including path', function () {
267 filepath
= '/path/to/package/code/folder/deep/index.ts';
268 fs
.statSync
.restore();
269 sinon
.stub(fs
, 'statSync')
271 .onCall(3).returns() // packageBase found
275 const result
= fileScope(filepath
, { includePath: false })(method
);
276 assert
.strictEqual(result
, 'deep:method');