mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-22 12:58:33 +00:00
Fix the untar rules and filenames with spaces
This commit is contained in:
parent
ecfc180280
commit
0a6a3db65d
@ -1,7 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
import tarfile
|
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
|
from tests.utils import Command
|
||||||
|
|
||||||
|
|
||||||
@ -32,34 +33,40 @@ def tar_error(tmpdir):
|
|||||||
|
|
||||||
return fixture
|
return fixture
|
||||||
|
|
||||||
parametrize_filename = pytest.mark.parametrize('filename', [
|
parametrize_extensions = pytest.mark.parametrize('ext', tar_extensions)
|
||||||
'foo.tar',
|
|
||||||
'foo.tar.gz',
|
# (filename as typed by the user, unquoted filename, quoted filename as per shells.quote)
|
||||||
'foo.tgz'])
|
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', [
|
parametrize_script = pytest.mark.parametrize('script, fixed', [
|
||||||
('tar xvf {}', 'mkdir -p foo && tar xvf {} -C foo'),
|
('tar xvf {}', 'mkdir -p {dir} && tar xvf {filename} -C {dir}'),
|
||||||
('tar -xvf {}', 'mkdir -p foo && tar -xvf {} -C foo'),
|
('tar -xvf {}', 'mkdir -p {dir} && tar -xvf {filename} -C {dir}'),
|
||||||
('tar --extract -f {}', 'mkdir -p foo && tar --extract -f {} -C foo')])
|
('tar --extract -f {}', 'mkdir -p {dir} && tar --extract -f {filename} -C {dir}')])
|
||||||
|
|
||||||
|
|
||||||
|
@parametrize_extensions
|
||||||
@parametrize_filename
|
@parametrize_filename
|
||||||
@parametrize_script
|
@parametrize_script
|
||||||
def test_match(tar_error, filename, script, fixed):
|
def test_match(ext, tar_error, filename, unquoted, quoted, script, fixed):
|
||||||
tar_error(filename)
|
tar_error(unquoted.format(ext))
|
||||||
assert match(Command(script=script.format(filename)))
|
assert match(Command(script=script.format(filename.format(ext))))
|
||||||
|
|
||||||
|
|
||||||
|
@parametrize_extensions
|
||||||
@parametrize_filename
|
@parametrize_filename
|
||||||
@parametrize_script
|
@parametrize_script
|
||||||
def test_side_effect(tar_error, filename, script, fixed):
|
def test_side_effect(ext, tar_error, filename, unquoted, quoted, script, fixed):
|
||||||
tar_error(filename)
|
tar_error(unquoted.format(ext))
|
||||||
side_effect(Command(script=script.format(filename)), None)
|
side_effect(Command(script=script.format(filename.format(ext))), None)
|
||||||
assert set(os.listdir('.')) == {filename, 'd'}
|
assert set(os.listdir('.')) == {unquoted.format(ext), 'd'}
|
||||||
|
|
||||||
|
|
||||||
|
@parametrize_extensions
|
||||||
@parametrize_filename
|
@parametrize_filename
|
||||||
@parametrize_script
|
@parametrize_script
|
||||||
def test_get_new_command(tar_error, filename, script, fixed):
|
def test_get_new_command(ext, tar_error, filename, unquoted, quoted, script, fixed):
|
||||||
tar_error(filename)
|
tar_error(unquoted.format(ext))
|
||||||
assert get_new_command(Command(script=script.format(filename))) == fixed.format(filename)
|
assert (get_new_command(Command(script=script.format(filename.format(ext))))
|
||||||
|
== fixed.format(dir=quoted.format(''), filename=filename.format(ext)))
|
||||||
|
@ -4,6 +4,11 @@ from thefuck import shells
|
|||||||
from thefuck.utils import for_app
|
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):
|
def _is_tar_extract(cmd):
|
||||||
if '--extract' in cmd:
|
if '--extract' in cmd:
|
||||||
return True
|
return True
|
||||||
@ -14,11 +19,8 @@ def _is_tar_extract(cmd):
|
|||||||
|
|
||||||
|
|
||||||
def _tar_file(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:
|
for ext in tar_extensions:
|
||||||
if c.endswith(ext):
|
if c.endswith(ext):
|
||||||
return (c, c[0:len(c) - len(ext)])
|
return (c, c[0:len(c) - len(ext)])
|
||||||
@ -28,16 +30,17 @@ def _tar_file(cmd):
|
|||||||
def match(command):
|
def match(command):
|
||||||
return ('-C' not in command.script
|
return ('-C' not in command.script
|
||||||
and _is_tar_extract(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):
|
def get_new_command(command):
|
||||||
|
dir = shells.quote(_tar_file(command.split_script)[1])
|
||||||
return shells.and_('mkdir -p {dir}', '{cmd} -C {dir}') \
|
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):
|
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():
|
for file in archive.getnames():
|
||||||
try:
|
try:
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user