Skip to content

Commit 27e585c

Browse files
Refactor validateLicense to void function for clearer API semantics
1 parent 9299a24 commit 27e585c

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

react_on_rails_pro/packages/node-renderer/src/master.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,15 @@ import log from './shared/log';
77
import { buildConfig, Config, logSanitizedConfig } from './shared/configBuilder';
88
import restartWorkers from './master/restartWorkers';
99
import * as errorReporter from './shared/errorReporter';
10-
import { validateLicense, getValidationError } from './shared/licenseValidator';
10+
import { validateLicense } from './shared/licenseValidator';
1111

1212
const MILLISECONDS_IN_MINUTE = 60000;
1313

1414
export = function masterRun(runningConfig?: Partial<Config>) {
1515
// Validate license before starting - required in all environments
1616
log.info('[React on Rails Pro] Validating license...');
17-
18-
if (validateLicense()) {
19-
log.info('[React on Rails Pro] License validation successful');
20-
} else {
21-
// License validation already calls process.exit(1) in handleInvalidLicense
22-
// But we add this for safety in case the validator changes
23-
const error = getValidationError() || 'Invalid license';
24-
log.error(`[React on Rails Pro] ${error}`);
25-
process.exit(1);
26-
}
17+
validateLicense();
18+
log.info('[React on Rails Pro] License validation successful');
2719

2820
// Store config in app state. From now it can be loaded by any module using getConfig():
2921
const config = buildConfig(runningConfig);

react_on_rails_pro/packages/node-renderer/src/shared/licenseValidator.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ let cachedLicenseData: LicenseData | undefined;
1919
let cachedValidationError: string | undefined;
2020

2121
/**
22-
* Validates the license and raises an error if invalid.
22+
* Validates the license and exits the process if invalid.
2323
* Caches the result after first validation.
2424
*
25-
* @returns true if license is valid
2625
* @throws Exits process if license is invalid
2726
*/
28-
export function validateLicense(): boolean {
27+
export function validateLicense(): void {
2928
if (cachedValid !== undefined) {
30-
return cachedValid;
29+
return;
3130
}
3231

3332
cachedValid = performValidation();
34-
return cachedValid;
3533
}
3634

3735
/**

react_on_rails_pro/packages/node-renderer/tests/licenseValidator.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('LicenseValidator', () => {
7575
});
7676

7777
describe('validateLicense', () => {
78-
it('returns true for valid license in ENV', () => {
78+
it('validates successfully for valid license in ENV', () => {
7979
const validPayload = {
8080
8181
iat: Math.floor(Date.now() / 1000),
@@ -86,7 +86,8 @@ describe('LicenseValidator', () => {
8686
process.env.REACT_ON_RAILS_PRO_LICENSE = validToken;
8787

8888
const module = require('../src/shared/licenseValidator');
89-
expect(module.validateLicense()).toBe(true);
89+
expect(() => module.validateLicense()).not.toThrow();
90+
expect(process.exit).not.toHaveBeenCalled();
9091
});
9192

9293
it('calls process.exit for expired license', () => {
@@ -192,7 +193,8 @@ describe('LicenseValidator', () => {
192193
// Reset to pick up the new ENV variable
193194
reset();
194195

195-
expect(module.validateLicense()).toBe(true);
196+
expect(() => module.validateLicense()).not.toThrow();
197+
expect(process.exit).not.toHaveBeenCalled();
196198
});
197199

198200
it('caches validation result', () => {
@@ -208,13 +210,15 @@ describe('LicenseValidator', () => {
208210
const module = require('../src/shared/licenseValidator');
209211

210212
// First call
211-
expect(module.validateLicense()).toBe(true);
213+
module.validateLicense();
214+
expect(process.exit).not.toHaveBeenCalled();
212215

213216
// Change ENV (shouldn't affect cached result)
214217
delete process.env.REACT_ON_RAILS_PRO_LICENSE;
215218

216-
// Second call should use cache
217-
expect(module.validateLicense()).toBe(true);
219+
// Second call should use cache and not fail
220+
module.validateLicense();
221+
expect(process.exit).not.toHaveBeenCalled();
218222
});
219223
});
220224

0 commit comments

Comments
 (0)