Skip to content

Commit

Permalink
[yashaka#519] Fix: Format path as URI
Browse files Browse the repository at this point in the history
This method will format local file paths into clickable URIs. Handles
paths in both Windows and Unix based paths.
  • Loading branch information
cshimm committed Apr 26, 2024
2 parents 4d7b027 + f4d74c2 commit 9cd5e22
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions selene/core/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,16 @@ def with_(self, **options_to_override) -> Config:
)
return persistent.replace(self, **options)

def _format_path_as_uri(self, path):
"""Converts a local file path to a URI that can be clicked in most editors and browsers."""
prefix = 'file://'
if os.name == 'nt': # Windows specific
# Replace backslashes with forward slashes and prepend with 'file://'
return f"{prefix}{path.replace(os.sep, '/')}"
else:
# Unix-based paths
return f"{prefix}{path}"

def _generate_filename(self, prefix='', suffix=''):
path = self.reports_folder
next_id = next(self._counter)
Expand All @@ -1409,10 +1419,11 @@ def _inject_screenshot_and_page_source_pre_hooks(self, hook):
# or refactor somehow to eliminate all times defining hook fns
def save_and_log_screenshot(error: TimeoutException) -> Exception:
path = self._save_screenshot_strategy(self) # type: ignore
uri = self._format_path_as_uri(path)
return TimeoutException(
error.msg
+ f'''
Screenshot: file://{path}'''
Screenshot: {uri}'''
)

def save_and_log_page_source(error: TimeoutException) -> Exception:
Expand All @@ -1425,7 +1436,8 @@ def save_and_log_page_source(error: TimeoutException) -> Exception:
)

path = self._save_page_source_strategy(self, filename)
return TimeoutException(error.msg + f'\nPageSource: file://{path}')
uri = self._format_path_as_uri(path)
return TimeoutException(error.msg + f'\nPageSource: {uri}')

return fp.pipe(
save_and_log_screenshot if self.save_screenshot_on_failure else None,
Expand Down

0 comments on commit 9cd5e22

Please sign in to comment.