Skip to content

Commit

Permalink
fix(input check): set input defaults appropriately
Browse files Browse the repository at this point in the history
Checks and sets the input to the corresponding type/format required for successful execution. Moved
the checks into its own function for handling.
  • Loading branch information
vimalnathnambiar committed Mar 28, 2024
1 parent 6e4999b commit 67e112a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
24 changes: 24 additions & 0 deletions src/__tests__/setInquirerDefaults.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, test, expect } from 'vitest';

import { setInquirerDefaults } from '../utils/setInquirerDefaults';

/**
* To test setInquirerDefaults()
* Input: configParam (Object)
* Output: configParam (Object)
*/
describe('setInquirerDefaults', () => {
// Dummy data
const testConfigParam = {
outputFormat: ['JSON'],
msLevel: '1 2',
};

// Tests
test('configure configParam appropriately', async () => {
const configParam = await setInquirerDefaults(testConfigParam);

expect(configParam.outputFormat).toStrictEqual('JSON');
expect(configParam.msLevel).toStrictEqual([1, 2]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ import { join, basename } from 'path';

import { describe, test, expect, beforeAll, afterAll } from 'vitest';

import { setDefaults } from '../utils/setDefaults.js';
import { setYargsDefaults } from '../utils/setYargsDefaults.js';

/**
* To test setDefaults()
* To test setYargsDefaults()
* Input: argv (Yargs)
* Output: configParam (Object) || Error message (Error)
*/
describe('setDefaults', () => {
describe('setYargsDefaults', () => {
// Dummy data
/**
* @type {Yargs}
*/
const testArgv = {
interactive: false,
inputDirectory: './.tmp/setDefaults/inputDirectory/',
inputDirectory: './.tmp/setYargsDefaults/inputDirectory/',
fileList: ['*'],
outputFormat: ['JSON'],
outputDirectory: join(
homedir(),
'/exfilms/outputFormat/inputDirectoryName/',
),
logDirectory: './.tmp/setDefaults/logDirectory/',
logDirectory: './.tmp/setYargsDefaults/logDirectory/',
decimalPlace: NaN,
targeted: false,
targetFile: './.tmp/setDefaults/targetFile.tsv',
targetFile: './.tmp/setYargsDefaults/targetFile.tsv',
mzTolerance: 0.005,
ppmTolerance: 5,
mzRange: false,
Expand All @@ -60,7 +60,7 @@ describe('setDefaults', () => {

// Tests
test('return configParam: using default values', async () => {
const configParam = await setDefaults(testArgv);
const configParam = await setYargsDefaults(testArgv);
expect(configParam.inputDirectory).toStrictEqual(testArgv.inputDirectory);
expect(configParam.fileList).toStrictEqual([
'testFile1.mzML',
Expand Down Expand Up @@ -95,11 +95,11 @@ describe('setDefaults', () => {

test('return configParam: using defined values', async () => {
testArgv.fileList = ['testFile1.mzML'];
testArgv.outputDirectory = './.tmp/setDefaults/outputDirectory/';
testArgv.outputDirectory = './.tmp/setYargsDefaults/outputDirectory/';
testArgv.targeted = true;
testArgv.mzRange = true;
testArgv.filterSpectrumData = true;
const configParam = await setDefaults(testArgv);
const configParam = await setYargsDefaults(testArgv);
expect(configParam.fileList).toStrictEqual(testArgv.fileList);
expect(configParam.outputDirectory).toStrictEqual(testArgv.outputDirectory);
expect(configParam.targetFile).toStrictEqual(testArgv.targetFile);
Expand All @@ -116,13 +116,13 @@ describe('setDefaults', () => {
test('throw error: listMZML() input type check', async () => {
testArgv.inputDirectory = 0;
testArgv.fileList = ['*'];
await expect(setDefaults(testArgv)).rejects.toThrowError(
await expect(setYargsDefaults(testArgv)).rejects.toThrowError(
'listMZML(): directory must be of type string',
);
});

// Clean up test environment after tests
afterAll(() => {
rmSync('./.tmp/setDefaults/', { recursive: true });
rmSync('./.tmp/setYargsDefaults/', { recursive: true });
});
});
8 changes: 3 additions & 5 deletions src/exfilms.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createDefaultDirectories } from './utils/createDefaultDirectories.js';
import { prompts } from './utils/inquirerPrompts.js';
import { parseMZML } from './utils/parseMZML.js';
import { setForSpectraFiltering } from './utils/setForSpectraFiltering.js';
import { setInquirerDefaults } from './utils/setInquirerDefaults.js';
import { writeLog } from './utils/writeLog.js';
import { yargsCheck } from './utils/yargsCheck.js';
import { argv } from './utils/yargsConfig.js';
Expand Down Expand Up @@ -52,11 +53,8 @@ figlet('ExfilMS', async (err, data) => {
// If interactive mode
configParam = await inquirer.prompt(prompts);

// Configure some parameters to match argv setDefault() settings format
configParam.outputFormat = configParam.outputFormat[0];
if (configParam.msLevel) {
configParam.msLevel = configParam.msLevel.split(' ').map(Number);
}
// Set up configuration parameters appropriately
configParam = await setInquirerDefaults(configParam);
console.log('');
} else {
// Check yargs arguments and set up appropriately
Expand Down
15 changes: 15 additions & 0 deletions src/utils/setInquirerDefaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Set the configuration parameters based on Inquirer input.
* @param {Object} configParam Configuration parameters received from the Inquirer prompts
* @returns {Promise<Object>} A promise that resolves with an object containing the configuration parameters in the required format for execution.
*/
export async function setInquirerDefaults(configParam) {
// Set output format as a string instead on an array
configParam.outputFormat = configParam.outputFormat[0];

// Set MS level as a number array instead of a string
if (configParam.msLevel) {
configParam.msLevel = configParam.msLevel.split(' ').map(Number);
}
return configParam;
}
8 changes: 4 additions & 4 deletions src/utils/setDefaults.js → src/utils/setYargsDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { join, basename } from 'path';
import { listMZML } from './listMZML.js';

/**
* Set the default values for configuration parameters.
* Set the configuration parameters based on Yargs input.
* @param {Yargs} argv Yargs command line arguments.
* @returns {Promise<Object>} A promise that resolves with an object containing the default values for the configuration parameters.
* @throws {Error} Throws error if setDefaults() encounters issues in its process.
* @returns {Promise<Object>} A promise that resolves with an object containing the configuration parameters in the required format for execution.
* @throws {Error} Throws error if setYargsDefaults() encounters issues in its process.
*/
export async function setDefaults(argv) {
export async function setYargsDefaults(argv) {
let configParam = {};

// General config
Expand Down
4 changes: 2 additions & 2 deletions src/utils/yargsCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @typedef {import('../typedef/index.mjs').Yargs} Yargs
*/

import { setDefaults } from './setDefaults.js';
import { setYargsDefaults } from './setYargsDefaults.js';

/**
* Check if CLI arguments are used correctly
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function yargsCheck(argv) {
);
} else {
// Set default values (where required)
configParam = await setDefaults(argv);
configParam = await setYargsDefaults(argv);
}

return configParam;
Expand Down

0 comments on commit 67e112a

Please sign in to comment.