Skip to content

Commit

Permalink
test: Use keyword argument in constructor
Browse files Browse the repository at this point in the history
Should have been included in af677a1.
  • Loading branch information
jmgate committed Jul 10, 2024
1 parent 4d6e826 commit f9bf643
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions test/test_shell_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def shell_logger() -> ShellLogger:
The parent :class:`ShellLogger` object described above.
"""
# Initialize a parent `ShellLogger`.
parent = ShellLogger("Parent", Path.cwd())
parent = ShellLogger("Parent", log_dir=Path.cwd())

# Run the command.
# `stdout` ; `stderr`
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_initialization_creates_stream_dir() -> None:
creates a temporary directory
(``log_dir/%Y-%m-%d_%H%M%S``<random string>) if not already created.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
timestamp = logger.init_time.strftime("%Y-%m-%d_%H.%M.%S.%f")
assert len(list(Path.cwd().glob(f"{timestamp}_*"))) == 1

Expand All @@ -105,7 +105,7 @@ def test_initialization_creates_html_file() -> None:
Verify the initialization of a parent :class:`ShellLogger` object
creates a starting HTML file in the :attr:`log_dir`.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
timestamp = logger.init_time.strftime("%Y-%m-%d_%H.%M.%S.%f")
streamm_dir = next(Path.cwd().glob(f"{timestamp}_*"))
assert (streamm_dir / f"{stack()[0][3]}.html").exists()
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_log_method_return_info_works_correctly(
return_info: Whether or not to return the
``stdout``/``stderr``.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())

# `stdout` ; `stderr`
cmd = "echo 'Hello world out'; echo 'Hello world error' 1>&2"
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_log_method_live_stdout_stderr_works_correctly(
live_stderr: Whether or not to capture ``stderr`` while running
the :func:`log` command.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
cmd = "echo 'Hello world out'; echo 'Hello world error' 1>&2"
logger.log(
"test cmd",
Expand Down Expand Up @@ -404,7 +404,7 @@ def test_json_file_can_reproduce_html_file(

def test_under_stress() -> None:
"""Test that all is well when handling lots of output."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
cmd = (
"dd if=/dev/urandom bs=1024 count=262144 | "
"LC_ALL=C tr -c '[:print:]' '*' ; sleep 1"
Expand All @@ -416,7 +416,7 @@ def test_under_stress() -> None:

def test_heredoc() -> None:
"""Ensure that heredocs in the command to be executed work."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
cmd = "bash << EOF\necho hello\nEOF"
msg = "Test out a heredoc"
result = logger.log(msg, cmd)
Expand All @@ -425,7 +425,7 @@ def test_heredoc() -> None:

def test_devnull_stdin() -> None:
"""Ensure ``stdin`` is redirected to ``/dev/null`` by default."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
cmd = "cat"
msg = "Make sure stdin is redirected to /dev/null by default"
result = logger.log(msg, cmd)
Expand All @@ -434,7 +434,7 @@ def test_devnull_stdin() -> None:

def test_syntax_error() -> None:
"""Ensure syntax errors are handled appropriately."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
cmd = "echo (this is a syntax error"
msg = "Test out a syntax error"
with pytest.raises(RuntimeError) as excinfo:
Expand All @@ -445,7 +445,7 @@ def test_syntax_error() -> None:
@pytest.mark.skipif(psutil is None, reason="`psutil` is unavailable")
def test_logger_does_not_store_stdout_string_by_default() -> None:
"""Ensure we don't hold a commands ``stdout`` in memory by default."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
cmd = (
"dd if=/dev/urandom bs=1024 count=262144 | "
"LC_ALL=C tr -c '[:print:]' '*' ; sleep 1"
Expand All @@ -465,7 +465,7 @@ def test_logger_does_not_store_stdout_string_by_default() -> None:
)
def test_logger_does_not_store_trace_string_by_default() -> None:
"""Ensure we don't keep trace output in memory by default."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
logger.log("echo hello", "echo hello", cwd=Path.cwd(), trace="ltrace")
assert logger.log_book[0]["trace"] is None
logger.log(
Expand All @@ -480,34 +480,34 @@ def test_logger_does_not_store_trace_string_by_default() -> None:

def test_stdout() -> None:
"""Ensure printing to ``stdout`` works."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
assert logger._run(":").stdout == ""
assert logger._run("echo hello").stdout == "hello\n"


def test_returncode_no_op() -> None:
"""Ensure the return code for the `:` command is 0."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
assert logger._run(":").returncode == 0


def test_args() -> None:
"""Ensure we accurately capture the command that was run."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
assert logger._run("echo hello").args == "echo hello"


def test_stderr() -> None:
"""Ensure we accurately capture ``stderr``."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
command = "echo hello 1>&2"
assert logger._run(command).stderr == "hello\n"
assert logger._run(command).stdout == ""


def test_timing() -> None:
"""Ensure we accurately capture the wall clock time of a command."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
command = "sleep 1"
if os.name == "posix":
command = "sleep 1"
Expand All @@ -527,7 +527,7 @@ def test_auxiliary_data() -> None:
Ensure we accurately capture all the auxiliary data when executing a
command.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
result = logger._run("pwd")
assert result.pwd == result.stdout.strip()
result = logger._run(":")
Expand All @@ -554,7 +554,7 @@ def test_working_directory() -> None:
Ensure we accurately capture the working directory when executing a
command.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
command = "pwd"
directory = "/tmp"
if os.name != "posix":
Expand All @@ -566,7 +566,7 @@ def test_working_directory() -> None:

def test_trace() -> None:
"""Ensure we accurately capture trace output."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
if os.uname().sysname == "Linux":
result = logger._run("echo letter", trace="ltrace")
assert 'getenv("POSIXLY_CORRECT")' in result.trace
Expand All @@ -582,7 +582,7 @@ def test_trace() -> None:

def test_trace_expression() -> None:
"""Ensure specifying a trace expression works correctly."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
if os.uname().sysname == "Linux":
result = logger._run("echo hello", trace="ltrace", expression="getenv")
assert 'getenv("POSIXLY_CORRECT")' in result.trace
Expand All @@ -597,7 +597,7 @@ def test_trace_expression() -> None:

def test_trace_summary() -> None:
"""Ensure requesting a trace summary works correctly."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
if os.uname().sysname == "Linux":
result = logger._run("echo hello", trace="ltrace", summary=True)
assert 'getenv("POSIXLY_CORRECT")' not in result.trace
Expand All @@ -615,7 +615,7 @@ def test_trace_summary() -> None:

def test_trace_expression_and_summary() -> None:
"""Ensure specifying a trace expression and requesting a summary works."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
if os.uname().sysname == "Linux":
echo_location = logger._run("which echo").stdout.strip()
result = logger._run(
Expand All @@ -639,7 +639,7 @@ def test_trace_expression_and_summary() -> None:

def test_stats() -> None:
"""Ensure capturing CPU, memory, and disk statistics works correctly."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
measure = ["cpu", "memory", "disk"]
result = logger._run("sleep 2", measure=measure, interval=0.1)
min_results, max_results = 1, 30
Expand All @@ -664,7 +664,7 @@ def test_trace_and_stats() -> None:
Ensure both tracing a command and capturing multiple statistics work
together.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
if os.uname().sysname == "Linux":
measure = ["cpu", "memory", "disk"]
result = logger._run(
Expand Down Expand Up @@ -697,7 +697,7 @@ def test_trace_and_stat() -> None:
Ensure both tracing a command and capturing a single statistic work
together.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
if os.uname().sysname == "Linux":
result = logger._run(
"sleep 1",
Expand Down Expand Up @@ -725,7 +725,7 @@ def test_trace_and_stat() -> None:
@pytest.mark.skip(reason="Not sure it's worth it to fix this or not")
def test_set_env_trace() -> None:
"""Ensure environment variables work with trace."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
result = logger._run("TEST_ENV=abdc env | grep TEST_ENV", trace="ltrace")
assert "TEST_ENV=abdc" in result.stdout
result = logger._run("TEST_ENV=abdc env | grep TEST_ENV", trace="strace")
Expand All @@ -735,7 +735,7 @@ def test_set_env_trace() -> None:
def test_log_book_trace_and_stats() -> None:
"""Ensure trace and statistics are accurately captured in the log book."""
if os.uname().sysname == "Linux":
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
measure = ["cpu", "memory", "disk"]
logger.log(
"Sleep",
Expand Down Expand Up @@ -764,7 +764,7 @@ def test_log_book_trace_and_stats() -> None:

def test_change_pwd() -> None:
"""Ensure changing directories affects subsequent calls."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
pwd_command = "pwd"
directory1 = "/"
directory2 = "/tmp"
Expand All @@ -782,7 +782,7 @@ def test_change_pwd() -> None:

def test_returncode() -> None:
"""Ensure we get the expected return code when a command fails."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
command = "false"
expected_returncode = 1
if os.name != "posix":
Expand All @@ -798,7 +798,7 @@ def test_sgr_gets_converted_to_html() -> None:
Ensure Select Graphic Rendition (SGR) codes get accurately
translated to valid HTML/CSS.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
logger.print("\x1b[31mHello\x1b[0m")
logger.print("\x1b[31;43m\x1b[4mthere\x1b[0m")
logger.print("\x1b[38;5;196m\x1b[48;5;232m\x1b[4mmr.\x1b[0m logger")
Expand Down Expand Up @@ -834,7 +834,7 @@ def test_html_print(capsys: CaptureFixture) -> None:
Parameters:
capsys: A fixture for capturing the ``stdout``.
"""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
logger.html_print(
"The quick brown fox jumps over the lazy dog.", msg_title="Brown Fox"
)
Expand All @@ -860,7 +860,7 @@ def test_html_print(capsys: CaptureFixture) -> None:

def test_append_mode() -> None:
"""Ensure we're able to append to a previously generated log file."""
logger1 = ShellLogger(stack()[0][3] + "_1", Path.cwd())
logger1 = ShellLogger(stack()[0][3] + "_1", log_dir=Path.cwd())
logger1.log("Print HELLO to stdout", "echo HELLO")
logger1.print("Printed once to stdout")
logger1.html_print("Printed ONCE to STDOUT")
Expand Down Expand Up @@ -914,7 +914,7 @@ def test_append_mode() -> None:

def test_invalid_decodings() -> None:
"""Ensure we appropriately handle invalid bytes when decoding output."""
logger = ShellLogger(stack()[0][3], Path.cwd())
logger = ShellLogger(stack()[0][3], log_dir=Path.cwd())
result = logger.log(
"Print invalid start byte for bytes decode()",
"printf '\\xFDHello\\n'",
Expand Down

0 comments on commit f9bf643

Please sign in to comment.