Skip to content

Commit

Permalink
Added tests and better handling in get_list_args
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyE123 committed May 29, 2024
1 parent 2636ea5 commit ad7a52f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
37 changes: 28 additions & 9 deletions megalinter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,36 @@ def get_list(request_id, config_var, default=None):
return default


# Retrieve a configuration variable as a list of arguments, handling various input formats.
def get_list_args(request_id, config_var, default=None):
# Retrieve the variable from the configuration
var = get(request_id, config_var, None)
if var is not None:
if isinstance(var, list):
return var
if var == "":
return []
if " " not in var:
return [var]
return shlex.split(var)
return default

# Check if the variable is None and return the default value if true
if var is None:
return default

# Check if the variable is an empty string and return an empty list if true
if var == "":
return []

# Check if the variable is an integer or a decimal and return it as a list
if isinstance(var, (int, float)):
return [str(var)]

# Check if the variable is a list and return it if true
if isinstance(var, list):
return var
# Check if the variable is a string and return it as a list if it does not contain spaces
if isinstance(var, str) and var.strip() is "":
return []

# Check if the variable is a string and return it as a list if it does not contain spaces
if isinstance(var, str) and " " not in var.strip():
return [var]

# Otherwise, split the string using shlex and return the result
return shlex.split(var)


def set_value(request_id, config_var, val):
Expand Down
56 changes: 55 additions & 1 deletion megalinter/tests/test_megalinter/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from megalinter.constants import ML_REPO
from megalinter.MegaLinter import Megalinter
from megalinter.utils import REPO_HOME_DEFAULT

from unittest.mock import patch

class config_test(unittest.TestCase):
repository = None
Expand Down Expand Up @@ -481,3 +481,57 @@ def get_repository(self):

def get_branch(self):
return os.environ.get("GITHUB_BRANCH", "main")

def test_get_list_args(self):
# This is a test to check the functionality of the get_list_args method.
# It mocks the behavior of the get method within this method and asserts the expected output.
scenarios = [
(1, "none_value", None, None),
(2, "single_boolean_true", True, ['True']),
(3, "single_boolean_false", False, ['False']),
(4, "integer_value", 42, ['42']),
(5, "float_value", 3.14, ['3.14']),
(6, "empty_list", [], []),
(7, "empty_string", "", []),
(8, "single_space", ' ', []),
(9, "double_space", ' ', []),
(10, "empty_quoted_string", '""', ['""']),
(11, "non_empty_list", ["item1", "item2"], ['item1', 'item2']),
(12, "single_quoted_string", "'single_quoted_string'", ["'single_quoted_string'"]),
(13, "space_separated_items", 'item1 item2', ['item1', 'item2']),
(14, "multiple_single_quoted_strings", "'string1' 'string2' 'string3'", ['string1', 'string2', 'string3']),
(15, "mixed_single_and_double_quotes", '"double quoted" \'single quoted\'', ['double quoted', 'single quoted']),
(16, "three_space_separated_items", "item1 item2 item3", ['item1', 'item2', 'item3']),
(17, "four_space_separated_items", "item1 item2 item3 item4", ['item1', 'item2', 'item3', 'item4']),
(18, "five_space_separated_items", "item1 item2 item3 item4 item5", ['item1', 'item2', 'item3', 'item4', 'item5']),
(19, "single_quoted_with_spaces", "' single quoted with spaces '", [" single quoted with spaces "]),
(20, " quoted_with_leading_space", '" leading space"', [" leading space"]),
(21, "quoted_with_trailing_space", '"trailing space "', ["trailing space "]),
(22, "quoted_with_leading_and_trailing_spaces", '" leading and trailing spaces "', [" leading and trailing spaces "]),
(23, "multiple_quoted_strings_with_spaces", '" string1 " "string2 " " string3 "', [" string1 ", "string2 ", " string3 "]),
(24, "paths_with_nested_folders", "./nested/folder/file.txt", ['./nested/folder/file.txt']),
(25, "paths_with_parent_directory", "../parent/file.txt", ['../parent/file.txt']),
(26, "paths_with_tilde_home", "~/home/user/file.txt", ['~/home/user/file.txt']),
(27, "paths_with_url_files", "http://example.com/file.txt", ['http://example.com/file.txt']),
(28, "multiple_spaces_between_paths", 'path1 path2', ['path1', 'path2']),
(29, "relative_paths", "./relative/path ./another/relative/path", ['./relative/path', './another/relative/path']),
(30, "paths_with_file_extensions", "./file.txt ./folder/file.py", ['./file.txt', './folder/file.py']),
(31, "paths_with_hidden_files", "./folder/.file.sln ./folder/.hiddenfile", ['./folder/.file.sln', './folder/.hiddenfile']),
(32, "absolute_unix_paths", "/root/path /another/root/path", ['/root/path', '/another/root/path']),
(33, "quoted_paths_with_spaces", '"quoted path/with spaces" "another/quoted path"', ['quoted path/with spaces', 'another/quoted path']),
(34, "paths_with_url_and_local_files", "http://example.com/file.txt ./local/file.txt", ['http://example.com/file.txt', './local/file.txt']),
(35, "mixed_quotes_and_spaces", '"quoted item1" item2 \'quoted item3\' item4', ['quoted item1', 'item2', 'quoted item3', 'item4']),
(36, "command_with_options", 'command --option="value with spaces" --flag', ['command', '--option=value with spaces', "--flag"])

# Commented out cases due to shlex.split removing the /
# (37, "absolute_windows_paths", "C:\\absolute\\path C:\\another\\absolute\\path", ['C:\\absolute\\path', 'C:\\another\\absolute\\path']),
# (38, "paths_with_environment_variables", "$HOME/file.txt $USERPROFILE\\file.txt", ['$HOME/file.txt', '$USERPROFILE\\file.txt']),
# (39, "path_with_mixed_separators", "path/to/file path\\to\\another\\file", ['path/to/file', 'path\\to\\another\\file']),
# (40, "complex_paths_and_files", 'file1 "complex path/file2" file3\\with\\backslashes', ['file1', 'complex path/file2', 'file3\\with\\backslashes']),
]

for scenario_number, scenario_name, return_value, expected_result in scenarios:
with self.subTest(scenario_number=scenario_number, scenario_name=scenario_name):
with patch.object(config, 'get', return_value=return_value):
result = config.get_list_args('dummy_request_id', scenario_name)
self.assertEqual(result, expected_result, f"Failed on result scenario {scenario_number}: {scenario_name}")

0 comments on commit ad7a52f

Please sign in to comment.