2015-07-25 22:37:41 +02:00
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
import tarfile
|
2015-10-28 15:12:59 +01:00
|
|
|
from thefuck.rules.dirty_untar import match, get_new_command, side_effect, \
|
2016-10-06 14:51:22 -04:00
|
|
|
tar_extensions # noqa: E126
|
2017-08-31 17:58:56 +02:00
|
|
|
from thefuck.types import Command
|
2015-07-25 22:37:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tar_error(tmpdir):
|
|
|
|
def fixture(filename):
|
|
|
|
path = os.path.join(str(tmpdir), filename)
|
|
|
|
|
|
|
|
def reset(path):
|
2015-09-07 20:48:10 +02:00
|
|
|
os.mkdir('d')
|
2015-07-25 22:37:41 +02:00
|
|
|
with tarfile.TarFile(path, 'w') as archive:
|
2015-09-07 20:48:10 +02:00
|
|
|
for file in ('a', 'b', 'c', 'd/e'):
|
2015-07-25 22:37:41 +02:00
|
|
|
with open(file, 'w') as f:
|
|
|
|
f.write('*')
|
|
|
|
|
|
|
|
archive.add(file)
|
|
|
|
|
|
|
|
os.remove(file)
|
|
|
|
|
|
|
|
with tarfile.TarFile(path, 'r') as archive:
|
|
|
|
archive.extractall()
|
|
|
|
|
|
|
|
os.chdir(str(tmpdir))
|
|
|
|
reset(path)
|
|
|
|
|
2015-09-07 20:48:10 +02:00
|
|
|
assert set(os.listdir('.')) == {filename, 'a', 'b', 'c', 'd'}
|
|
|
|
assert set(os.listdir('./d')) == {'e'}
|
2015-07-25 22:37:41 +02:00
|
|
|
|
|
|
|
return fixture
|
|
|
|
|
2017-03-08 19:53:54 -05:00
|
|
|
|
2015-10-28 15:12:59 +01:00
|
|
|
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{}'")])
|
2015-07-25 22:37:41 +02:00
|
|
|
|
|
|
|
parametrize_script = pytest.mark.parametrize('script, fixed', [
|
2015-10-28 15:12:59 +01:00
|
|
|
('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}')])
|
2015-07-25 22:37:41 +02:00
|
|
|
|
2015-08-17 13:44:15 +02:00
|
|
|
|
2015-10-28 15:12:59 +01:00
|
|
|
@parametrize_extensions
|
2015-07-25 22:37:41 +02:00
|
|
|
@parametrize_filename
|
|
|
|
@parametrize_script
|
2015-10-28 15:12:59 +01:00
|
|
|
def test_match(ext, tar_error, filename, unquoted, quoted, script, fixed):
|
|
|
|
tar_error(unquoted.format(ext))
|
2017-08-31 17:58:56 +02:00
|
|
|
assert match(Command(script.format(filename.format(ext)), ''))
|
2015-07-25 22:37:41 +02:00
|
|
|
|
|
|
|
|
2015-10-28 15:12:59 +01:00
|
|
|
@parametrize_extensions
|
2015-07-25 22:37:41 +02:00
|
|
|
@parametrize_filename
|
|
|
|
@parametrize_script
|
2015-10-28 15:12:59 +01:00
|
|
|
def test_side_effect(ext, tar_error, filename, unquoted, quoted, script, fixed):
|
|
|
|
tar_error(unquoted.format(ext))
|
2017-08-31 17:58:56 +02:00
|
|
|
side_effect(Command(script.format(filename.format(ext)), ''), None)
|
2015-10-28 15:12:59 +01:00
|
|
|
assert set(os.listdir('.')) == {unquoted.format(ext), 'd'}
|
2015-07-25 22:37:41 +02:00
|
|
|
|
2015-11-15 18:02:37 +01:00
|
|
|
|
2015-10-28 15:12:59 +01:00
|
|
|
@parametrize_extensions
|
2015-07-25 22:37:41 +02:00
|
|
|
@parametrize_filename
|
|
|
|
@parametrize_script
|
2015-10-28 15:12:59 +01:00
|
|
|
def test_get_new_command(ext, tar_error, filename, unquoted, quoted, script, fixed):
|
|
|
|
tar_error(unquoted.format(ext))
|
2017-08-31 17:58:56 +02:00
|
|
|
assert (get_new_command(Command(script.format(filename.format(ext)), ''))
|
2015-10-28 15:12:59 +01:00
|
|
|
== fixed.format(dir=quoted.format(''), filename=filename.format(ext)))
|