From 0d7226ce14a31539e70a1d24802fd5ae9b1105dc Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 00:21:02 +0800 Subject: Fixed tests, just need to add new tests for file filtering --- tests/unit/mainwindow/test_prompt.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests') diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 668cd0710..e67ab1702 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -82,7 +82,11 @@ class TestFileCompletion: qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) # foo should get completed from f + prompt.item_focus('next') + + # qtbot.keyPress(prompt._lineedit, Qt.Key_Tab) + # qtbot.keyRelease(prompt._lineedit, Qt.Key_Tab) assert prompt._lineedit.text() == str(testdir / 'foo') # Deleting /[foo] -- cgit v1.2.3-54-g00ecf From 3a0d20e5f96bb8188d2b4b29f544c35d94976923 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 00:34:34 +0800 Subject: Added new test --- tests/unit/mainwindow/test_prompt.py | 43 ++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index e67ab1702..8f32d6338 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -85,8 +85,6 @@ class TestFileCompletion: prompt.item_focus('next') - # qtbot.keyPress(prompt._lineedit, Qt.Key_Tab) - # qtbot.keyRelease(prompt._lineedit, Qt.Key_Tab) assert prompt._lineedit.text() == str(testdir / 'foo') # Deleting /[foo] @@ -97,6 +95,47 @@ class TestFileCompletion: prompt.item_focus('next') prompt.item_focus('next') assert prompt._lineedit.text() == str(testdir / 'bar') + + def test_filtering_path(self, qtbot, tmp_path, get_prompt): + testdir = tmp_path / 'test' + + for directory in ['bar', 'foo', 'bat']: + (testdir / directory).mkdir(parents=True) + + prompt = get_prompt(str(testdir) + os.sep) + + # Make sure all directories are shown + num_rows = prompt._file_model.rowCount(prompt._root_index) + visible = [] + for row in range(num_rows): + index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) + if prompt._file_view.isRowHidden(index.row(), index.parent()): + visible.append(index.data()) + + assert visible.sort() == ['bar', 'foo', 'bat'].sort() + + # Only foo should be completed with f + qtbot.keyPress(prompt._lineedit, Qt.Key_F) + num_rows = prompt._file_model.rowCount(prompt._root_index) + visible = [] + for row in range(num_rows): + index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) + if prompt._file_view.isRowHidden(index.row(), index.parent()): + visible.append(index.data()) + + assert visible.sort() == ['foo'].sort() + + # bat and bar should show up with a typed + qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) + qtbot.keyPress(prompt._lineedit, Qt.Key_A) + num_rows = prompt._file_model.rowCount(prompt._root_index) + visible = [] + for row in range(num_rows): + index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) + if prompt._file_view.isRowHidden(index.row(), index.parent()): + visible.append(index.data()) + + assert visible.sort() == ['bar', 'bat'].sort() @pytest.mark.linux def test_root_path(self, get_prompt): -- cgit v1.2.3-54-g00ecf From 5610a43177e64a882896fbdd0e1befb66a65facc Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 01:03:52 +0800 Subject: Not working with this commit, but have to commit to merge --- qutebrowser/mainwindow/prompt.py | 9 ++++----- tests/unit/mainwindow/test_prompt.py | 10 ++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index 40c6fde3a..d4f4336dc 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -728,7 +728,7 @@ class FilenamePrompt(_BasePrompt): # The model needs to be sorted so we get the correct first/last index self._file_model.directoryLoaded.connect( lambda: self._file_model.sort(0)) - + def _get_valid_dirs(self, path): dirs = [] try: @@ -738,17 +738,16 @@ class FilenamePrompt(_BasePrompt): hidden = self._to_complete not in index.data() if not hidden: dirs.append(index.data()) - # self._file_view.setRowHidden(index.row(), index.parent(), hidden) except FileNotFoundError: log.prompt.debug("Directory doesn't exist, can't \ get valid dirs") - + return dirs def _do_completion(self, idx, which): filename = self._file_model.fileName(idx) valid_dirs = self._get_valid_dirs(self._current_path) - while not filename in valid_dirs and idx.isValid(): + while idx.isValid() and not self._file_view.isRowHidden(idx.row(), idx.parent()): if which == 'prev': idx = self._file_view.indexAbove(idx) else: @@ -798,7 +797,7 @@ class FilenamePrompt(_BasePrompt): # wrap around if we arrived at beginning/end if not idx.isValid(): idx = last_index if which == 'prev' else first_index - + idx = self._do_completion(idx, which) selmodel.setCurrentIndex( diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 8f32d6338..71d94e51d 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -95,20 +95,21 @@ class TestFileCompletion: prompt.item_focus('next') prompt.item_focus('next') assert prompt._lineedit.text() == str(testdir / 'bar') - + def test_filtering_path(self, qtbot, tmp_path, get_prompt): testdir = tmp_path / 'test' for directory in ['bar', 'foo', 'bat']: (testdir / directory).mkdir(parents=True) - + prompt = get_prompt(str(testdir) + os.sep) # Make sure all directories are shown num_rows = prompt._file_model.rowCount(prompt._root_index) visible = [] for row in range(num_rows): - index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) + parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) + index = prompt._file_model.index(row, 0, parent) if prompt._file_view.isRowHidden(index.row(), index.parent()): visible.append(index.data()) @@ -119,7 +120,8 @@ class TestFileCompletion: num_rows = prompt._file_model.rowCount(prompt._root_index) visible = [] for row in range(num_rows): - index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) + parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) + index = prompt._file_model.index(row, 0, parent) if prompt._file_view.isRowHidden(index.row(), index.parent()): visible.append(index.data()) -- cgit v1.2.3-54-g00ecf From 1fe3ba356e4243581fa3ee308287560bff7ccab4 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 01:20:49 +0800 Subject: Better tests, added back _get_valid_dirs hopefully temporarily --- qutebrowser/mainwindow/prompt.py | 5 +- tests/unit/mainwindow/test_prompt.py | 89 ++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 37 deletions(-) (limited to 'tests') diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index 182b58b40..3e6ffba01 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -738,16 +738,17 @@ class FilenamePrompt(_BasePrompt): hidden = self._to_complete not in index.data() if not hidden: dirs.append(index.data()) + # self._file_view.setRowHidden(index.row(), index.parent(), hidden) except FileNotFoundError: log.prompt.debug("Directory doesn't exist, can't \ get valid dirs") - + return dirs def _do_completion(self, idx, which): filename = self._file_model.fileName(idx) valid_dirs = self._get_valid_dirs(self._current_path) - while idx.isValid() and not self._file_view.isRowHidden(idx.row(), idx.parent()): + while not filename in valid_dirs and idx.isValid(): if which == 'prev': idx = self._file_view.indexAbove(idx) else: diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 71d94e51d..558eaa89b 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -96,7 +96,11 @@ class TestFileCompletion: prompt.item_focus('next') assert prompt._lineedit.text() == str(testdir / 'bar') - def test_filtering_path(self, qtbot, tmp_path, get_prompt): + @pytest.mark.parametrize("keys,expected", + [([], ['bar', 'foo', 'bat'].sort()), + (['F'], ['foo'].sort()), + (['Backspace', 'A'], ['bar', 'bat'].sort())]) + def test_filtering_path(self, qtbot, tmp_path, get_prompt, keys, expected): testdir = tmp_path / 'test' for directory in ['bar', 'foo', 'bat']: @@ -104,40 +108,55 @@ class TestFileCompletion: prompt = get_prompt(str(testdir) + os.sep) - # Make sure all directories are shown - num_rows = prompt._file_model.rowCount(prompt._root_index) - visible = [] - for row in range(num_rows): - parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) - index = prompt._file_model.index(row, 0, parent) - if prompt._file_view.isRowHidden(index.row(), index.parent()): - visible.append(index.data()) - - assert visible.sort() == ['bar', 'foo', 'bat'].sort() - - # Only foo should be completed with f - qtbot.keyPress(prompt._lineedit, Qt.Key_F) - num_rows = prompt._file_model.rowCount(prompt._root_index) - visible = [] - for row in range(num_rows): - parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) - index = prompt._file_model.index(row, 0, parent) - if prompt._file_view.isRowHidden(index.row(), index.parent()): - visible.append(index.data()) - - assert visible.sort() == ['foo'].sort() - - # bat and bar should show up with a typed - qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) - qtbot.keyPress(prompt._lineedit, Qt.Key_A) - num_rows = prompt._file_model.rowCount(prompt._root_index) - visible = [] - for row in range(num_rows): - index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) - if prompt._file_view.isRowHidden(index.row(), index.parent()): - visible.append(index.data()) - - assert visible.sort() == ['bar', 'bat'].sort() + for _ in range(len(keys)): + for key in keys: + eval("qtbot.keyPress(prompt._lineedit, Qt.Key_{})".format(key)) + + num_rows = prompt._file_model.rowCount(prompt._root_index) + visible = [] + for row in range(num_rows): + parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) + index = prompt._file_model.index(row, 0, parent) + if prompt._file_view.isRowHidden(index.row(), index.parent()): + visible.append(index.data()) + + assert visible.sort() == expected + + + # # Make sure all directories are shown + # num_rows = prompt._file_model.rowCount(prompt._root_index) + # visible = [] + # for row in range(num_rows): + # parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) + # index = prompt._file_model.index(row, 0, parent) + # if prompt._file_view.isRowHidden(index.row(), index.parent()): + # visible.append(index.data()) + + # assert visible.sort() == ['bar', 'foo', 'bat'].sort() + + # # Only foo should be completed with f + # qtbot.keyPress(prompt._lineedit, Qt.Key_F) + # num_rows = prompt._file_model.rowCount(prompt._root_index) + # visible = [] + # for row in range(num_rows): + # parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) + # index = prompt._file_model.index(row, 0, parent) + # if prompt._file_view.isRowHidden(index.row(), index.parent()): + # visible.append(index.data()) + + # assert visible.sort() == ['foo'].sort() + + # # bat and bar should show up with a typed + # qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) + # qtbot.keyPress(prompt._lineedit, Qt.Key_A) + # num_rows = prompt._file_model.rowCount(prompt._root_index) + # visible = [] + # for row in range(num_rows): + # index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) + # if prompt._file_view.isRowHidden(index.row(), index.parent()): + # visible.append(index.data()) + + # assert visible.sort() == ['bar', 'bat'].sort() @pytest.mark.linux def test_root_path(self, get_prompt): -- cgit v1.2.3-54-g00ecf From a88a83765c5749d18c24ff6daeff4cdbb534f0f6 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 01:26:41 +0800 Subject: Forgot to delete commented out code --- tests/unit/mainwindow/test_prompt.py | 36 ------------------------------------ 1 file changed, 36 deletions(-) (limited to 'tests') diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 558eaa89b..337de36d6 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -122,42 +122,6 @@ class TestFileCompletion: assert visible.sort() == expected - - # # Make sure all directories are shown - # num_rows = prompt._file_model.rowCount(prompt._root_index) - # visible = [] - # for row in range(num_rows): - # parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) - # index = prompt._file_model.index(row, 0, parent) - # if prompt._file_view.isRowHidden(index.row(), index.parent()): - # visible.append(index.data()) - - # assert visible.sort() == ['bar', 'foo', 'bat'].sort() - - # # Only foo should be completed with f - # qtbot.keyPress(prompt._lineedit, Qt.Key_F) - # num_rows = prompt._file_model.rowCount(prompt._root_index) - # visible = [] - # for row in range(num_rows): - # parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) - # index = prompt._file_model.index(row, 0, parent) - # if prompt._file_view.isRowHidden(index.row(), index.parent()): - # visible.append(index.data()) - - # assert visible.sort() == ['foo'].sort() - - # # bat and bar should show up with a typed - # qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) - # qtbot.keyPress(prompt._lineedit, Qt.Key_A) - # num_rows = prompt._file_model.rowCount(prompt._root_index) - # visible = [] - # for row in range(num_rows): - # index = prompt._file_model.index(row, 0, prompt._file_model.index(os.path.basename(prompt._lineedit.text()))) - # if prompt._file_view.isRowHidden(index.row(), index.parent()): - # visible.append(index.data()) - - # assert visible.sort() == ['bar', 'bat'].sort() - @pytest.mark.linux def test_root_path(self, get_prompt): """With / as path, show root contents.""" -- cgit v1.2.3-54-g00ecf From 4fc17293b02ee8e27f31143364545e1b23991257 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 09:58:50 +0800 Subject: All tests should pass now --- qutebrowser/mainwindow/prompt.py | 24 ++++++++++++------------ tests/unit/mainwindow/test_prompt.py | 14 +++++++++----- 2 files changed, 21 insertions(+), 17 deletions(-) (limited to 'tests') diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index db8d79235..2581831f6 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -632,16 +632,16 @@ class FilenamePrompt(_BasePrompt): def _directories_hide_show_model(self, path): """Get rid of non-matching directories.""" - dirs = self._get_dirs(path) - for dir in dirs['invalid']: - dir = os.path.join(path, dir) - index = self._file_model.index(dir) + dirs = self._get_dirs() + for directory in dirs['invalid']: + directory = os.path.join(path, directory) + index = self._file_model.index(directory) self._file_view.setRowHidden(index.row(), index.parent(), True) - for dir in dirs['valid']: - dir = os.path.join(path, dir) - index = self._file_model.index(dir) - self._file_view.setRowHidden(index.row(), index.parent(), False) + for directory in dirs['valid']: + directory = os.path.join(path, directory) + index = self._file_model.index(directory) + self._file_view.setRowHidden(index.row(), index.parent(), False) @pyqtSlot(str) def _set_fileview_root(self, path, *, tabbed=False): @@ -730,8 +730,8 @@ class FilenamePrompt(_BasePrompt): self._file_model.directoryLoaded.connect( lambda: self._file_model.sort(0)) - def _get_dirs(self, path): - dirs = {"valid": [], "invalid": []} + def _get_dirs(self): + dirs = {"valid": [], "invalid": []} # type: dict[list[str]] try: num_rows = self._file_model.rowCount(self._root_index) for row in range(num_rows): @@ -749,8 +749,8 @@ class FilenamePrompt(_BasePrompt): def _do_completion(self, idx, which): filename = self._file_model.fileName(idx) - valid_dirs = self._get_dirs(self._current_path) - while not filename in valid_dirs['valid'] and idx.isValid(): + valid_dirs = self._get_dirs() + while filename not in valid_dirs['valid'] and idx.isValid(): if which == 'prev': idx = self._file_view.indexAbove(idx) else: diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 337de36d6..48bc703a7 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -96,8 +96,8 @@ class TestFileCompletion: prompt.item_focus('next') assert prompt._lineedit.text() == str(testdir / 'bar') - @pytest.mark.parametrize("keys,expected", - [([], ['bar', 'foo', 'bat'].sort()), + @pytest.mark.parametrize("keys,expected", + [([], ['bar', 'foo', 'bat'].sort()), (['F'], ['foo'].sort()), (['Backspace', 'A'], ['bar', 'bat'].sort())]) def test_filtering_path(self, qtbot, tmp_path, get_prompt, keys, expected): @@ -111,15 +111,19 @@ class TestFileCompletion: for _ in range(len(keys)): for key in keys: eval("qtbot.keyPress(prompt._lineedit, Qt.Key_{})".format(key)) - + num_rows = prompt._file_model.rowCount(prompt._root_index) visible = [] for row in range(num_rows): - parent = prompt._file_model.index(os.path.basename(prompt._lineedit.text())) + parent = prompt._file_model.index( + os.path.basename( + prompt._lineedit.text() + ) + ) index = prompt._file_model.index(row, 0, parent) if prompt._file_view.isRowHidden(index.row(), index.parent()): visible.append(index.data()) - + assert visible.sort() == expected @pytest.mark.linux -- cgit v1.2.3-54-g00ecf From df28469d441266b45708cf065d6c0432f90c1819 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 10:04:34 +0800 Subject: Fixed indentation, typing --- qutebrowser/mainwindow/prompt.py | 4 ++-- tests/unit/mainwindow/test_prompt.py | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index 2581831f6..962698dc0 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -24,7 +24,7 @@ import html import collections import functools import dataclasses -from typing import Deque, MutableSequence, Optional, cast +from typing import Deque, Dict, List, MutableSequence, Optional, cast from PyQt5.QtCore import (pyqtSlot, pyqtSignal, Qt, QTimer, QDir, QModelIndex, QItemSelectionModel, QObject, QEventLoop) @@ -731,7 +731,7 @@ class FilenamePrompt(_BasePrompt): lambda: self._file_model.sort(0)) def _get_dirs(self): - dirs = {"valid": [], "invalid": []} # type: dict[list[str]] + dirs = {"valid": [], "invalid": []} # type: Dict[str, List[str]] try: num_rows = self._file_model.rowCount(self._root_index) for row in range(num_rows): diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 48bc703a7..09c8ef6f6 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -116,10 +116,7 @@ class TestFileCompletion: visible = [] for row in range(num_rows): parent = prompt._file_model.index( - os.path.basename( - prompt._lineedit.text() - ) - ) + os.path.basename(prompt._lineedit.text())) index = prompt._file_model.index(row, 0, parent) if prompt._file_view.isRowHidden(index.row(), index.parent()): visible.append(index.data()) -- cgit v1.2.3-54-g00ecf From 412e428e9d2da8f85c6a3bb42fe5ac6d2fe5cb78 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Fri, 4 Jun 2021 22:35:37 +0800 Subject: Took suggestions, added '..' back to tabbing, fixed tests --- qutebrowser/mainwindow/prompt.py | 60 ++++++++++++------------------------ tests/unit/mainwindow/test_prompt.py | 41 ++++++++++++------------ 2 files changed, 40 insertions(+), 61 deletions(-) (limited to 'tests') diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index ab84f902d..65f2e95c0 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -632,16 +632,17 @@ class FilenamePrompt(_BasePrompt): def _directories_hide_show_model(self, path): """Get rid of non-matching directories.""" - dirs = self._get_dirs() - for directory in dirs['invalid']: - directory = os.path.join(path, directory) - index = self._file_model.index(directory) - self._file_view.setRowHidden(index.row(), index.parent(), True) - - for directory in dirs['valid']: - directory = os.path.join(path, directory) - index = self._file_model.index(directory) - self._file_view.setRowHidden(index.row(), index.parent(), False) + try: + num_rows = self._file_model.rowCount(self._root_index) + for row in range(num_rows): + index = self._file_model.index(row, 0, self._root_index) + hidden = self._to_complete not in index.data() + if index.data() == '..': + hidden = False + self._file_view.setRowHidden(index.row(), index.parent(), hidden) + except FileNotFoundError: + log.prompt.debug("Directory doesn't exist, can't \ + get valid dirs") @pyqtSlot(str) def _set_fileview_root(self, path, *, tabbed=False): @@ -728,36 +729,6 @@ class FilenamePrompt(_BasePrompt): self._file_model.directoryLoaded.connect( lambda: self._file_model.sort(0)) - def _get_dirs(self): - dirs: Dict[str, List[str]] = {"valid": [], "invalid": []} - try: - num_rows = self._file_model.rowCount(self._root_index) - for row in range(num_rows): - index = self._file_model.index(row, 0, self._root_index) - hidden = self._to_complete not in index.data() - if not hidden: - dirs['valid'].append(index.data()) - else: - dirs['invalid'].append(index.data()) - except FileNotFoundError: - log.prompt.debug("Directory doesn't exist, can't \ - get valid dirs") - - return dirs - - def _do_completion(self, idx, which): - filename = self._file_model.fileName(idx) - valid_dirs = self._get_dirs() - while filename not in valid_dirs['valid'] and idx.isValid(): - if which == 'prev': - idx = self._file_view.indexAbove(idx) - else: - assert which == 'next', which - idx = self._file_view.indexBelow(idx) - filename = self._file_model.fileName(idx) - - return idx - def accept(self, value=None, save=False): self._check_save_support(save) text = value if value is not None else self._lineedit.text() @@ -807,6 +778,15 @@ class FilenamePrompt(_BasePrompt): QItemSelectionModel.Rows) self._insert_path(idx, clicked=False) + def _do_completion(self, idx, which): + while idx.isValid() and self._file_view.isIndexHidden(idx): + if which == 'prev': + idx = self._file_view.indexAbove(idx) + else: + assert which == 'next', which + idx = self._file_view.indexBelow(idx) + return idx + def _allowed_commands(self): return [('prompt-accept', 'Accept'), ('mode-leave', 'Abort')] diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 09c8ef6f6..d5ef5a363 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -80,11 +80,12 @@ class TestFileCompletion: with qtbot.wait_signal(prompt._file_model.directoryLoaded): for _ in range(3): qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) + prompt._set_fileview_root(prompt._lineedit.text()) - # foo should get completed from f - + # ...and foo should get completed from f + prompt.item_focus('next') + assert prompt._lineedit.text() == str(tmp_path) prompt.item_focus('next') - assert prompt._lineedit.text() == str(testdir / 'foo') # Deleting /[foo] @@ -97,9 +98,9 @@ class TestFileCompletion: assert prompt._lineedit.text() == str(testdir / 'bar') @pytest.mark.parametrize("keys,expected", - [([], ['bar', 'foo', 'bat'].sort()), - (['F'], ['foo'].sort()), - (['Backspace', 'A'], ['bar', 'bat'].sort())]) + [([], ['bar', 'bat', 'foo']), + ([Qt.Key_F], ['foo']), + ([Qt.Key_A], ['bar', 'bat'])]) def test_filtering_path(self, qtbot, tmp_path, get_prompt, keys, expected): testdir = tmp_path / 'test' @@ -107,21 +108,19 @@ class TestFileCompletion: (testdir / directory).mkdir(parents=True) prompt = get_prompt(str(testdir) + os.sep) - - for _ in range(len(keys)): - for key in keys: - eval("qtbot.keyPress(prompt._lineedit, Qt.Key_{})".format(key)) - - num_rows = prompt._file_model.rowCount(prompt._root_index) - visible = [] - for row in range(num_rows): - parent = prompt._file_model.index( - os.path.basename(prompt._lineedit.text())) - index = prompt._file_model.index(row, 0, parent) - if prompt._file_view.isRowHidden(index.row(), index.parent()): - visible.append(index.data()) - - assert visible.sort() == expected + for key in keys: + qtbot.keyPress(prompt._lineedit, key) + prompt._set_fileview_root(prompt._lineedit.text()) + + num_rows = prompt._file_model.rowCount(prompt._file_view.rootIndex()) + visible = [] + for row in range(num_rows): + parent = prompt._file_model.index( + os.path.dirname(prompt._lineedit.text())) + index = prompt._file_model.index(row, 0, parent) + if not prompt._file_view.isRowHidden(index.row(), index.parent()) and index.data() != "..": + visible.append(index.data()) + assert visible == expected @pytest.mark.linux def test_root_path(self, get_prompt): -- cgit v1.2.3-54-g00ecf From 67e16a5a0ce82c9aa10ce748d1c698928770c465 Mon Sep 17 00:00:00 2001 From: jso8910 <60021163+jso8910@users.noreply.github.com> Date: Sat, 5 Jun 2021 08:53:22 +0800 Subject: Update tests/unit/mainwindow/test_prompt.py Co-authored-by: Florian Bruhin --- tests/unit/mainwindow/test_prompt.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index d5ef5a363..1110e80d7 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -97,10 +97,11 @@ class TestFileCompletion: prompt.item_focus('next') assert prompt._lineedit.text() == str(testdir / 'bar') - @pytest.mark.parametrize("keys,expected", - [([], ['bar', 'bat', 'foo']), - ([Qt.Key_F], ['foo']), - ([Qt.Key_A], ['bar', 'bat'])]) + @pytest.mark.parametrize("keys, expected", [ + ([], ['bar', 'bat', 'foo']), + ([Qt.Key_F], ['foo']), + ([Qt.Key_A], ['bar', 'bat']), + ]) def test_filtering_path(self, qtbot, tmp_path, get_prompt, keys, expected): testdir = tmp_path / 'test' -- cgit v1.2.3-54-g00ecf From 6c4216d607a54843dec988904e758b166c2a8d9e Mon Sep 17 00:00:00 2001 From: jso8910 <60021163+jso8910@users.noreply.github.com> Date: Sat, 5 Jun 2021 08:53:54 +0800 Subject: Update tests/unit/mainwindow/test_prompt.py Co-authored-by: Florian Bruhin --- tests/unit/mainwindow/test_prompt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index 1110e80d7..16a636714 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -82,7 +82,7 @@ class TestFileCompletion: qtbot.keyPress(prompt._lineedit, Qt.Key_Backspace) prompt._set_fileview_root(prompt._lineedit.text()) - # ...and foo should get completed from f + # '..' and 'foo' should get completed from 'f' prompt.item_focus('next') assert prompt._lineedit.text() == str(tmp_path) prompt.item_focus('next') -- cgit v1.2.3-54-g00ecf From 68cdae184a4d5f0c0542c8092c156164511f58a3 Mon Sep 17 00:00:00 2001 From: Jason Rosenzweig Date: Sat, 5 Jun 2021 13:34:45 +0800 Subject: Less redundant code --- qutebrowser/mainwindow/prompt.py | 22 +++++++++------------- tests/unit/mainwindow/test_prompt.py | 8 ++++---- 2 files changed, 13 insertions(+), 17 deletions(-) (limited to 'tests') diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index 65f2e95c0..b096f2da7 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -629,20 +629,17 @@ class FilenamePrompt(_BasePrompt): self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) self._to_complete = '' + self._root_index = QModelIndex() def _directories_hide_show_model(self, path): """Get rid of non-matching directories.""" - try: - num_rows = self._file_model.rowCount(self._root_index) - for row in range(num_rows): - index = self._file_model.index(row, 0, self._root_index) - hidden = self._to_complete not in index.data() - if index.data() == '..': - hidden = False - self._file_view.setRowHidden(index.row(), index.parent(), hidden) - except FileNotFoundError: - log.prompt.debug("Directory doesn't exist, can't \ - get valid dirs") + num_rows = self._file_model.rowCount(self._root_index) + for row in range(num_rows): + index = self._file_model.index(row, 0, self._root_index) + hidden = self._to_complete not in index.data() + if index.data() == '..': + hidden = False + self._file_view.setRowHidden(index.row(), index.parent(), hidden) @pyqtSlot(str) def _set_fileview_root(self, path, *, tabbed=False): @@ -679,7 +676,6 @@ class FilenamePrompt(_BasePrompt): self._file_view.setRootIndex(self._root_index) self._directories_hide_show_model(path) - self._file_view.setModel(self._file_model) @pyqtSlot(QModelIndex) def _insert_path(self, index, *, clicked=True): @@ -779,7 +775,7 @@ class FilenamePrompt(_BasePrompt): self._insert_path(idx, clicked=False) def _do_completion(self, idx, which): - while idx.isValid() and self._file_view.isIndexHidden(idx): + while idx.isValid() and self._file_view.isIndexHidden(idx): if which == 'prev': idx = self._file_view.indexAbove(idx) else: diff --git a/tests/unit/mainwindow/test_prompt.py b/tests/unit/mainwindow/test_prompt.py index d5ef5a363..cb394e8c7 100644 --- a/tests/unit/mainwindow/test_prompt.py +++ b/tests/unit/mainwindow/test_prompt.py @@ -98,9 +98,9 @@ class TestFileCompletion: assert prompt._lineedit.text() == str(testdir / 'bar') @pytest.mark.parametrize("keys,expected", - [([], ['bar', 'bat', 'foo']), - ([Qt.Key_F], ['foo']), - ([Qt.Key_A], ['bar', 'bat'])]) + [([], ['..', 'bar', 'bat', 'foo']), + ([Qt.Key_F], ['..', 'foo']), + ([Qt.Key_A], ['..', 'bar', 'bat'])]) def test_filtering_path(self, qtbot, tmp_path, get_prompt, keys, expected): testdir = tmp_path / 'test' @@ -118,7 +118,7 @@ class TestFileCompletion: parent = prompt._file_model.index( os.path.dirname(prompt._lineedit.text())) index = prompt._file_model.index(row, 0, parent) - if not prompt._file_view.isRowHidden(index.row(), index.parent()) and index.data() != "..": + if not prompt._file_view.isRowHidden(index.row(), index.parent()): visible.append(index.data()) assert visible == expected -- cgit v1.2.3-54-g00ecf