Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect usage of lstrip bug in fileutils tests #377

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion boltons/strutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
'iter_splitlines', 'indent', 'escape_shell_args',
'args2cmd', 'args2sh', 'parse_int_list', 'format_int_list',
'complement_int_list', 'int_ranges_from_int_list', 'MultiReplace',
'multi_replace', 'unwrap_text']
'multi_replace', 'unwrap_text', 'removeprefix']


_punct_ws_str = string.punctuation + string.whitespace
Expand Down Expand Up @@ -1273,3 +1273,17 @@ def unwrap_text(text, ending='\n\n'):
if ending is None:
return all_grafs
return ending.join(all_grafs)

def removeprefix(text: str, prefix: str) -> str:
r"""
Remove `prefix` from start of `text` if present.

Backport of `str.removeprefix` for Python versions less than 3.9.

Args:
text: A string to remove the prefix from.
prefix: The string to remove from the beginning of `text`.
"""
if text.startswith(prefix):
return text[len(prefix):]
return text
3 changes: 2 additions & 1 deletion tests/test_fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from boltons import fileutils
from boltons.fileutils import FilePerms, iter_find_files
from boltons.strutils import removeprefix


BOLTONS_PATH = os.path.dirname(os.path.abspath(fileutils.__file__))
Expand All @@ -31,7 +32,7 @@ def test_fileperms():

def test_iter_find_files():
def _to_baseless_list(paths):
return [p.lstrip(BOLTONS_PATH) for p in paths]
return [removeprefix(p, BOLTONS_PATH).lstrip(os.path.sep) for p in paths]

assert 'fileutils.py' in _to_baseless_list(iter_find_files(BOLTONS_PATH, patterns=['*.py']))

Expand Down
Loading