-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
446: vfs: Handle if __main__ has no __file__ r=pathunstrom a=astronouth7303 Closes #267 Closes #437 Co-authored-by: Jamie Bliss <jamie@ivyleav.es> Co-authored-by: Piper Thunstrom <pathunstrom@gmail.com>
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import contextlib | ||
import os | ||
import tempfile | ||
|
||
import __main__ # The pytest entry point | ||
|
||
import pytest | ||
|
||
import ppb.vfs | ||
|
||
|
||
@contextlib.contextmanager | ||
def save_main_file(): | ||
value = __main__.__file__ | ||
try: | ||
yield | ||
finally: | ||
__main__.__file__ = value | ||
|
||
|
||
@pytest.fixture | ||
def cwd_file(): | ||
# Windows doesn't like multiple open handles, so we need to close and | ||
# delete the temp file manually. | ||
|
||
with tempfile.NamedTemporaryFile(dir=os.getcwd(), delete=False) as ntf: | ||
ntf.close() | ||
yield os.path.basename(ntf.name) | ||
os.remove(ntf.name) | ||
|
||
|
||
def test_main_normal(cwd_file): | ||
with pytest.raises(FileNotFoundError): | ||
with ppb.vfs.open(cwd_file): | ||
pass | ||
|
||
|
||
def test_main_none(cwd_file): | ||
with save_main_file(): | ||
__main__.__file__ = None | ||
with ppb.vfs.open(cwd_file): | ||
pass | ||
|
||
|
||
def test_main_missing(cwd_file): | ||
with save_main_file(): | ||
del __main__.__file__ | ||
with ppb.vfs.open(cwd_file): | ||
pass |