Skip to content

Commit f5090b9

Browse files
fmt
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
1 parent 59e518a commit f5090b9

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

tests/unit/thrift-field-id-validation.test.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,42 @@ import { expect } from 'chai';
1111
describe('Thrift Field ID Validation', () => {
1212
const MAX_ALLOWED_FIELD_ID = 3329;
1313
const THRIFT_DIR = path.join(__dirname, '../../thrift');
14-
14+
1515
it('should ensure all Thrift field IDs are within allowed range', () => {
1616
const violations: string[] = [];
17-
17+
1818
// Get all JavaScript files in the thrift directory
19-
const thriftFiles = fs.readdirSync(THRIFT_DIR)
20-
.filter(file => file.endsWith('.js'))
21-
.map(file => path.join(THRIFT_DIR, file));
22-
19+
const thriftFiles = fs
20+
.readdirSync(THRIFT_DIR)
21+
.filter((file) => file.endsWith('.js'))
22+
.map((file) => path.join(THRIFT_DIR, file));
23+
2324
expect(thriftFiles.length).to.be.greaterThan(0, 'No Thrift JavaScript files found');
24-
25+
2526
for (const filePath of thriftFiles) {
2627
const fileName = path.basename(filePath);
2728
const fileContent = fs.readFileSync(filePath, 'utf8');
28-
29+
2930
// Extract field IDs from both read and write functions
3031
const fieldIds = extractFieldIds(fileContent);
31-
32+
3233
for (const fieldId of fieldIds) {
3334
if (fieldId >= MAX_ALLOWED_FIELD_ID) {
34-
violations.push(`${fileName}: Field ID ${fieldId} exceeds maximum allowed value of ${MAX_ALLOWED_FIELD_ID - 1}`);
35+
violations.push(
36+
`${fileName}: Field ID ${fieldId} exceeds maximum allowed value of ${MAX_ALLOWED_FIELD_ID - 1}`,
37+
);
3538
}
3639
}
3740
}
38-
41+
3942
if (violations.length > 0) {
4043
const errorMessage = [
4144
`Found Thrift field IDs that exceed the maximum allowed value of ${MAX_ALLOWED_FIELD_ID - 1}.`,
4245
'This can cause compatibility issues and conflicts with reserved ID ranges.',
4346
'Violations found:',
44-
...violations.map(v => ` - ${v}`)
47+
...violations.map((v) => ` - ${v}`),
4548
].join('\n');
46-
49+
4750
throw new Error(errorMessage);
4851
}
4952
});
@@ -55,29 +58,29 @@ describe('Thrift Field ID Validation', () => {
5558
*/
5659
function extractFieldIds(fileContent: string): number[] {
5760
const fieldIds = new Set<number>();
58-
61+
5962
// Pattern 1: Extract field IDs from case statements in read functions
6063
// Example: case 1281:
6164
const casePattern = /case\s+(\d+):/g;
6265
let match;
63-
66+
6467
while ((match = casePattern.exec(fileContent)) !== null) {
6568
const fieldId = parseInt(match[1], 10);
6669
if (!isNaN(fieldId)) {
6770
fieldIds.add(fieldId);
6871
}
6972
}
70-
73+
7174
// Pattern 2: Extract field IDs from writeFieldBegin calls in write functions
7275
// Example: output.writeFieldBegin('errorDetailsJson', Thrift.Type.STRING, 1281);
7376
const writeFieldPattern = /writeFieldBegin\([^,]+,\s*[^,]+,\s*(\d+)\)/g;
74-
77+
7578
while ((match = writeFieldPattern.exec(fileContent)) !== null) {
7679
const fieldId = parseInt(match[1], 10);
7780
if (!isNaN(fieldId)) {
7881
fieldIds.add(fieldId);
7982
}
8083
}
81-
84+
8285
return Array.from(fieldIds).sort((a, b) => a - b);
83-
}
86+
}

0 commit comments

Comments
 (0)