Skip to content

Commit d27096c

Browse files
committed
Added a unit test
1 parent fd7170f commit d27096c

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

‎libraries/api-extractor/src/PackageJsonLookup.ts‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ interface IPackageJson {
1616
* and retrieving the name of the package. The results are cached.
1717
*/
1818
export default class PackageJsonLookup {
19-
private _tryFindPackagePathUpwardsCache: Map<string, string | undefined>;
20-
private _readPackageNameCache: Map<string, string>;
19+
// Cached the return values for tryFindPackagePathUpwards():
20+
// sourceFilePath --> packageJsonFolder
21+
private _packageFolderCache: Map<string, string | undefined>;
22+
23+
// Cached the return values for readPackageName():
24+
// packageJsonPath --> packageName
25+
private _packageNameCache: Map<string, string>;
2126

2227
constructor() {
23-
this._tryFindPackagePathUpwardsCache = new Map<string, string>();
24-
this._readPackageNameCache = new Map<string, string>();
28+
this._packageFolderCache = new Map<string, string | undefined>();
29+
this._packageNameCache = new Map<string, string>();
2530
}
2631

2732
/**
@@ -32,25 +37,25 @@ export default class PackageJsonLookup {
3237
* @param currentPath - a path (relative or absolute) of the current location
3338
* @returns a relative path to the package folder
3439
*/
35-
public tryFindPackagePathUpwards(currentPath: string): string | undefined {
40+
public tryFindPackagePathUpwards(sourceFilePath: string): string | undefined {
3641
// Two lookups are required, because get() cannot distinguish the undefined value
3742
// versus a missing key.
38-
if (this._tryFindPackagePathUpwardsCache.has(currentPath)) {
39-
return this._tryFindPackagePathUpwardsCache.get(currentPath);
43+
if (this._packageFolderCache.has(sourceFilePath)) {
44+
return this._packageFolderCache.get(sourceFilePath);
4045
}
4146

4247
let result: string | undefined;
4348

44-
const parentFolder: string = path.dirname(currentPath);
45-
if (!parentFolder || parentFolder === currentPath) {
49+
const parentFolder: string = path.dirname(sourceFilePath);
50+
if (!parentFolder || parentFolder === sourceFilePath) {
4651
result = undefined;
4752
} else if (fsx.existsSync(path.join(parentFolder, 'package.json'))) {
4853
result = path.normalize(parentFolder);
4954
} else {
5055
result = this.tryFindPackagePathUpwards(parentFolder);
5156
}
5257

53-
this._tryFindPackagePathUpwardsCache.set(currentPath, result);
58+
this._packageFolderCache.set(sourceFilePath, result);
5459
return result;
5560
}
5661

@@ -62,15 +67,15 @@ export default class PackageJsonLookup {
6267
* @returns the name of the package (E.g. @microsoft/api-extractor)
6368
*/
6469
public readPackageName(packageJsonPath: string): string {
65-
let result: string = this._readPackageNameCache.get(packageJsonPath);
70+
let result: string = this._packageNameCache.get(packageJsonPath);
6671
if (result !== undefined) {
6772
return result;
6873
}
6974

7075
const packageJson: IPackageJson = JsonFile.loadJsonFile(path.join(packageJsonPath, 'package.json')) as IPackageJson;
7176
result = packageJson.name;
7277

73-
this._readPackageNameCache.set(packageJsonPath, result);
78+
this._packageNameCache.set(packageJsonPath, result);
7479
return result;
7580
}
7681
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference types="mocha" />
2+
/* tslint:disable:no-function-expression - Mocha uses a poorly scoped "this" pointer */
3+
4+
import { assert } from 'chai';
5+
import * as path from 'path';
6+
import PackageJsonLookup from '../PackageJsonLookup';
7+
8+
describe('PackageJsonLookup', function (): void {
9+
10+
describe('basic tests', function (): void {
11+
12+
it('readPackageName() test', function (): void {
13+
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
14+
const sourceFilePath: string = path.join(__dirname, '../../testInputs/example1');
15+
assert.equal(packageJsonLookup.readPackageName(sourceFilePath), 'example1');
16+
});
17+
18+
it('tryFindPackagePathUpwards() test', function (): void {
19+
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
20+
const sourceFilePath: string = path.join(__dirname, '../../testInputs/example1/folder/AliasClass.ts');
21+
22+
// Example: C:\web-build-tools\libraries\api-extractor\testInputs\example1
23+
const foundPath: string = packageJsonLookup.tryFindPackagePathUpwards(sourceFilePath);
24+
assert.isTrue(foundPath.search(/[\\/]example1$/i) >= 0, 'Unexpected result: ' + foundPath);
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)