Skip to content

Commit

Permalink
fix: Cleaned up file and env var evaluation.
Browse files Browse the repository at this point in the history
Signed-off-by: ebadiere <[email protected]>
  • Loading branch information
ebadiere committed Oct 24, 2024
1 parent 8a2731b commit e55a1a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
21 changes: 9 additions & 12 deletions packages/relay/src/lib/config/hbarSpendingPlanConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,18 @@ export class HbarSpendingPlanConfigService {
*/
private loadSpendingPlansConfig(): SpendingPlanConfig[] {
const configPath = findConfig(this.SPENDING_PLANS_CONFIG);
if (!configPath || !fs.existsSync(configPath)) {
try {
if (this.SPENDING_PLANS_CONFIG) {
return JSON.parse(this.SPENDING_PLANS_CONFIG) as SpendingPlanConfig[];
}
throw new Error('SPENDING_PLANS_CONFIG is undefined');
} catch (error: any) {
throw new Error(`Failed to parse JSON from HBAR_SPENDING_PLANS_CONFIG: ${error.message}`);
}
if (!this.SPENDING_PLANS_CONFIG) {
throw new Error('SPENDING_PLANS_CONFIG is undefined');
}
try {
const rawData = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(rawData) as SpendingPlanConfig[];
// Try to parse the value as a file path
if (configPath && fs.existsSync(configPath)) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8')) as SpendingPlanConfig[];
}
// If it's not a valid file path, try to parse it directly as JSON
return JSON.parse(this.SPENDING_PLANS_CONFIG) as SpendingPlanConfig[];
} catch (error: any) {
throw new Error(`Failed to parse JSON from ${configPath}: ${error.message}`);
throw new Error(`Failed to parse SPENDING_PLANS_CONFIG: ${error.message}`);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ describe('HbarSpendingPlanConfigService', function () {
});

describe('populatePreconfiguredSpendingPlans', function () {
// overrideEnvsInMochaDescribe({ HBAR_SPENDING_PLANS_CONFIG: spendingPlansConfig });
// overrideEnvsInMochaDescribe({ HBAR_SPENDING_PLANS_CONFIG: spendingPlansConfigFile });

describe('negative scenarios', function () {
it('should not throw an error if the configuration file is not found', async function () {
await expect(hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans()).not.to.be.rejected;
Expand All @@ -121,7 +118,7 @@ describe('HbarSpendingPlanConfigService', function () {
it('should throw an error if configuration file is not a parsable JSON', async function () {
sinon.stub(fs, 'readFileSync').returns('invalid JSON');
await expect(hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans()).to.be.rejectedWith(
`Failed to parse JSON from ${path}: Unexpected token 'i', "invalid JSON" is not valid JSON`,
`Failed to parse SPENDING_PLANS_CONFIG: Unexpected token 'i', "invalid JSON" is not valid JSON`,
);
});

Expand Down Expand Up @@ -568,7 +565,6 @@ describe('HbarSpendingPlanConfigService', function () {
overrideEnvsInMochaDescribe({ HBAR_SPENDING_PLANS_CONFIG: 'invalid JSON' });

it('should throw an error if environment variable contains invalid JSON', async function () {
console.log('process.env.HBAR_SPENDING_PLANS_CONFIG', process.env.HBAR_SPENDING_PLANS_CONFIG);
hbarSpendingPlanConfigService = await reloadHBarSpendingPlanConfigService(
hbarSpendingPlanConfigService,
logger,
Expand All @@ -577,7 +573,7 @@ describe('HbarSpendingPlanConfigService', function () {
ipAddressHbarSpendingPlanRepository,
);
await expect(hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans()).to.be.rejectedWith(
/Failed to parse JSON from HBAR_SPENDING_PLANS_CONFIG: /,
/Failed to parse SPENDING_PLANS_CONFIG: /,
);
});
});
Expand Down

0 comments on commit e55a1a4

Please sign in to comment.