mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-31 07:04:12 +00:00 
			
		
		
		
	Merge branch 'unzip-clean' of https://github.com/mcarton/thefuck into mcarton-unzip-clean
This commit is contained in:
		
							
								
								
									
										62
									
								
								tests/rules/test_dirty_untar.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								tests/rules/test_dirty_untar.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| import os | ||||
| import pytest | ||||
| import tarfile | ||||
| from thefuck.rules.dirty_untar import match, get_new_command, side_effect | ||||
| from tests.utils import Command | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def tar_error(tmpdir): | ||||
|     def fixture(filename): | ||||
|         path = os.path.join(str(tmpdir), filename) | ||||
|  | ||||
|         def reset(path): | ||||
|             with tarfile.TarFile(path, 'w') as archive: | ||||
|                 for file in ('a', 'b', 'c'): | ||||
|                     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) | ||||
|  | ||||
|         assert(set(os.listdir('.')) == {filename, 'a', 'b', 'c'}) | ||||
|  | ||||
|     return fixture | ||||
|  | ||||
| parametrize_filename = pytest.mark.parametrize('filename', [ | ||||
|     'foo.tar', | ||||
|     'foo.tar.gz', | ||||
|     'foo.tgz']) | ||||
|  | ||||
| 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')]) | ||||
|  | ||||
| @parametrize_filename | ||||
| @parametrize_script | ||||
| def test_match(tar_error, filename, script, fixed): | ||||
|     tar_error(filename) | ||||
|     assert match(Command(script=script.format(filename)), None) | ||||
|  | ||||
|  | ||||
| @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(os.listdir('.') == [filename]) | ||||
|  | ||||
|  | ||||
| @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)), None) == fixed.format(filename) | ||||
							
								
								
									
										45
									
								
								tests/rules/test_dirty_unzip.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								tests/rules/test_dirty_unzip.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| import os | ||||
| import pytest | ||||
| import zipfile | ||||
| from thefuck.rules.dirty_unzip import match, get_new_command, side_effect | ||||
| from tests.utils import Command | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def zip_error(tmpdir): | ||||
|     path = os.path.join(str(tmpdir), 'foo.zip') | ||||
|  | ||||
|     def reset(path): | ||||
|         with zipfile.ZipFile(path, 'w') as archive: | ||||
|             archive.writestr('a', '1') | ||||
|             archive.writestr('b', '2') | ||||
|             archive.writestr('c', '3') | ||||
|  | ||||
|             archive.extractall() | ||||
|  | ||||
|     os.chdir(str(tmpdir)) | ||||
|     reset(path) | ||||
|  | ||||
|     assert(set(os.listdir('.')) == {'foo.zip', 'a', 'b', 'c'}) | ||||
|  | ||||
|  | ||||
| @pytest.mark.parametrize('script', [ | ||||
|     'unzip foo', | ||||
|     'unzip foo.zip']) | ||||
| def test_match(zip_error, script): | ||||
|     assert match(Command(script=script), None) | ||||
|  | ||||
|  | ||||
| @pytest.mark.parametrize('script', [ | ||||
|     'unzip foo', | ||||
|     'unzip foo.zip']) | ||||
| def test_side_effect(zip_error, script): | ||||
|     side_effect(Command(script=script), None) | ||||
|     assert(os.listdir('.') == ['foo.zip']) | ||||
|  | ||||
|  | ||||
| @pytest.mark.parametrize('script,fixed', [ | ||||
|     ('unzip foo', 'unzip foo -d foo'), | ||||
|     ('unzip foo.zip', 'unzip foo.zip -d foo')]) | ||||
| def test_get_new_command(zip_error, script, fixed): | ||||
|     assert get_new_command(Command(script=script), None) == fixed | ||||
| @@ -14,7 +14,8 @@ def test_load_rule(mocker): | ||||
|         return_value=Mock(match=match, | ||||
|                           get_new_command=get_new_command, | ||||
|                           enabled_by_default=True, | ||||
|                           priority=900)) | ||||
|                           priority=900, | ||||
|                           requires_output=True)) | ||||
|     assert main.load_rule(Path('/rules/bash.py')) \ | ||||
|            == Rule('bash', match, get_new_command, priority=900) | ||||
|     load_source.assert_called_once_with('bash', '/rules/bash.py') | ||||
| @@ -152,7 +153,7 @@ class TestConfirm(object): | ||||
|  | ||||
|     def test_with_side_effect_and_without_confirmation(self, capsys): | ||||
|         assert main.confirm('command', Mock(), Mock(require_confirmation=False)) | ||||
|         assert capsys.readouterr() == ('', 'command*\n') | ||||
|         assert capsys.readouterr() == ('', 'command (+side effect)\n') | ||||
|  | ||||
|     # `stdin` fixture should be applied after `capsys` | ||||
|     def test_when_confirmation_required_and_confirmed(self, capsys, stdin): | ||||
| @@ -164,7 +165,7 @@ class TestConfirm(object): | ||||
|     def test_when_confirmation_required_and_confirmed_with_side_effect(self, capsys, stdin): | ||||
|         assert main.confirm('command', Mock(), Mock(require_confirmation=True, | ||||
|                                                     no_colors=True)) | ||||
|         assert capsys.readouterr() == ('', 'command* [enter/ctrl+c]') | ||||
|         assert capsys.readouterr() == ('', 'command (+side effect) [enter/ctrl+c]') | ||||
|  | ||||
|     def test_when_confirmation_required_and_aborted(self, capsys, stdin): | ||||
|         stdin.side_effect = KeyboardInterrupt | ||||
|   | ||||
| @@ -10,7 +10,8 @@ def Rule(name='', match=lambda *_: True, | ||||
|          get_new_command=lambda *_: '', | ||||
|          enabled_by_default=True, | ||||
|          side_effect=None, | ||||
|          priority=DEFAULT_PRIORITY): | ||||
|          priority=DEFAULT_PRIORITY, | ||||
|          requires_output=True): | ||||
|     return types.Rule(name, match, get_new_command, | ||||
|                       enabled_by_default, side_effect, | ||||
|                       priority) | ||||
|                       priority, requires_output) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user