Skip to content

Commit

Permalink
fixed --url bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Dec 11, 2024
1 parent 84d2feb commit af3fe71
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
24 changes: 22 additions & 2 deletions cat_win/src/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from cat_win.src.const.regex import RE_Q_REPLACE, RE_R_EPLACE, RE_Q_TRUNC, RE_T_RUNC
from cat_win.src.const.regex import RE_CUT, RE_REPLACE, RE_REPLACE_COMMA
from cat_win.src.service.helper.environment import on_windows_os
from cat_win.src.web.urls import sep_valid_urls

IS_FILE, IS_DIR, IS_PATTERN = range(0, 3)

Expand Down Expand Up @@ -98,7 +99,26 @@ def get_arguments(self, argv: list, delete: bool = False) -> tuple:
echo_args = echo_args.encode(self.file_encoding).decode('unicode_escape').encode('latin-1').decode(self.file_encoding)
except UnicodeError:
pass
return (self._args, self._unknown_args, self._unknown_files, echo_args)
return (self._args, self._unknown_args, echo_args)

def filter_urls(self, do_filter: bool) -> tuple:
"""
filter the unknown files for valid urls
Parameters:
do_filter (bool):
indicates if urls should be filtered
Returns:
(unknown_files, valid_urls) (tuple):
contains the unknown files and the valid urls
"""
if not do_filter:
unknown_files = [Path(file) for file in self._unknown_files]
return (unknown_files, [])
unknown_files, valid_urls = sep_valid_urls(self._unknown_files)
unknown_files = [Path(file) for file in unknown_files]
return (unknown_files, valid_urls)

def get_files(self, dot_files: bool = False) -> list:
"""
Expand Down Expand Up @@ -322,7 +342,7 @@ def _add_argument(self, param: str, delete: bool = False) -> bool:
# out of bound is not possible, in case of length 0 param, possible_path would have
# become the working-path and therefor handled the param as a directory
elif param[0] != '-':
self._unknown_files.append(Path(param))
self._unknown_files.append(param)
else:
self._unknown_args.append(param)
return False
Expand Down
8 changes: 3 additions & 5 deletions cat_win/src/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from cat_win.src.service.summary import Summary
from cat_win.src.service.visualizer import Visualizer
from cat_win.src.web.updatechecker import print_update_information
from cat_win.src.web.urls import sep_valid_urls, read_url
from cat_win.src.web.urls import read_url
from cat_win import __project__, __version__, __sysversion__, __author__, __url__


Expand Down Expand Up @@ -981,14 +981,12 @@ def init(repl: bool = False) -> tuple:
setup()

# read parameter-args
args, _, unknown_files, echo_args = arg_parser.get_arguments(config.get_args(sys.argv[:]))
args, _, echo_args = arg_parser.get_arguments(config.get_args(sys.argv[:]))

u_args.set_args(args)

known_files = arg_parser.get_files(u_args[ARGS_DOTFILES])
valid_urls = []
if u_args[ARGS_URI]:
valid_urls, unknown_files = sep_valid_urls(unknown_files)
unknown_files, valid_urls = arg_parser.filter_urls(u_args[ARGS_URI])

if u_args[ARGS_RECONFIGURE] or u_args[ARGS_RECONFIGURE_IN]:
sys.stdin.reconfigure(encoding=arg_parser.file_encoding)
Expand Down
2 changes: 1 addition & 1 deletion cat_win/src/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def sep_valid_urls(to_check: list) -> tuple:
else:
not_valid_urls.append(url)

return (valid_urls, not_valid_urls)
return (not_valid_urls, valid_urls)

def read_url(url: str, _rec: bool = False) -> bytes:
"""
Expand Down
33 changes: 13 additions & 20 deletions cat_win/tests/src/test_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,54 @@ class TestArgParser(TestCase):

def test_get_arguments_empty(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT'])
args, unknown_args, echo_args = arg_parser.get_arguments(['CAT'])
known_files = arg_parser.get_files()
self.assertCountEqual(args, [])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, [])

