From f981cbf89465cb5127f00e82acdae20fdeb659d9 Mon Sep 17 00:00:00 2001 From: Pallas Cain <89534001+PCain02@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:37:17 -0500 Subject: [PATCH 1/3] fix: test_main.py so it works on Windows --- tests/test_main.py | 103 ++++++++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 30 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 5043f8d..7ab9d5e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -3,6 +3,8 @@ import os import subprocess import tempfile +import sys +import venv from pathlib import Path import pytest @@ -12,43 +14,84 @@ EXPECTED_EXIT_CODE_FILE_NOT_FOUND = 4 +@pytest.fixture +def poetry_env(): + """Create a temporary virtual environment with poetry installed.""" + with tempfile.TemporaryDirectory() as temp_dir: + venv_path = Path(temp_dir) / "venv" + # Create a virtual environment + venv.create(venv_path, with_pip=True) + # Get the path to the Python executable in the virtual environment + if sys.platform == "win32": + python_path = venv_path / "Scripts" / "python.exe" + pip_path = venv_path / "Scripts" / "pip.exe" + else: + python_path = venv_path / "bin" / "python" + pip_path = venv_path / "bin" / "pip" + # Install poetry in the virtual environment + subprocess.run( + [str(pip_path), "install", "poetry"], + check=True, + capture_output=True, + text=True + ) + yield str(python_path) @pytest.fixture def cwd(): """Define a test fixture for the current working directory.""" return os.getcwd() - -def test_run_with_missing_test(cwd): +@pytest.mark.no_print +def test_run_with_missing_test(cwd, poetry_env, capfd): """Test the run command with default options.""" with tempfile.TemporaryDirectory() as temp_dir: test_one = Path(temp_dir) / "test_one" test_one.mkdir() - - # Run the CLI command in a subprocess - result = subprocess.run( - [ - "poetry", - "run", - "execexam", - ".", - os.path.join(".", "tests", "test_question_one.py"), - "--report", - "trace", - "--report", - "status", - "--report", - "failure", - "--report", - "code", - "--report", - "setup", - ], - capture_output=True, - text=True, - encoding="utf-8", # Ensure correct handling of Unicode characters - check=False, - ) - - # Check the return code - assert result.returncode == EXPECTED_EXIT_CODE_FILE_NOT_FOUND + test_path = Path(".") / "tests" / "test_question_one.py" + test_path_str = str(test_path) + env = os.environ.copy() + if sys.platform == "win32": + env["PYTHONIOENCODING"] = "utf-8" + env["PYTHONUTF8"] = "1" + try: + # Disable output capture temporarily + with capfd.disabled(): + result = subprocess.run( + [ + poetry_env, + "-m", + "poetry", + "run", + "execexam", + ".", + test_path_str, + "--report", + "trace", + "--report", + "status", + "--report", + "failure", + "--report", + "code", + "--report", + "setup", + ], + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + check=False, + env=env, + cwd=cwd + ) + assert result.returncode in [EXPECTED_EXIT_CODE_FILE_NOT_FOUND], \ + f"Expected return code {EXPECTED_EXIT_CODE_FILE_NOT_FOUND}, got {result.returncode}" + assert "file or directory not found" in result.stdout.lower() or \ + "no such file or directory" in result.stderr.lower(), \ + "Expected error message about missing file not found in output" + except UnicodeDecodeError as e: + pytest.fail(f"Unicode decode error: {str(e)}") + except Exception as e: + pytest.fail(f"Unexpected error: {str(e)}") + From 237b5a501df27c38158703c474f60fc8bd44ac4d Mon Sep 17 00:00:00 2001 From: Pallas Cain <89534001+PCain02@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:41:07 -0500 Subject: [PATCH 2/3] lint: test_main.py --- tests/test_main.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 7ab9d5e..de22b32 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -14,6 +14,7 @@ EXPECTED_EXIT_CODE_FILE_NOT_FOUND = 4 + @pytest.fixture def poetry_env(): """Create a temporary virtual environment with poetry installed.""" @@ -33,15 +34,17 @@ def poetry_env(): [str(pip_path), "install", "poetry"], check=True, capture_output=True, - text=True + text=True, ) yield str(python_path) + @pytest.fixture def cwd(): """Define a test fixture for the current working directory.""" return os.getcwd() + @pytest.mark.no_print def test_run_with_missing_test(cwd, poetry_env, capfd): """Test the run command with default options.""" @@ -83,15 +86,20 @@ def test_run_with_missing_test(cwd, poetry_env, capfd): errors="replace", check=False, env=env, - cwd=cwd + cwd=cwd, ) - assert result.returncode in [EXPECTED_EXIT_CODE_FILE_NOT_FOUND], \ - f"Expected return code {EXPECTED_EXIT_CODE_FILE_NOT_FOUND}, got {result.returncode}" - assert "file or directory not found" in result.stdout.lower() or \ - "no such file or directory" in result.stderr.lower(), \ - "Expected error message about missing file not found in output" + assert ( + result.returncode in [EXPECTED_EXIT_CODE_FILE_NOT_FOUND] + ), f"Expected return code {EXPECTED_EXIT_CODE_FILE_NOT_FOUND}, got {result.returncode}" + assert ( + "file or directory not found" in result.stdout.lower() + or "no such file or directory" in result.stderr.lower() + ), "Expected error message about missing file not found in output" except UnicodeDecodeError as e: pytest.fail(f"Unicode decode error: {str(e)}") except Exception as e: pytest.fail(f"Unexpected error: {str(e)}") + + except Exception as e: + pytest.fail(f"Unexpected error: {str(e)}") From d0e9ccd7caf62fa1ab802b6c11474cce30fd56dc Mon Sep 17 00:00:00 2001 From: Pallas Cain <89534001+PCain02@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:43:06 -0500 Subject: [PATCH 3/3] lint: test_main.py