Skip to content

Commit 40aeb19

Browse files
Updated to enum, added examples and error msg
Signed-off-by: Aayush Chouhan <[email protected]>
1 parent d1bf2c1 commit 40aeb19

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/manage_nsfs/manage_nsfs_validations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async function validate_input_types(type, action, argv) {
8989
function validate_no_leading_space(argv_) {
9090
// checking if 'argv_' contains more then 2 values (i.e, type and action)
9191
if (argv_.length > 2) {
92-
const details = 'flag value is invalid - leading spaces or an empty value detected, ensure flag values are properly formatted.';
92+
const details = `flag value is invalid - leading spaces detected in: ${JSON.stringify(argv_.slice(2))}, ensure flag values are properly formatted.`;
9393
throw_cli_error(ManageCLIError.InvalidArgument, details);
9494
}
9595
}

src/test/unit_tests/jest_tests/test_nc_account_cli.test.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const timeout = 50000;
2323
const config = require('../../../../config');
2424
const config_fs_account_options = { show_secrets: true, decrypt_secret_key: true };
2525

26+
const quoted_type = Object.freeze({
27+
SPACE_ONLY: "s", // space only
28+
QUOTE_SPACE: "qs", // quote then space
29+
SPACE_QUOTE: "sq", // space then quote
30+
NONE: "none" // none
31+
});
32+
2633
// eslint-disable-next-line max-lines-per-function
2734
describe('manage nsfs cli account flow', () => {
2835
describe('cli create account', () => {
@@ -108,7 +115,7 @@ describe('manage nsfs cli account flow', () => {
108115
const account_options = { config_root, name, uid, gid}; // will add new_buckets_path with leading space later
109116
const action = ACTIONS.ADD;
110117
const command = create_command(type, action, account_options);
111-
const res = await exec_manage_cli_add_leading_space_option(command, 'new_buckets_path', new_buckets_path, 's'); // only space
118+
const res = await exec_manage_cli_add_leading_space_option(command, 'new_buckets_path', new_buckets_path, quoted_type.SPACE_ONLY); // only space
112119
expect(JSON.parse(res.stdout).error.code).toBe(ManageCLIError.InvalidArgument.code);
113120
});
114121

@@ -117,7 +124,7 @@ describe('manage nsfs cli account flow', () => {
117124
const account_options = { config_root, name, uid, gid}; // will add quoted new_buckets_path with leading space later
118125
const action = ACTIONS.ADD;
119126
const command = create_command(type, action, account_options);
120-
const res = await exec_manage_cli_add_leading_space_option(command, 'new_buckets_path', new_buckets_path, 'sq'); // space then quote
127+
const res = await exec_manage_cli_add_leading_space_option(command, 'new_buckets_path', new_buckets_path, quoted_type.SPACE_QUOTE); // space then quote
121128
expect(JSON.parse(res.stdout).error.code).toBe(ManageCLIError.InvalidArgument.code);
122129
});
123130

@@ -126,7 +133,7 @@ describe('manage nsfs cli account flow', () => {
126133
const account_options = { config_root, name, uid, gid}; // will add new_buckets_path with quoted leading space later
127134
const action = ACTIONS.ADD;
128135
const command = create_command(type, action, account_options);
129-
const res = await exec_manage_cli_add_leading_space_option(command, 'new_buckets_path', new_buckets_path, 'qs'); // quote then space
136+
const res = await exec_manage_cli_add_leading_space_option(command, 'new_buckets_path', new_buckets_path, quoted_type.QUOTE_SPACE); // quote then space
130137
expect(JSON.parse(res.stdout).error.code).toBe(ManageCLIError.InvalidAccountNewBucketsPath.code);
131138
});
132139

@@ -2250,19 +2257,36 @@ async function exec_manage_cli_add_empty_option(command, option) {
22502257
* @param {string} command
22512258
* @param {string} option
22522259
* @param {string} value
2253-
* @param {string} quoted
2260+
* @param {"s" | "qs" | "sq" | "none"} quoted
2261+
*
2262+
* @examples
2263+
* - await exec_manage_cli_add_leading_space_option("cli_command", "flag", "val", quoted_type.SPACE_ONLY);
2264+
* // cli_command --flag= val (Space before value)
2265+
*
2266+
* - await exec_manage_cli_add_leading_space_option("cli_command", "flag", "val", quoted_type.QUOTE_SPACE);
2267+
* // cli_command --flag=" val" (quote then space)
2268+
*
2269+
* - await exec_manage_cli_add_leading_space_option("cli_command", "flag", "val", quoted_type.SPACE_QUOTE);
2270+
* // cli_command --flag= "val" (space then quote)
2271+
*
2272+
* - await exec_manage_cli_add_leading_space_option("cli_command", "flag", "val", quoted_type.NONE);
2273+
* // cli_command --flag=val (No special formatting)
22542274
*/
22552275
async function exec_manage_cli_add_leading_space_option(command, option, value, quoted) {
22562276
let changed_command;
22572277

2258-
if (quoted === 's') { // only space
2259-
changed_command = command + ` --${option}= ${value}`;
2260-
} else if (quoted === 'qs') { // quote then space
2261-
changed_command = command + ` --${option}=" ${value}"`;
2262-
} else if (quoted === 'sq') { // space then quote
2263-
changed_command = command + ` --${option}= "${value}"`;
2264-
} else {
2265-
changed_command = command + ` --${option}=${value}`;
2278+
switch (quoted) {
2279+
case quoted_type.SPACE_ONLY:
2280+
changed_command = `${command} --${option}= ${value}`;
2281+
break;
2282+
case quoted_type.QUOTE_SPACE:
2283+
changed_command = `${command} --${option}=" ${value}"`;
2284+
break;
2285+
case quoted_type.SPACE_QUOTE:
2286+
changed_command = `${command} --${option}= "${value}"`;
2287+
break;
2288+
default:
2289+
changed_command = `${command} --${option}=${value}`;
22662290
}
22672291

22682292
let res;

0 commit comments

Comments
 (0)