Skip to content

Commit e8b85ca

Browse files
Copilotmatteius
andauthored
fix: use context managers for file reads in test_credential_safety.py
Agent-Logs-Url: https://github.com/pypa/pipenv/sessions/1f02aadb-471e-451b-8a7a-07a81c405f67 Co-authored-by: matteius <[email protected]>
1 parent 2ec7ca7 commit e8b85ca

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

tests/unit/test_credential_safety.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def test_write_credentials_netrc_emits_machine_block(tmp_path):
8282
assert netrc_path is not None
8383
assert os.path.isfile(netrc_path)
8484

85-
contents = open(netrc_path).read()
85+
with open(netrc_path, encoding="utf-8") as f:
86+
contents = f.read()
8687
assert "machine private.example.com" in contents
8788
assert "login __token__" in contents
8889
assert "password abc123" in contents
@@ -107,7 +108,8 @@ def test_write_credentials_netrc_dedupes_hosts(tmp_path):
107108
{"url": "https://u2:[email protected]/b/"},
108109
]
109110
netrc_path = write_credentials_netrc(sources, tmp_path)
110-
contents = open(netrc_path).read()
111+
with open(netrc_path, encoding="utf-8") as f:
112+
contents = f.read()
111113
# Only the first occurrence is kept — pipenv resolves auth by host so
112114
# repeats would just collide anyway.
113115
assert contents.count("machine same.example.com") == 1
@@ -122,7 +124,8 @@ def test_write_credentials_netrc_decodes_url_encoded_password(tmp_path):
122124
server."""
123125
sources = [{"url": "https://user:p%40ss%[email protected]/simple"}]
124126
netrc_path = write_credentials_netrc(sources, tmp_path)
125-
contents = open(netrc_path).read()
127+
with open(netrc_path, encoding="utf-8") as f:
128+
contents = f.read()
126129
assert "password p@ss!" in contents
127130

128131

@@ -211,7 +214,8 @@ def communicate(self):
211214
assert "SuperSecret123" not in env.get("PIP_INDEX_URL", "")
212215
netrc_path = env.get("NETRC")
213216
assert netrc_path and os.path.isfile(netrc_path)
214-
netrc_contents = open(netrc_path).read()
217+
with open(netrc_path, encoding="utf-8") as f:
218+
netrc_contents = f.read()
215219
assert "SuperSecret123" in netrc_contents
216220
assert "machine 10.255.255.1" in netrc_contents
217221

0 commit comments

Comments
 (0)