Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🐛 Add edge-case scenario handling for getLogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Dec 2, 2023
1 parent 8381088 commit f28f696
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export const getNetworkByNetworkID = (_networkID: string): string | Error => {

export const getLogLevel = (loggerConfig: LoggerConfig): string => {
const highestLogPriority = Math.max(
LOG_LEVEL_PRIORITY[String(loggerConfig.fileLogLevel).toUpperCase()],
LOG_LEVEL_PRIORITY[String(loggerConfig.consoleLogLevel).toUpperCase()],
LOG_LEVEL_PRIORITY[String(loggerConfig.fileLogLevel || '').toUpperCase()] ??
LOG_LEVEL_PRIORITY.INFO,
LOG_LEVEL_PRIORITY[String(loggerConfig.consoleLogLevel || '').toUpperCase()] ??
LOG_LEVEL_PRIORITY.INFO,
);

try {
Expand Down
38 changes: 37 additions & 1 deletion test/unit/utils/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import { configV3, configV4 } from '../fixtures/config';
import { NETWORK_CONSTANT } from '../../../src/constants';
import {
getNetworkByNetworkID,
getLogLevel,
migrateUserConfig,
validateConfig,
writeConfig,
resolveConfigPathByNetworkID,
createBackup,
getConfig,
} from '../../../src/utils/config';
import { ApplicationConfigV3 } from '../../../src/types';
import { ApplicationConfigV3, LoggerConfig } from '../../../src/types';

const migratedConfigFilePath = join(__dirname, 'test/config');
const expectedBackupPath = join(__dirname, '../../..', 'backup');
Expand All @@ -38,6 +39,41 @@ const mockCommand = {
error,
};

describe('Test getLogLevel method', () => {
it('should return highest priority logLevel provided by user', async () => {
const loggerConfig = {
fileLogLevel: 'trace',
consoleLogLevel: 'error',
} as LoggerConfig;
const logLevel = getLogLevel(loggerConfig);
expect(logLevel).toBe('trace');
});

it('should return info when logger config is not available', async () => {
const loggerConfig = {} as LoggerConfig;
const logLevel = getLogLevel(loggerConfig);
expect(logLevel).toBe('info');
});

it('should return the highest priority log level when one of the specified logLevel is incorrect', async () => {
const loggerConfig = {
fileLogLevel: 'trace',
consoleLogLevel: 'err',
} as LoggerConfig;
const logLevel = getLogLevel(loggerConfig);
expect(logLevel).toBe('trace');
});

it('should return info if the specified valid log level is of lower priority than info and other is incorrect', async () => {
const loggerConfig = {
fileLogLevel: 'fatal',
consoleLogLevel: 'err',
} as LoggerConfig;
const logLevel = getLogLevel(loggerConfig);
expect(logLevel).toBe('info');
});
});

describe('Migrate user configuration', () => {
afterAll(() => {
fs.removeSync(migratedConfigFilePath);
Expand Down

0 comments on commit f28f696

Please sign in to comment.