From 189c2e342325221cd9a29645217fe7b8e0021a2b Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:03:08 -0400 Subject: [PATCH 1/7] add a test to demonstrate issue 13 is not an issue --- src/wxflow/__init__.py | 2 +- src/wxflow/executable.py | 2 +- tests/test_executable.py | 24 +++++++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/wxflow/__init__.py b/src/wxflow/__init__.py index 1f9efa3..f85da17 100644 --- a/src/wxflow/__init__.py +++ b/src/wxflow/__init__.py @@ -4,7 +4,7 @@ from .configuration import (Configuration, cast_as_dtype, cast_strdict_as_dtypedict) from .exceptions import WorkflowException, msg_except_handle -from .executable import CommandNotFoundError, Executable, which +from .executable import CommandNotFoundError, Executable, ProcessError, which from .factory import Factory from .file_utils import FileHandler from .fsutils import chdir, cp, mkdir, mkdir_p, rm_p, rmdir diff --git a/src/wxflow/executable.py b/src/wxflow/executable.py index b2e2388..f1ce0c7 100644 --- a/src/wxflow/executable.py +++ b/src/wxflow/executable.py @@ -4,7 +4,7 @@ import sys from typing import Any, List, Optional, Union -__all__ = ["Executable", "which", "CommandNotFoundError"] +__all__ = ["Executable", "which", "CommandNotFoundError", "ProcessError"] class Executable: diff --git a/tests/test_executable.py b/tests/test_executable.py index d137114..2182bb2 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -3,7 +3,7 @@ import pytest -from wxflow import CommandNotFoundError, Executable, which +from wxflow import CommandNotFoundError, Executable, ProcessError, which script = """#!/bin/bash echo ${USER} @@ -59,3 +59,25 @@ def test_which(tmpdir): exe = which("test.x") assert exe is not None assert exe.path == path + + +def test_stderr(tmp_path): + """ + Tests the `stderr` attribute of the `Executable` class + """ + cmd = which("ls", required=True) + + stdout_file = tmp_path / 'stdout' + stderr_file = tmp_path / 'stderr' + with pytest.raises(ProcessError): + cmd("--help", output=str(stdout_file), error=str(stderr_file)) + + # Assert there is no stdout + with open(str(stdout_file)) as fh: + assert fh.read() == '' + + # Assert stderr is not empty, '--help' is an unrecognized option + with open(str(stderr_file)) as fh: + stderr = fh.read() + assert stderr != '' + print(stderr) From ef94324e3b367cd192ce3bb212355e7f3792a756 Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:21:11 -0400 Subject: [PATCH 2/7] ls was not found on GH runner. use a different program to test stderr --- tests/test_executable.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_executable.py b/tests/test_executable.py index 2182bb2..03aeed3 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -65,19 +65,21 @@ def test_stderr(tmp_path): """ Tests the `stderr` attribute of the `Executable` class """ - cmd = which("ls", required=True) + cmd = which("which", required=True) stdout_file = tmp_path / 'stdout' stderr_file = tmp_path / 'stderr' with pytest.raises(ProcessError): - cmd("--help", output=str(stdout_file), error=str(stderr_file)) + cmd("-h", output=str(stdout_file), error=str(stderr_file)) # Assert there is no stdout with open(str(stdout_file)) as fh: assert fh.read() == '' - # Assert stderr is not empty, '--help' is an unrecognized option + # Assert stderr is not empty, '-h' is a bad option for `which` with open(str(stderr_file)) as fh: stderr = fh.read() assert stderr != '' - print(stderr) + # Depending on the OS, the error message may vary + # This was seen on macOS + # assert stderr == "/usr/bin/which: illegal option -- h" + '\n' + "usage: which [-as] program ..." + '\n' From c578b59bb1e75dc18860644f518a5a1ad5690add Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:28:59 -0400 Subject: [PATCH 3/7] put /bin and /usr/bin in PATH and try again --- tests/test_executable.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/test_executable.py b/tests/test_executable.py index 03aeed3..9c8ef59 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -65,21 +65,26 @@ def test_stderr(tmp_path): """ Tests the `stderr` attribute of the `Executable` class """ - cmd = which("which", required=True) + + # Put "/usr/bin" and "/bin" in the PATH + os.environ["PATH"] = "/usr/bin:/bin" + + cmd = which("ls", required=True) stdout_file = tmp_path / 'stdout' stderr_file = tmp_path / 'stderr' with pytest.raises(ProcessError): - cmd("-h", output=str(stdout_file), error=str(stderr_file)) + cmd("--help", output=str(stdout_file), error=str(stderr_file)) # Assert there is no stdout with open(str(stdout_file)) as fh: assert fh.read() == '' - # Assert stderr is not empty, '-h' is a bad option for `which` + # Assert stderr is not empty, '--help' is an unrecognized option with open(str(stderr_file)) as fh: stderr = fh.read() assert stderr != '' # Depending on the OS, the error message may vary # This was seen on macOS - # assert stderr == "/usr/bin/which: illegal option -- h" + '\n' + "usage: which [-as] program ..." + '\n' + # assert stderr == "ls: unrecognized option `--help'" + '\n' + \ + # "usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]" + '\n' From 1716d5508c679b81183e8feba1e7e05caa4dcc0f Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:40:10 -0400 Subject: [PATCH 4/7] put /bin and /usr/bin in PATH and try again --- tests/test_executable.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_executable.py b/tests/test_executable.py index 9c8ef59..c512661 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -60,15 +60,13 @@ def test_which(tmpdir): assert exe is not None assert exe.path == path - +@pytest.mark.skip(reason="test passes locally, but fails in GH runner") def test_stderr(tmp_path): """ Tests the `stderr` attribute of the `Executable` class + TODO: This test fails in the GH runner, but passes locally. Investigate why. """ - # Put "/usr/bin" and "/bin" in the PATH - os.environ["PATH"] = "/usr/bin:/bin" - cmd = which("ls", required=True) stdout_file = tmp_path / 'stdout' From 112ad33a3cef5dc5c4cf0e837987f10d96471257 Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:40:41 -0400 Subject: [PATCH 5/7] ls was not found on GH runner. use a different program to test stderr --- tests/test_executable.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_executable.py b/tests/test_executable.py index c512661..764ba8d 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -60,6 +60,7 @@ def test_which(tmpdir): assert exe is not None assert exe.path == path + @pytest.mark.skip(reason="test passes locally, but fails in GH runner") def test_stderr(tmp_path): """ From f17a3c211787289b8a8ea728d9b10bbf1e2ff4f2 Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:48:24 -0400 Subject: [PATCH 6/7] possibly fixes this on GH runner --- tests/test_executable.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/test_executable.py b/tests/test_executable.py index 764ba8d..70be377 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -61,19 +61,16 @@ def test_which(tmpdir): assert exe.path == path -@pytest.mark.skip(reason="test passes locally, but fails in GH runner") def test_stderr(tmp_path): """ Tests the `stderr` attribute of the `Executable` class - TODO: This test fails in the GH runner, but passes locally. Investigate why. """ cmd = which("ls", required=True) stdout_file = tmp_path / 'stdout' stderr_file = tmp_path / 'stderr' - with pytest.raises(ProcessError): - cmd("--help", output=str(stdout_file), error=str(stderr_file)) + cmd("--myopt", output=str(stdout_file), error=str(stderr_file), fail_on_error=False) # Assert there is no stdout with open(str(stdout_file)) as fh: @@ -83,7 +80,8 @@ def test_stderr(tmp_path): with open(str(stderr_file)) as fh: stderr = fh.read() assert stderr != '' + print(stderr) # Depending on the OS, the error message may vary # This was seen on macOS - # assert stderr == "ls: unrecognized option `--help'" + '\n' + \ + # assert stderr == "ls: unrecognized option `--myopt'" + '\n' + \ # "usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]" + '\n' From 6ff9b2f5369bc3adc45775c4c2ab00dcdef76f1b Mon Sep 17 00:00:00 2001 From: Rahul Mahajan Date: Sat, 23 Mar 2024 06:49:59 -0400 Subject: [PATCH 7/7] possibly fixes this on GH runner --- tests/test_executable.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_executable.py b/tests/test_executable.py index 70be377..2bb734e 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -66,6 +66,8 @@ def test_stderr(tmp_path): Tests the `stderr` attribute of the `Executable` class """ + os.environ["PATH"] = "/usr/bin:/bin" + cmd = which("ls", required=True) stdout_file = tmp_path / 'stdout'