Skip to content

Commit

Permalink
Fix bug #1634 (drop finishing spaces and EOL) (#1638)
Browse files Browse the repository at this point in the history
* Fix bug #1634 (drop finishing spaces and EOL)

* Update tests/test_utils_hf_folder.py

* Update tests/test_utils_hf_folder.py

* make style

---------

Co-authored-by: Lucain <[email protected]>
  • Loading branch information
GBR-613 and Wauplin authored Sep 5, 2023
1 parent 94c427a commit 282bda7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/huggingface_hub/utils/_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ def get_token(cls) -> Optional[str]:
# 1. Is it set by environment variable ?
token: Optional[str] = os.environ.get("HUGGING_FACE_HUB_TOKEN")
if token is not None:
token = token.replace("\r", "").replace("\n", "").strip()
return token

# 2. Is it set in token path ?
try:
return cls.path_token.read_text()
token = cls.path_token.read_text()
token = token.replace("\r", "").replace("\n", "").strip()
return token
except FileNotFoundError:
return None

Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ def test_token_in_old_path(self):
# Un-patch
new_patcher.stop()
old_patcher.stop()

def test_token_strip(self):
"""
Test the workflow when the token is mistakenly finishing with new-line or space character.
"""
token = _generate_token()
HfFolder.save_token(" " + token + "\n")
self.assertEqual(HfFolder.get_token(), token)
HfFolder.delete_token()

0 comments on commit 282bda7

Please sign in to comment.