I am not a new developer, but I am new to python.
I am modifying an existing routine that checks for a token in an environment variable. I'm modifying it to look in .netrc
if the token does not exist in the environment and I want to test it, but I can't figure out how to mock a missing file. The code looks like this.
def _get_path() -> str:;
"""Get personal access token."""
pat = os.environ["MY_TOKEN"]
if not pat:
try:
pat = netrc.netrc().authenticators('my.site')[2]
except netrc.NetrcParseError as ne:
print(f"Parse error in ${ne.filename} at ${ne.lineno}: ${ne.msg})
else:
raise ValueError('No PAT found')
return pat
This defaults to looking for $HOME/.netrc
, which exists in my environment but may not in another developers environment. It certainly won't exist in the test environment.
I'm pretty sure I can figure out how to test a bad .netrc
but I just can't seem to figure out how to use pytest, mock or mockito to test for a missing $HOME/.netrc
regardless of whether or not aa real one exists.
How do I do this?
Edit: To clarify, the testing environment may have the MY_TOKEN variable set and/or may have $HOME/.netrc
... how can I test the non-existence of $HOME/.netrc
regardless of whether or not it exists in the testing environment?
This sounds like a good time to use
if not os.path.exists(filepath):