From 43fead02d3a24fef71534116c5550def0f56830c Mon Sep 17 00:00:00 2001
From: mcarton <cartonmartin+git@gmail.com>
Date: Wed, 29 Jul 2015 21:35:49 +0200
Subject: [PATCH] Test if the file exists in the `fix_file` rule

This avoid false positives in `match`.
---
 tests/rules/test_fix_file.py | 14 ++++++++++++--
 thefuck/rules/fix_file.py    |  7 ++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/tests/rules/test_fix_file.py b/tests/rules/test_fix_file.py
index 2eb42cab..83ac2738 100644
--- a/tests/rules/test_fix_file.py
+++ b/tests/rules/test_fix_file.py
@@ -158,19 +158,29 @@ ReferenceError: conole is not defined
 
 
 @pytest.mark.parametrize('test', tests)
-def test_match(monkeypatch, test):
+def test_match(mocker, monkeypatch, test):
+    mocker.patch('os.path.isfile', return_value=True)
     monkeypatch.setenv('EDITOR', 'dummy_editor')
     assert match(Command(stderr=test[4]), None)
 
 
 @pytest.mark.parametrize('test', tests)
-def test_not_match(monkeypatch, test):
+def test_no_editor(mocker, monkeypatch, test):
+    mocker.patch('os.path.isfile', return_value=True)
     if 'EDITOR' in os.environ:
         monkeypatch.delenv('EDITOR')
 
     assert not match(Command(stderr=test[4]), None)
 
 
+@pytest.mark.parametrize('test', tests)
+def test_not_file(mocker, monkeypatch, test):
+    mocker.patch('os.path.isfile', return_value=False)
+    monkeypatch.setenv('EDITOR', 'dummy_editor')
+
+    assert not match(Command(stderr=test[4]), None)
+
+
 @pytest.mark.parametrize('test', tests)
 def test_get_new_command(monkeypatch, test):
     monkeypatch.setenv('EDITOR', 'dummy_editor')
diff --git a/thefuck/rules/fix_file.py b/thefuck/rules/fix_file.py
index 5f4aafb9..28b17ba1 100644
--- a/thefuck/rules/fix_file.py
+++ b/thefuck/rules/fix_file.py
@@ -50,7 +50,12 @@ def _search(stderr):
 
 
 def match(command, settings):
-    return 'EDITOR' in os.environ and _search(command.stderr)
+    if 'EDITOR' not in os.environ:
+        return False
+
+    m = _search(command.stderr)
+
+    return m and os.path.isfile(m.group('file'))
 
 
 def get_new_command(command, settings):