Skip to content

Commit

Permalink
Merge from 5.x: PR #20971
Browse files Browse the repository at this point in the history
Fixes #20964
  • Loading branch information
ccordoba12 committed May 29, 2023
2 parents ee90ce1 + 1143a66 commit eb98dd9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
33 changes: 18 additions & 15 deletions spyder/plugins/findinfiles/widgets/results_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ def __init__(self, parent, path, filename, sorting, text_color):

# Get relative dirname according to the path we're searching in.
dirname = osp.dirname(filename)
rel_dirname = dirname.split(path)[1]
if rel_dirname.startswith(osp.sep):
rel_dirname = rel_dirname[1:]

# Catch errors when it's not possible to get the relative directory
# name. This happens when the user is searching in a single file.
# Fixes spyder-ide/spyder#17443 and spyder-ide/spyder#20964
try:
rel_dirname = dirname.split(path)[1]
if rel_dirname.startswith(osp.sep):
rel_dirname = rel_dirname[1:]
except IndexError:
rel_dirname = dirname

self.rel_dirname = rel_dirname

title = (
Expand Down Expand Up @@ -257,18 +265,13 @@ def clear_title(self, search_text):
def append_file_result(self, filename):
"""Real-time update of file items."""
if len(self.data) < self.max_results:
# Catch any error while creating file items.
# Fixes spyder-ide/spyder#17443
try:
item = FileMatchItem(
self,
self.path,
filename,
self.sorting,
self.text_color
)
except Exception:
return
item = FileMatchItem(
self,
self.path,
filename,
self.sorting,
self.text_color
)

self.files[filename] = item

Expand Down
5 changes: 4 additions & 1 deletion spyder/plugins/findinfiles/widgets/search_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ def find_string_in_file(self, fname):
found = line.find(text, found + 1)
if found > -1:
break

except IOError as xxx_todo_changeme:
(_errno, _strerror) = xxx_todo_changeme.args
self.error_flag = _("permission denied errors were encountered")

# Process any pending results
if self.is_file and self.partial_results:
self.process_results()

self.completed = True

def process_results(self):
Expand Down
20 changes: 20 additions & 0 deletions spyder/plugins/findinfiles/widgets/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,5 +649,25 @@ def test_max_results(findinfiles, qtbot):
findinfiles.set_max_results(1000)


@flaky(max_runs=5)
def test_find_in_single_file(findinfiles, qtbot):
"""
Test that find in files works for a single file.
This a regression test for issues spyder-ide/spyder#17443 and
spyder-ide/spyder#20964.
"""
findinfiles.set_search_text("spam")
findinfiles.set_file_path(osp.join(LOCATION, "data", 'spam.txt'))
findinfiles.path_selection_combo.setCurrentIndex(FILE_PATH)

with qtbot.waitSignal(findinfiles.sig_finished):
findinfiles.find()

matches = process_search_results(findinfiles.result_browser.data)
assert list(matches.keys()) == ['spam.txt']
assert expected_results()['spam.txt'] == matches['spam.txt']


if __name__ == "__main__":
pytest.main(['-x', osp.basename(__file__), '-v', '-rw'])

0 comments on commit eb98dd9

Please sign in to comment.