forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageMetadataManager.test.ts
More file actions
138 lines (128 loc) · 6.6 KB
/
PackageMetadataManager.test.ts
File metadata and controls
138 lines (128 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as path from 'path';
import { PackageMetadataManager } from '../PackageMetadataManager';
import { FileSystem, PackageJsonLookup, INodePackageJson, NewlineKind } from '@rushstack/node-core-library';
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
function resolveInTestPackage(testPackageName: string, ...args: string[]): string {
return path.resolve(__dirname, 'test-data/tsdoc-metadata-path-inference', testPackageName, ...args);
}
function getPackageMetadata(testPackageName: string): { packageFolder: string, packageJson: INodePackageJson } {
const packageFolder: string = resolveInTestPackage(testPackageName);
const packageJson: INodePackageJson | undefined = packageJsonLookup.tryLoadPackageJsonFor(packageFolder);
if (!packageJson) {
throw new Error('There should be a package.json file in the test package');
}
return { packageFolder, packageJson };
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function firstArgument(mockFn: jest.Mock): any {
return mockFn.mock.calls[0][0];
}
/* eslint-disable @typescript-eslint/typedef */
describe('PackageMetadataManager', () => {
describe('.writeTsdocMetadataFile()', () => {
const originalWriteFile = FileSystem.writeFile;
const mockWriteFile: jest.Mock = jest.fn();
beforeAll(() => {
FileSystem.writeFile = mockWriteFile;
});
afterEach(() => {
mockWriteFile.mockClear();
});
afterAll(() => {
FileSystem.writeFile = originalWriteFile;
});
it('writes the tsdoc metadata file at the provided path', () => {
PackageMetadataManager.writeTsdocMetadataFile('/foo/bar', NewlineKind.CrLf);
expect(firstArgument(mockWriteFile)).toBe('/foo/bar');
});
});
describe('.resolveTsdocMetadataPath()', () => {
describe('when an empty tsdocMetadataPath is provided', () => {
const tsdocMetadataPath: string = '';
describe('given a package.json where the field "tsdocMetadata" is defined', () => {
it('outputs the tsdoc metadata path as given by "tsdocMetadata" relative to the folder of package.json', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-inferred-from-tsdoc-metadata');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, packageJson.tsdocMetadata as string));
});
});
describe('given a package.json where the field "typings" is defined and "tsdocMetadata" is not defined', () => {
it('outputs the tsdoc metadata file "tsdoc-metadata.json" in the same folder as the path of "typings"', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-inferred-from-typings');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, path.dirname(packageJson.typings!), 'tsdoc-metadata.json'));
});
});
describe('given a package.json where the field "main" is defined but not "typings" nor "tsdocMetadata"', () => {
it('outputs the tsdoc metadata file "tsdoc-metadata.json" in the same folder as the path of "main"', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-inferred-from-main');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, path.dirname(packageJson.main!), 'tsdoc-metadata.json'));
});
});
describe('given a package.json where the fields "main", "typings" and "tsdocMetadata" are not defined', () => {
it('outputs the tsdoc metadata file "tsdoc-metadata.json" in the folder where package.json is located', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-default');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, 'tsdoc-metadata.json'));
});
});
});
describe('when a non-empty tsdocMetadataPath is provided', () => {
const tsdocMetadataPath: string = 'path/to/custom-tsdoc-metadata.json';
describe('given a package.json where the field "tsdocMetadata" is defined', () => {
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-inferred-from-tsdocMetadata');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, tsdocMetadataPath));
});
});
describe('given a package.json where the field "typings" is defined and "tsdocMetadata" is not defined', () => {
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-inferred-from-typings');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, tsdocMetadataPath));
});
});
describe('given a package.json where the field "main" is defined but not "typings" nor "tsdocMetadata"', () => {
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-inferred-from-main');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, tsdocMetadataPath));
});
});
describe('given a package.json where the fields "main", "typings" and "tsdocMetadata" are not defined', () => {
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => {
const {
packageFolder,
packageJson
} = getPackageMetadata('package-default');
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath))
.toBe(path.resolve(packageFolder, tsdocMetadataPath));
});
});
});
});
});
/* eslint-enable @typescript-eslint/typedef */