-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathSubspace.test.ts
More file actions
153 lines (122 loc) · 6.98 KB
/
Subspace.test.ts
File metadata and controls
153 lines (122 loc) · 6.98 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { RushConfiguration } from '../RushConfiguration';
import { Subspace } from '../Subspace';
describe(Subspace.name, () => {
describe('getPnpmCatalogsHash', () => {
it('returns undefined when no catalogs are defined', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repo', 'rush-pnpm.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const catalogsHash: string | undefined = defaultSubspace.getPnpmCatalogsHash();
expect(catalogsHash).toBeUndefined();
});
it('returns undefined for non-pnpm package manager', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repo', 'rush-npm.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const catalogsHash: string | undefined = defaultSubspace.getPnpmCatalogsHash();
expect(catalogsHash).toBeUndefined();
});
it('computes hash when catalogs are defined', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repoCatalogs', 'rush.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const catalogsHash: string | undefined = defaultSubspace.getPnpmCatalogsHash();
expect(catalogsHash).toBeDefined();
expect(typeof catalogsHash).toBe('string');
expect(catalogsHash).toHaveLength(40); // SHA1 hash is 40 characters
});
it('computes consistent hash for same catalog data', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repoCatalogs', 'rush.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const hash1: string | undefined = defaultSubspace.getPnpmCatalogsHash();
const hash2: string | undefined = defaultSubspace.getPnpmCatalogsHash();
expect(hash1).toBeDefined();
expect(hash1).toBe(hash2);
});
it('computes different hashes for different catalog data', () => {
// Configuration without catalogs
const rushJsonWithoutCatalogs: string = path.resolve(__dirname, 'repo', 'rush-pnpm.json');
const rushConfigWithoutCatalogs: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonWithoutCatalogs);
const subspaceWithoutCatalogs: Subspace = rushConfigWithoutCatalogs.defaultSubspace;
// Configuration with catalogs
const rushJsonWithCatalogs: string = path.resolve(__dirname, 'repoCatalogs', 'rush.json');
const rushConfigWithCatalogs: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonWithCatalogs);
const subspaceWithCatalogs: Subspace = rushConfigWithCatalogs.defaultSubspace;
const hashWithoutCatalogs: string | undefined = subspaceWithoutCatalogs.getPnpmCatalogsHash();
const hashWithCatalogs: string | undefined = subspaceWithCatalogs.getPnpmCatalogsHash();
// One should be undefined (no catalogs) and one should have a hash
expect(hashWithoutCatalogs).toBeUndefined();
expect(hashWithCatalogs).toBeDefined();
});
});
describe('getPackageJsonInjectedDependenciesHash', () => {
it('returns undefined when no injected dependencies exist', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repo', 'rush-pnpm.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const hash: string | undefined = defaultSubspace.getPackageJsonInjectedDependenciesHash();
expect(hash).toBeUndefined();
});
it('computes a hash when injected dependencies exist', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repoInjectedDeps', 'rush.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const hash: string | undefined = defaultSubspace.getPackageJsonInjectedDependenciesHash();
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
expect(hash).toHaveLength(40); // SHA1 hash
});
it('does not change when devDependencies of the injected package change', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repoInjectedDeps', 'rush.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const hashBefore: string | undefined = defaultSubspace.getPackageJsonInjectedDependenciesHash();
// Mutate devDependencies of the injected provider package
const providerProject = rushConfiguration.getProjectByName('provider')!;
const originalDevDeps = providerProject.packageJson.devDependencies;
providerProject.packageJson.devDependencies = {
...originalDevDeps,
jest: '^29.0.0'
};
const hashAfter: string | undefined = defaultSubspace.getPackageJsonInjectedDependenciesHash();
expect(hashBefore).toBeDefined();
expect(hashAfter).toBeDefined();
expect(hashBefore).toBe(hashAfter);
// Restore
providerProject.packageJson.devDependencies = originalDevDeps;
});
it('changes when production dependencies of the injected package change', () => {
const rushJsonFilename: string = path.resolve(__dirname, 'repoInjectedDeps', 'rush.json');
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilename);
const defaultSubspace: Subspace = rushConfiguration.defaultSubspace;
const hashBefore: string | undefined = defaultSubspace.getPackageJsonInjectedDependenciesHash();
// Mutate dependencies of the injected provider package
const providerProject = rushConfiguration.getProjectByName('provider')!;
const originalDeps = providerProject.packageJson.dependencies;
providerProject.packageJson.dependencies = {
...originalDeps,
axios: '^1.6.0'
};
const hashAfter: string | undefined = defaultSubspace.getPackageJsonInjectedDependenciesHash();
expect(hashBefore).toBeDefined();
expect(hashAfter).toBeDefined();
expect(hashBefore).not.toBe(hashAfter);
// Restore
providerProject.packageJson.dependencies = originalDeps;
});
});
});