summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2021-01-20 17:57:54 +0100
committerFlorian Bruhin <me@the-compiler.org>2021-01-20 17:57:54 +0100
commit673c0be1798af4a35945ce77c07d4ae1b44bd10c (patch)
treea30757bc156ff8a16350222007594a71578a16e8
parent65c948d1e864bd01cf8f9ceb4c8eefad304ec0b0 (diff)
downloadqutebrowser-673c0be1798af4a35945ce77c07d4ae1b44bd10c.tar.gz
qutebrowser-673c0be1798af4a35945ce77c07d4ae1b44bd10c.zip
completion: Avoid pathlib in filepathcategory
On GitHub Actions, it looks like there's a situation where os.path.expanduser and pathlib.Path.expanduser disagree. When in test_filesystem_completion_hypothesis the completion gets '~' as input, it fails with: tests/unit/completion/test_models.py:469: in test_filesystem_completion_hypothesis model.set_pattern(text) qutebrowser/completion/models/filepathcategory.py:88: in set_pattern self._paths = sorted(self._contract_user(val, path) for path in paths) qutebrowser/completion/models/filepathcategory.py:88: in <genexpr> self._paths = sorted(self._contract_user(val, path) for path in paths) qutebrowser/completion/models/filepathcategory.py:52: in _contract_user return str(head / pathlib.Path(path).relative_to(head.expanduser())) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = PosixPath('/home/runneradmin'), other = (PosixPath('/home/runner'),) parts = ['/', 'home', 'runneradmin'], drv = '', root = '/' def relative_to(self, *other): [...] E ValueError: '/home/runneradmin' does not start with '/home/runner' Let's use os.path everywhere, so we can be sure the two code paths agree with each other.
-rw-r--r--qutebrowser/completion/models/filepathcategory.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/qutebrowser/completion/models/filepathcategory.py b/qutebrowser/completion/models/filepathcategory.py
index 30f79e024..7a420dea4 100644
--- a/qutebrowser/completion/models/filepathcategory.py
+++ b/qutebrowser/completion/models/filepathcategory.py
@@ -17,11 +17,18 @@
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
-"""Completion category for filesystem paths."""
+"""Completion category for filesystem paths.
+
+NOTE: This module deliberatly uses os.path rather than pathlib, because of how
+it interacts with the completion, which operates on strings. For example, we
+need to be able to tell the difference between "~/input" and "~/input/". Also,
+if we get "~/input", we want to glob "~/input*" rather than "~/input/*" which
+is harder to achieve via pathlib.
+"""
import glob
import os
-import pathlib
+import os.path
from typing import List, Optional, Iterable
from PyQt5.QtCore import QAbstractListModel, QModelIndex, QObject, Qt, QUrl
@@ -48,8 +55,8 @@ class FilePathCategory(QAbstractListModel):
"""
if not val.startswith('~'):
return path
- head = pathlib.Path(pathlib.Path(val).parts[0])
- return str(head / pathlib.Path(path).relative_to(head.expanduser()))
+ head = val.split(os.sep)[0]
+ return path.replace(os.path.expanduser(head), head, 1)
def _glob(self, val: str) -> Iterable[str]:
"""Find paths based on the given pattern."""