-
-
Notifications
You must be signed in to change notification settings - Fork 259
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
Detect terminal text-based-browsers better and block them #1483
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,6 +410,8 @@ def open_in_browser(self, url: str) -> None: | |
try: | ||
# Checks for a runnable browser in the system and returns | ||
# its browser controller, if found, else reports an error | ||
term = os.environ.get("TERM") # Saving the original value | ||
del os.environ["TERM"] # Temporarily deleting variable | ||
Comment on lines
+413
to
+414
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like |
||
browser_controller = webbrowser.get() | ||
# Suppress stdout and stderr when opening browser | ||
with suppress_output(): | ||
|
@@ -424,6 +426,8 @@ def open_in_browser(self, url: str) -> None: | |
[f"The link was successfully opened using {browser_name}"] | ||
) | ||
except webbrowser.Error as e: | ||
if isinstance(term, str): | ||
os.environ["TERM"] = term # resetting | ||
Comment on lines
+429
to
+430
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This certainly shouldn't be in the exceptional block. As it is now, TERM will not be reset if there is no error. If you want something to happen in every case, you could use The simplest solution would be to reset TERM as soon as the browser is determined. If so, this may be clearer with two try blocks, so you can isolate the 'only-terminal-browsers' error from others. That is, unless the webbrowser module does other detection of this kind after that point, which could require the environment to be set for later operations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
# Set a footer text if no runnable browser is located | ||
self.report_error([f"ERROR: {e}"]) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your test changes make the tests pass, but don't test the expectations from the new behavior:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused about writing the tests for this. The
TERM
environ doesn't really change, how do we test its transitory absence?Also writing a test specifically for text_browser sounds good but again not sure what we can test. We can assert if
TERM
environ doesn't exist inos.environ but that's a value we are setting and not what we are fetching from the function.