Skip to content

Commit

Permalink
Remove empty logging folders
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed Aug 18, 2023
1 parent b3dd883 commit 39aaaef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
9 changes: 4 additions & 5 deletions seleniumbase/behave/behave_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def get_configured_sb(context):
if sb_config.dash_title:
constants.Dashboard.TITLE = sb_config.dash_title.replace("_", " ")

log_helper.log_folder_setup(sb.log_path, sb.archive_logs)
log_helper.log_folder_setup("latest_logs/", sb.archive_logs)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()
return sb
Expand Down Expand Up @@ -1152,10 +1152,9 @@ def _perform_behave_unconfigure_():
except Exception:
pass
sb_config.shared_driver = None
if hasattr(sb_config, "log_path"):
log_helper.archive_logs_if_set(
sb_config.log_path, sb_config.archive_logs
)
if hasattr(sb_config, "archive_logs"):
log_helper.archive_logs_if_set("latest_logs/", sb_config.archive_logs)
log_helper.clear_empty_logs()
# Dashboard post-processing: Disable time-based refresh and stamp complete
if not hasattr(sb_config, "dashboard") or not sb_config.dashboard:
# Done with "behave_unconfigure" unless using the Dashboard
Expand Down
15 changes: 15 additions & 0 deletions seleniumbase/core/log_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,18 @@ def log_folder_setup(log_path, archive_logs=False):
pass
else:
shutil.rmtree(archived_logs) # (Archive test run later)


def clear_empty_logs():
latest_logs_dir = os.path.join(os.getcwd(), "latest_logs") + os.sep

This comment has been minimized.

Copy link
@SzilvasiPeter

SzilvasiPeter Aug 19, 2023

Hard-coded path values can be problematic if the path changes.

This comment has been minimized.

Copy link
@mdmintz

mdmintz Aug 19, 2023

Author Member

That one is hard-coded on purpose. latest_logs is cleared at the start of every test run. I don’t want people to accidentally set that path to an important one.

This comment has been minimized.

Copy link
@mdmintz

mdmintz Aug 19, 2023

Author Member

It’s also relative to the location where tests are called from.

This comment has been minimized.

Copy link
@SzilvasiPeter

SzilvasiPeter Aug 19, 2023

I see. I mean, I am not too worried about the hard-coded strings, rather the code duplication. The "latest_logs" string are present in multiple places:

Extracting the string into a constant variable will help update the log path more easily across the project. It also decreases the code readability a little bit. (Adaptability vs Readability tradeoff)

This comment has been minimized.

archived_folder = os.path.join(os.getcwd(), "archived_logs") + os.sep
if os.path.exists(latest_logs_dir) and not os.listdir(latest_logs_dir):
try:
os.rmdir(latest_logs_dir)
except OSError:
pass
if os.path.exists(archived_folder) and not os.listdir(archived_folder):
try:
os.rmdir(archived_folder)
except OSError:
pass
1 change: 1 addition & 0 deletions seleniumbase/plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def finalize(self, result):
log_helper.archive_logs_if_set(
self.options.log_path, self.options.archive_logs
)
log_helper.clear_empty_logs()
if self.report_on:
if not self.import_error:
report_helper.add_bad_page_log_file(self.page_results_list)
Expand Down
9 changes: 4 additions & 5 deletions seleniumbase/plugins/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ def pytest_configure(config):
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper

log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)
log_helper.log_folder_setup("latest_logs/", sb_config.archive_logs)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()

Expand Down Expand Up @@ -1810,7 +1810,7 @@ def pytest_collection_finish(session):
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper

log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)
log_helper.log_folder_setup("latest_logs/", sb_config.archive_logs)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()
if sb_config.dashboard and len(session.items) > 0:
Expand Down Expand Up @@ -2016,9 +2016,8 @@ def _perform_pytest_unconfigure_():
pass
sb_config.shared_driver = None
if hasattr(sb_config, "log_path") and sb_config.item_count > 0:
log_helper.archive_logs_if_set(
sb_config.log_path, sb_config.archive_logs
)
log_helper.archive_logs_if_set("latest_logs/", sb_config.archive_logs)
log_helper.clear_empty_logs()
# Dashboard post-processing: Disable time-based refresh and stamp complete
if not hasattr(sb_config, "dashboard") or not sb_config.dashboard:
# Done with "pytest_unconfigure" unless using the Dashboard
Expand Down
5 changes: 3 additions & 2 deletions seleniumbase/plugins/sb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def SB(
interval=None, # SECONDS (Autoplay interval for SB Slides & Tour steps.)
time_limit=None, # SECONDS (Safely fail tests that exceed the time limit.)
):
import os
import sys
import time
import traceback
Expand Down Expand Up @@ -809,7 +810,6 @@ def SB(
terminal_width = shared_utils.get_terminal_width()
if test:
import colorama
import os

colorama.init(autoreset=True)
c1 = colorama.Fore.GREEN
Expand All @@ -834,7 +834,8 @@ def SB(
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper

log_helper.log_folder_setup(sb_config.log_path)
log_helper.log_folder_setup("latest_logs/")
log_helper.clear_empty_logs()
download_helper.reset_downloads_folder()
if not sb_config.multi_proxy:
proxy_helper.remove_proxy_zip_if_present()
Expand Down

0 comments on commit 39aaaef

Please sign in to comment.