def test_get_arguments_duplicate(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', '-n', '-n', '-c'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '-c', '-n'])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, [])

def test_get_arguments_concatenated(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', '-abcef'])
args, unknown_args, echo_args = arg_parser.get_arguments(['CAT', '-abcef'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-a', '-b', '-c', '-e', '-f'])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, [])

def test_get_arguments_concatenated_unknown(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', '-+abcefϵg'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-a', '-b', '-c', '-e', '-f', '-g'])
self.assertCountEqual(unknown_args, ['-+', '-ϵ'])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, [])

def test_get_arguments_invalid(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', '--abcdefg'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, [])
self.assertCountEqual(unknown_args, ['--abcdefg'])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, [])

def test_get_arguments_encoding(self):
Expand Down Expand Up @@ -117,26 +112,25 @@ def test_get_arguments_cut(self):
def test_get_arguments_replace(self):
arg_parser = ArgParser()

args, _, _, _ = arg_parser.get_arguments(['CAT', '[test,404]'])
args, _, _ = arg_parser.get_arguments(['CAT', '[test,404]'])
self.assertCountEqual(args, [(ARGS_REPLACE, '[test,404]')])

args, _, _, _ = arg_parser.get_arguments(['CAT', '[test\\,,404]'])
args, _, _ = arg_parser.get_arguments(['CAT', '[test\\,,404]'])
self.assertCountEqual(args, [(ARGS_REPLACE, '[test\\,,404]')])

self.assertEqual(arg_parser.file_replace_mapping['[test\\,,404]'], ('test,', '404'))
args, _, _, _ = arg_parser.get_arguments(['CAT', '[\\n\\t,\\,\f]'])
args, _, _ = arg_parser.get_arguments(['CAT', '[\\n\\t,\\,\f]'])
self.assertCountEqual(args, [(ARGS_REPLACE, '[\\n\\t,\\,\x0c]')])
self.assertEqual(arg_parser.file_replace_mapping['[\\n\\t,\\,\x0c]'], ('\n\t', ',\x0c'))

def test_get_arguments_dir(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', test_text_file_dir])
known_files = arg_parser.get_files()
self.assertCountEqual(args, [])
self.assertCountEqual(unknown_args, [])
self.assertGreaterEqual(len(known_files), 7)
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, [])

def test_get_arguments_files_equal_dir(self):
Expand All @@ -149,8 +143,9 @@ def test_get_arguments_files_equal_dir(self):

def test_get_arguments_unknown_file(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', 'testTesttest', 'test-file.txt'])
unknown_files, _ = arg_parser.filter_urls(False)
known_files = arg_parser.get_files()
self.assertCountEqual(args, [])
self.assertCountEqual(unknown_args, [])
Expand All @@ -167,26 +162,24 @@ def test_get_arguments_unknown_args(self):

def test_get_arguments_echo_args(self):
arg_parser = ArgParser(unicode_echo=False)
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', '-n', '--echo', '-n', 'random', test_text_file_dir])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '--echo'])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertEqual(echo_args, '-n random ' + test_text_file_dir)

def test_get_arguments_echo_args_escaped(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
args, unknown_args, echo_args = arg_parser.get_arguments(
['CAT', '-n', '-E', '-n', '\\n', 'random'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '-E'])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertEqual(echo_args, '-n \n random')

def test_get_arguments_echo_args_recursive(self):
Expand Down
2 changes: 1 addition & 1 deletion cat_win/tests/src/web/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_sep_valid_urls(self):
]
test_input = valid_expected+invalid_expected
random.shuffle(test_input)
valid_output, invalid_output = sep_valid_urls(test_input)
invalid_output, valid_output = sep_valid_urls(test_input)
self.assertCountEqual(valid_expected, valid_output)
self.assertCountEqual(invalid_expected, invalid_output)

Expand Down

0 comments on commit af3fe71

Please sign in to comment.