Skip to content

Commit 742c75e

Browse files
authored
chore: enforce package.json %key% are present in package.nls.json (#1221)
in a unit test
1 parent 776dc6d commit 742c75e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/extension/test/node/configurations.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5+
import * as fs from 'fs';
6+
import * as path from 'path';
57
import { describe, expect, it } from 'vitest';
68
import { Config, ConfigKey } from '../../../platform/configuration/common/configurationService';
79
import { packageJson } from '../../../platform/env/common/packagejson';
@@ -76,4 +78,28 @@ describe('Configurations', () => {
7678
expect(publicKeys, 'Setting in package.json is not defined in code').toContain(key);
7779
});
7880
});
81+
82+
it('all localization strings in package.json are present in package.nls.json', async () => {
83+
// Get all keys from package.nls.json
84+
const packageJsonPath = path.join(__dirname, '../../../../package.json');
85+
const packageNlsPath = path.join(__dirname, '../../../../package.nls.json');
86+
const [packageJsonFileContents, packageNlsFileContents] = await Promise.all(
87+
[
88+
fs.promises.readFile(packageJsonPath, 'utf-8'),
89+
fs.promises.readFile(packageNlsPath, 'utf-8'),
90+
]
91+
);
92+
93+
const packageNls = JSON.parse(packageNlsFileContents);
94+
const nlsKeys = Object.keys(packageNls);
95+
96+
// Find all %key% references in package.json
97+
const nlsReferences = Array.from(packageJsonFileContents.matchAll(/"%([^"]+)%"/g)).map(match => match[1]);
98+
99+
// Validate all references exist in package.nls.json
100+
const missingKeys = nlsReferences.filter(key => !nlsKeys.includes(key));
101+
if (missingKeys.length > 0) {
102+
throw new Error(`Missing localization keys in package.nls.json but present in package.json: ${missingKeys.map(key => `'%${key}%'`).join(', ')}`);
103+
}
104+
});
79105
});

0 commit comments

Comments
 (0)