mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-31 07:04:12 +00:00 
			
		
		
		
	#1 Add history of last commands, allow fuck more than once
This commit is contained in:
		
							
								
								
									
										44
									
								
								tests/test_history.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								tests/test_history.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| import pytest | ||||
| from mock import patch, Mock | ||||
| from thefuck.history import History | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def process(monkeypatch): | ||||
|     Process = Mock() | ||||
|     Process.return_value.parent.return_value.pid = 1 | ||||
|     monkeypatch.setattr('thefuck.history.Process', Process) | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def db(monkeypatch): | ||||
|     class DBMock(dict): | ||||
|         def __init__(self): | ||||
|             super(DBMock, self).__init__() | ||||
|             self.sync = Mock() | ||||
|  | ||||
|         def __call__(self, *args, **kwargs): | ||||
|             return self | ||||
|  | ||||
|     db = DBMock() | ||||
|     monkeypatch.setattr('thefuck.history.shelve.open', db) | ||||
|     return db | ||||
|  | ||||
|  | ||||
| @pytest.mark.usefixtures('process') | ||||
| class TestHistory(object): | ||||
|     def test_set(self, db): | ||||
|         history = History() | ||||
|         history.update(last_script='ls', | ||||
|                        last_fixed_script=None) | ||||
|         assert db == {'1-last_script': 'ls', | ||||
|                       '1-last_fixed_script': None} | ||||
|  | ||||
|     def test_get(self, db): | ||||
|         history = History() | ||||
|         db['1-last_script'] = 'cd ..' | ||||
|         assert history.last_script == 'cd ..' | ||||
|  | ||||
|     def test_get_without_value(self, db): | ||||
|         history = History() | ||||
|         assert history.last_script is None | ||||
| @@ -46,15 +46,30 @@ def test_get_command(): | ||||
|                   return_value=True): | ||||
|         Popen.return_value.stdout.read.return_value = b'stdout' | ||||
|         Popen.return_value.stderr.read.return_value = b'stderr' | ||||
|         assert main.get_command(Mock(), ['thefuck', 'apt-get', | ||||
|                                          'search', 'vim']) \ | ||||
|         assert main.get_command(Mock(), Mock(), | ||||
|             ['thefuck', 'apt-get', 'search', 'vim']) \ | ||||
|                == Command('apt-get search vim', 'stdout', 'stderr') | ||||
|         Popen.assert_called_once_with('apt-get search vim', | ||||
|                                       shell=True, | ||||
|                                       stdout=PIPE, | ||||
|                                       stderr=PIPE, | ||||
|                                       env={'LANG': 'C'}) | ||||
|         assert main.get_command(Mock(), ['']) is None | ||||
|         assert main.get_command(Mock(), Mock(), ['']) is None | ||||
|         # When command is `fuck`: | ||||
|         assert main.get_command( | ||||
|             Mock(), | ||||
|             Mock(last_script='ls', last_fixed_script='ls -la'), | ||||
|             ['thefuck', 'fuck']).script == 'ls -la' | ||||
|         # When command equals to last command: | ||||
|         assert main.get_command( | ||||
|             Mock(), | ||||
|             Mock(last_script='ls', last_fixed_script='ls -la'), | ||||
|             ['thefuck', 'ls']).script == 'ls -la' | ||||
|         # When last command is `fuck` and no last fixed script: | ||||
|         assert main.get_command( | ||||
|             Mock(), | ||||
|             Mock(last_script='ls', last_fixed_script=''), | ||||
|             ['thefuck', 'ls']).script == 'ls' | ||||
|  | ||||
|  | ||||
| def test_get_matched_rule(capsys): | ||||
| @@ -72,20 +87,24 @@ def test_get_matched_rule(capsys): | ||||
| def test_run_rule(capsys): | ||||
|     with patch('thefuck.main.confirm', return_value=True): | ||||
|         main.run_rule(Rule(get_new_command=lambda *_: 'new-command'), | ||||
|                       None, None) | ||||
|                       Command(), Mock(), None) | ||||
|         assert capsys.readouterr() == ('new-command\n', '') | ||||
|         # With side effect: | ||||
|         side_effect = Mock() | ||||
|         settings = Mock() | ||||
|         command = Mock() | ||||
|         command = Mock(script='ls') | ||||
|         history = Mock() | ||||
|         main.run_rule(Rule(get_new_command=lambda *_: 'new-command', | ||||
|                            side_effect=side_effect), | ||||
|                       command, settings) | ||||
|                       command, history, settings) | ||||
|         assert capsys.readouterr() == ('new-command\n', '') | ||||
|         side_effect.assert_called_once_with(command, settings) | ||||
|         # Ensure that history updated: | ||||
|         history.update.assert_called_once_with(last_script='ls', | ||||
|                                                last_fixed_script='new-command') | ||||
|     with patch('thefuck.main.confirm', return_value=False): | ||||
|         main.run_rule(Rule(get_new_command=lambda *_: 'new-command'), | ||||
|                       None, None) | ||||
|                       Command(), Mock(), None) | ||||
|         assert capsys.readouterr() == ('', '') | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user