Skip to content

Commit 8a73a32

Browse files
author
Ben Keen
committed
Code review feedback - test and code cleanup
1 parent 30064fe commit 8a73a32

File tree

9 files changed

+200
-220
lines changed

9 files changed

+200
-220
lines changed

libraries/rush-lib/config/heft.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{
3131
"sourcePath": "src/cli/test",
3232
"destinationFolders": ["lib-intermediate-commonjs/cli/test"],
33-
"fileExtensions": [".js"]
33+
"fileExtensions": [".js", ".yaml"]
3434
},
3535
{
3636
"sourcePath": "src/logic/pnpm/test",

libraries/rush-lib/src/cli/test/RushPnpmCommandLineParser.test.ts

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,29 @@ import { FileSystem, JsonFile } from '@rushstack/node-core-library';
66
import { TestUtilities } from '@rushstack/heft-config-file';
77
import { RushConfiguration } from '../../api/RushConfiguration';
88

9+
const MONOREPO_ROOT: string = path.dirname(
10+
RushConfiguration.tryFindRushJsonLocation({ startingFolder: __dirname })!
11+
);
12+
const CATALOG_SYNC_REPO_PATH: string = `${__dirname}/catalogSyncTestRepo`;
13+
914
describe('RushPnpmCommandLineParser', () => {
1015
describe('catalog syncing', () => {
11-
let testRepoPath: string;
12-
let pnpmConfigPath: string;
13-
let pnpmWorkspacePath: string;
14-
15-
beforeEach(() => {
16-
testRepoPath = path.join(__dirname, 'temp', 'catalog-sync-test-repo');
17-
FileSystem.ensureFolder(testRepoPath);
18-
19-
const rushJsonPath: string = `${testRepoPath/rush.json`;
20-
const rushJson = {
21-
$schema: 'https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json',
22-
rushVersion: '5.166.0',
23-
pnpmVersion: '10.28.1',
24-
nodeSupportedVersionRange: '>=18.0.0',
25-
projects: []
26-
};
27-
JsonFile.save(rushJson, rushJsonPath, { ensureFolderExists: true });
28-
29-
const configDir: string = path.join(testRepoPath, 'common', 'config', 'rush');
30-
FileSystem.ensureFolder(configDir);
31-
32-
pnpmConfigPath = path.join(configDir, 'pnpm-config.json');
33-
const pnpmConfig = {
34-
globalCatalogs: {
35-
default: {
36-
react: '^18.0.0',
37-
'react-dom': '^18.0.0'
38-
}
39-
}
40-
};
41-
JsonFile.save(pnpmConfig, pnpmConfigPath);
16+
const testRepoPath: string = `${MONOREPO_ROOT}/temp/catalog-sync-test-repo`;
17+
const pnpmConfigPath: string = `${testRepoPath}/common/config/rush/pnpm-config.json`;
18+
const pnpmWorkspacePath: string = `${testRepoPath}/common/temp/pnpm-workspace.yaml`;
4219

43-
const tempDir: string = path.join(testRepoPath, 'common', 'temp');
44-
FileSystem.ensureFolder(tempDir);
20+
beforeEach(async () => {
21+
await FileSystem.copyFilesAsync({ sourcePath: CATALOG_SYNC_REPO_PATH, destinationPath: testRepoPath });
4522

46-
pnpmWorkspacePath = path.join(tempDir, 'pnpm-workspace.yaml');
47-
const workspaceYaml = `packages:
48-
- '../../apps/*'
49-
catalogs:
50-
default:
51-
react: ^18.0.0
52-
react-dom: ^18.0.0
53-
`;
54-
FileSystem.writeFile(pnpmWorkspacePath, workspaceYaml);
23+
// common/temp is gitignored so it is not part of the static repo; copy the initial workspace file here
24+
await FileSystem.copyFilesAsync({
25+
sourcePath: `${CATALOG_SYNC_REPO_PATH}/pnpm-workspace.yaml`,
26+
destinationPath: pnpmWorkspacePath
27+
});
5528
});
5629

57-
afterEach(() => {
58-
if (FileSystem.exists(testRepoPath)) {
59-
FileSystem.deleteFolder(testRepoPath);
60-
}
30+
afterEach(async () => {
31+
await FileSystem.deleteFolderAsync(testRepoPath);
6132
});
6233

6334
it('syncs updated catalogs from pnpm-workspace.yaml to pnpm-config.json', async () => {
@@ -71,10 +42,10 @@ catalogs:
7142
frontend:
7243
vue: ^3.4.0
7344
`;
74-
FileSystem.writeFile(pnpmWorkspacePath, updatedWorkspaceYaml);
45+
await FileSystem.writeFileAsync(pnpmWorkspacePath, updatedWorkspaceYaml);
7546

7647
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(
77-
path.join(testRepoPath, 'rush.json')
48+
`${testRepoPath}/rush.json`
7849
);
7950

8051
const subspace = rushConfiguration.getSubspace('default');
@@ -93,7 +64,7 @@ catalogs:
9364
pnpmOptions?.updateGlobalCatalogs(newCatalogs);
9465

9566
const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(
96-
path.join(testRepoPath, 'rush.json')
67+
`${testRepoPath}/rush.json`
9768
);
9869
const updatedSubspace = updatedRushConfiguration.getSubspace('default');
9970
const updatedPnpmOptions = updatedSubspace.getPnpmOptions();
@@ -115,7 +86,7 @@ catalogs:
11586
const newCatalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(pnpmWorkspacePath);
11687

11788
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(
118-
path.join(testRepoPath, 'rush.json')
89+
`${testRepoPath}/rush.json`
11990
);
12091
const subspace = rushConfiguration.getSubspace('default');
12192
const pnpmOptions = subspace.getPnpmOptions();
@@ -135,23 +106,23 @@ catalogs:
135106
const workspaceWithoutCatalogs = `packages:
136107
- '../../apps/*'
137108
`;
138-
FileSystem.writeFile(pnpmWorkspacePath, workspaceWithoutCatalogs);
109+
await FileSystem.writeFileAsync(pnpmWorkspacePath, workspaceWithoutCatalogs);
139110

140111
const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile');
141112
const newCatalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(pnpmWorkspacePath);
142113

143114
expect(newCatalogs).toBeUndefined();
144115

145116
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(
146-
path.join(testRepoPath, 'rush.json')
117+
`${testRepoPath}/rush.json`
147118
);
148119
const subspace = rushConfiguration.getSubspace('default');
149120
const pnpmOptions = subspace.getPnpmOptions();
150121

151122
pnpmOptions?.updateGlobalCatalogs(newCatalogs);
152123

153124
const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(
154-
path.join(testRepoPath, 'rush.json')
125+
`${testRepoPath}/rush.json`
155126
);
156127
const updatedSubspace = updatedRushConfiguration.getSubspace('default');
157128
const updatedPnpmOptions = updatedSubspace.getPnpmOptions();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"globalCatalogs": {
3+
"default": {
4+
"react": "^18.0.0",
5+
"react-dom": "^18.0.0"
6+
}
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packages:
2+
- '../../apps/*'
3+
catalogs:
4+
default:
5+
react: ^18.0.0
6+
react-dom: ^18.0.0
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
3+
"rushVersion": "5.166.0",
4+
"pnpmVersion": "10.28.1",
5+
"nodeSupportedVersionRange": ">=18.0.0",
6+
"projects": []
7+
}

libraries/rush-lib/src/logic/pnpm/PnpmOptionsConfiguration.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,7 @@ export class PnpmOptionsConfiguration extends PackageManagerOptionsConfiguration
535535
public updateGlobalPatchedDependencies(patchedDependencies: Record<string, string> | undefined): void {
536536
this._globalPatchedDependencies = patchedDependencies;
537537
this._json.globalPatchedDependencies = patchedDependencies;
538-
if (this.jsonFilename) {
539-
JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
540-
}
538+
JsonFile.save(this._json, this.jsonFilename as string, { updateExistingFile: true });
541539
}
542540

543541
/**
@@ -547,18 +545,20 @@ export class PnpmOptionsConfiguration extends PackageManagerOptionsConfiguration
547545
onlyBuiltDependencies: string[] | undefined
548546
): Promise<void> {
549547
this._json.globalOnlyBuiltDependencies = onlyBuiltDependencies;
550-
if (this.jsonFilename) {
551-
await JsonFile.saveAsync(this._json, this.jsonFilename, { updateExistingFile: true, ignoreUndefinedValues: true });
552-
}
548+
await JsonFile.saveAsync(this._json, this.jsonFilename as string, {
549+
updateExistingFile: true,
550+
ignoreUndefinedValues: true
551+
});
553552
}
554553

555554
/**
556555
* Updates globalCatalogs field of the PNPM options in the common/config/rush/pnpm-config.json file.
557556
*/
558557
public updateGlobalCatalogs(catalogs: Record<string, Record<string, string>> | undefined): void {
559558
this._json.globalCatalogs = catalogs;
560-
if (this.jsonFilename) {
561-
JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true, ignoreUndefinedValues: true });
562-
}
559+
JsonFile.save(this._json, this.jsonFilename as string, {
560+
updateExistingFile: true,
561+
ignoreUndefinedValues: true
562+
});
563563
}
564564
}

libraries/rush-lib/src/logic/pnpm/PnpmWorkspaceFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
7777
throw error;
7878
}
7979
}
80-
80+
8181
const parsed: IPnpmWorkspaceYaml | undefined = yamlModule.load(content) as IPnpmWorkspaceYaml | undefined;
8282

8383
return parsed?.catalogs;

0 commit comments

Comments
 (0)