2015-11-04 23:08:13 -02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-07-25 00:55:01 +02:00
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
import zipfile
|
|
|
|
from thefuck.rules.dirty_unzip import match, get_new_command, side_effect
|
2017-08-31 17:58:56 +02:00
|
|
|
from thefuck.types import Command
|
2015-11-04 23:08:13 -02:00
|
|
|
from unicodedata import normalize
|
2015-07-25 00:55:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def zip_error(tmpdir):
|
2015-11-04 23:08:13 -02:00
|
|
|
def zip_error_inner(filename):
|
|
|
|
path = os.path.join(str(tmpdir), filename)
|
|
|
|
|
|
|
|
def reset(path):
|
|
|
|
with zipfile.ZipFile(path, 'w') as archive:
|
|
|
|
archive.writestr('a', '1')
|
|
|
|
archive.writestr('b', '2')
|
|
|
|
archive.writestr('c', '3')
|
2015-07-25 00:55:01 +02:00
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
archive.writestr('d/e', '4')
|
2015-07-25 00:55:01 +02:00
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
archive.extractall()
|
2015-09-07 20:48:10 +02:00
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
os.chdir(str(tmpdir))
|
|
|
|
reset(path)
|
2015-07-25 00:55:01 +02:00
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
dir_list = os.listdir(u'.')
|
|
|
|
if filename not in dir_list:
|
|
|
|
filename = normalize('NFD', filename)
|
2015-07-25 00:55:01 +02:00
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
assert set(dir_list) == {filename, 'a', 'b', 'c', 'd'}
|
|
|
|
assert set(os.listdir('./d')) == {'e'}
|
|
|
|
return zip_error_inner
|
2015-07-25 00:55:01 +02:00
|
|
|
|
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
@pytest.mark.parametrize('script,filename', [
|
|
|
|
(u'unzip café', u'café.zip'),
|
|
|
|
(u'unzip café.zip', u'café.zip'),
|
|
|
|
(u'unzip foo', u'foo.zip'),
|
|
|
|
(u'unzip foo.zip', u'foo.zip')])
|
|
|
|
def test_match(zip_error, script, filename):
|
|
|
|
zip_error(filename)
|
2017-08-31 17:58:56 +02:00
|
|
|
assert match(Command(script, ''))
|
2015-07-25 00:55:01 +02:00
|
|
|
|
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
@pytest.mark.parametrize('script,filename', [
|
|
|
|
(u'unzip café', u'café.zip'),
|
|
|
|
(u'unzip café.zip', u'café.zip'),
|
|
|
|
(u'unzip foo', u'foo.zip'),
|
|
|
|
(u'unzip foo.zip', u'foo.zip')])
|
|
|
|
def test_side_effect(zip_error, script, filename):
|
|
|
|
zip_error(filename)
|
2017-08-31 17:58:56 +02:00
|
|
|
side_effect(Command(script, ''), None)
|
2015-11-04 23:08:13 -02:00
|
|
|
|
|
|
|
dir_list = os.listdir(u'.')
|
|
|
|
if filename not in set(dir_list):
|
|
|
|
filename = normalize('NFD', filename)
|
|
|
|
|
|
|
|
assert set(dir_list) == {filename, 'd'}
|
2015-07-25 00:55:01 +02:00
|
|
|
|
|
|
|
|
2015-11-04 23:08:13 -02:00
|
|
|
@pytest.mark.parametrize('script,fixed,filename', [
|
|
|
|
(u'unzip café', u"unzip café -d 'café'", u'café.zip'),
|
|
|
|
(u'unzip foo', u'unzip foo -d foo', u'foo.zip'),
|
|
|
|
(u"unzip 'foo bar.zip'", u"unzip 'foo bar.zip' -d 'foo bar'", u'foo.zip'),
|
|
|
|
(u'unzip foo.zip', u'unzip foo.zip -d foo', u'foo.zip')])
|
|
|
|
def test_get_new_command(zip_error, script, fixed, filename):
|
|
|
|
zip_error(filename)
|
2017-08-31 17:58:56 +02:00
|
|
|
assert get_new_command(Command(script, '')) == fixed
|