diff --git a/tests/rules/test_dirty_untar.py b/tests/rules/test_dirty_untar.py index 2e1c5f07..21579c8e 100644 --- a/tests/rules/test_dirty_untar.py +++ b/tests/rules/test_dirty_untar.py @@ -1,7 +1,8 @@ import os import pytest import tarfile -from thefuck.rules.dirty_untar import match, get_new_command, side_effect +from thefuck.rules.dirty_untar import match, get_new_command, side_effect, \ + tar_extensions from tests.utils import Command @@ -32,34 +33,40 @@ def tar_error(tmpdir): return fixture -parametrize_filename = pytest.mark.parametrize('filename', [ - 'foo.tar', - 'foo.tar.gz', - 'foo.tgz']) +parametrize_extensions = pytest.mark.parametrize('ext', tar_extensions) + +# (filename as typed by the user, unquoted filename, quoted filename as per shells.quote) +parametrize_filename = pytest.mark.parametrize('filename, unquoted, quoted', [ + ('foo{}', 'foo{}', 'foo{}'), + ('foo\ bar{}', 'foo bar{}', "'foo bar{}'"), + ('"foo bar{}"', 'foo bar{}', "'foo bar{}'")]) parametrize_script = pytest.mark.parametrize('script, fixed', [ - ('tar xvf {}', 'mkdir -p foo && tar xvf {} -C foo'), - ('tar -xvf {}', 'mkdir -p foo && tar -xvf {} -C foo'), - ('tar --extract -f {}', 'mkdir -p foo && tar --extract -f {} -C foo')]) + ('tar xvf {}', 'mkdir -p {dir} && tar xvf {filename} -C {dir}'), + ('tar -xvf {}', 'mkdir -p {dir} && tar -xvf {filename} -C {dir}'), + ('tar --extract -f {}', 'mkdir -p {dir} && tar --extract -f {filename} -C {dir}')]) +@parametrize_extensions @parametrize_filename @parametrize_script -def test_match(tar_error, filename, script, fixed): - tar_error(filename) - assert match(Command(script=script.format(filename))) +def test_match(ext, tar_error, filename, unquoted, quoted, script, fixed): + tar_error(unquoted.format(ext)) + assert match(Command(script=script.format(filename.format(ext)))) +@parametrize_extensions @parametrize_filename @parametrize_script -def test_side_effect(tar_error, filename, script, fixed): - tar_error(filename) - side_effect(Command(script=script.format(filename)), None) - assert set(os.listdir('.')) == {filename, 'd'} - +def test_side_effect(ext, tar_error, filename, unquoted, quoted, script, fixed): + tar_error(unquoted.format(ext)) + side_effect(Command(script=script.format(filename.format(ext))), None) + assert set(os.listdir('.')) == {unquoted.format(ext), 'd'} +@parametrize_extensions @parametrize_filename @parametrize_script -def test_get_new_command(tar_error, filename, script, fixed): - tar_error(filename) - assert get_new_command(Command(script=script.format(filename))) == fixed.format(filename) +def test_get_new_command(ext, tar_error, filename, unquoted, quoted, script, fixed): + tar_error(unquoted.format(ext)) + assert (get_new_command(Command(script=script.format(filename.format(ext)))) + == fixed.format(dir=quoted.format(''), filename=filename.format(ext))) diff --git a/thefuck/rules/dirty_untar.py b/thefuck/rules/dirty_untar.py index 4f10e3be..2b5090f5 100644 --- a/thefuck/rules/dirty_untar.py +++ b/thefuck/rules/dirty_untar.py @@ -4,6 +4,11 @@ from thefuck import shells from thefuck.utils import for_app +tar_extensions = ('.tar', '.tar.Z', '.tar.bz2', '.tar.gz', '.tar.lz', + '.tar.lzma', '.tar.xz', '.taz', '.tb2', '.tbz', '.tbz2', + '.tgz', '.tlz', '.txz', '.tz') + + def _is_tar_extract(cmd): if '--extract' in cmd: return True @@ -14,11 +19,8 @@ def _is_tar_extract(cmd): def _tar_file(cmd): - tar_extensions = ('.tar', '.tar.Z', '.tar.bz2', '.tar.gz', '.tar.lz', - '.tar.lzma', '.tar.xz', '.taz', '.tb2', '.tbz', '.tbz2', - '.tgz', '.tlz', '.txz', '.tz') - for c in cmd.split(): + for c in cmd: for ext in tar_extensions: if c.endswith(ext): return (c, c[0:len(c) - len(ext)]) @@ -28,16 +30,17 @@ def _tar_file(cmd): def match(command): return ('-C' not in command.script and _is_tar_extract(command.script) - and _tar_file(command.script) is not None) + and _tar_file(command.split_script) is not None) def get_new_command(command): + dir = shells.quote(_tar_file(command.split_script)[1]) return shells.and_('mkdir -p {dir}', '{cmd} -C {dir}') \ - .format(dir=_tar_file(command.script)[1], cmd=command.script) + .format(dir=dir, cmd=command.script) def side_effect(old_cmd, command): - with tarfile.TarFile(_tar_file(old_cmd.script)[0]) as archive: + with tarfile.TarFile(_tar_file(old_cmd.split_script)[0]) as archive: for file in archive.getnames(): try: os.remove(file)