basilisp-lang / basilisp

A Clojure-compatible(-ish) Lisp dialect targeting Python 3.8+
https://basilisp.readthedocs.io
Eclipse Public License 1.0
258 stars 7 forks source link

`basilisp test` fails when attempting to `print` anything out from inside a test file on MS-Windows #1080

Open ikappaki opened 1 week ago

ikappaki commented 1 week ago

Hi,

when running basilisp test, an error occurs when trying to print from a test file

    (.write stdout os/linesep)
E   OSError: [WinError 6] The handle is invalid

To reproduce,

  1. Clone https://github.com/ikappaki/issue-bas-test-macro-print example project, and setup
    git clone https://github.com/ikappaki/issue-bas-test-macro-print
    cd issue-bas-test-macro-print
    poetry install
    poetry shell
  2. Run the tests with basilisp, you should see the error
    
    basilisp test

==================================================== test session starts ==================================================== platform win32 -- Python 3.11.4, pytest-8.3.3, pluggy-1.5.0 rootdir: D:\bas\issue-bas-test-macro-print configfile: pyproject.toml plugins: basilisp-0.2.4 collected 0 items / 1 error

========================================================== ERRORS =========================================================== _ ERROR collecting tests/issuetests/issue_test.lpy __ ... During handling of the above exception, another exception occurred: tests\issuetests\issue_test.lpy:4: in (println :calc (+ 5 3)) ..\issuetests.venv\Lib\site-packages\basilisp\core.lpy:4376: in println (defn println ..\issuetests.venv\Lib\site-packages\basilisp\core.lpy:4398: in printlnarity_rest (.write stdout os/linesep) E OSError: [WinError 6] The handle is invalid ===================================================== warnings summary ====================================================== ..\issuetests.venv\Lib\site-packages_pytest\config__init__.py:1277 d:\bas\issuetests.venv\Lib\site-packages_pytest\config\init__.py:1277: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: basilisp self._mark_plugins_for_rewrite(hook)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ================================================== short test summary info ================================================== ERROR tests/issuetests/issue_test.lpy - OSError: [WinError 6] The handle is invalid !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ================================================ 1 warning, 1 error in 0.11s ================================================ :calc 8


The test file just tries to printout a value (I've also added a test event though is irrelevant)
``` clojure
(ns tests.issuetests.issue-test
  (:require [basilisp.test :refer [deftest are is testing]]))

(println :calc (+ 5 3))

(deftest 5-test []
  (is (= 5 5)))

Commenting out the println form allows the test to run without error.

This could as well be a MS-Windows only issue, I haven't had the chance to test on another architecture yet.

Thanks

ikappaki commented 6 days ago

This issue is specific to MS-Windows and works fine on Linux.

It only occurs when pytest is invoked programmatically.

Here are a couple of workarounds

  1. Instruct pytest not to capture output
    
    > basilisp test -- -s
    ==================================================== test session starts ====================================================
    platform win32 -- Python 3.11.4, pytest-8.3.3, pluggy-1.5.0
    configfile: pyproject.toml
    plugins: basilisp-0.2.4
    collecting ... :calc 8
    collected 1 item

tests\issuetests\issue_test.lpy .

===================================================== 1 passed in 0.03s =====================================================

2. Run `pytest` directly, ensure the empty dir is added to the `PYTHONPATH` so it can pick up the local directory. For example on PowerShell:
``` powershell
> $env:PYTHONPATH="."
> pytest
==================================================== test session starts ====================================================
platform win32 -- Python 3.11.4, pytest-8.3.3, pluggy-1.5.0
configfile: pyproject.toml
plugins: basilisp-0.2.4
collecting ... :calc 8
collected 1 item

tests\issuetests\issue_test.lpy .                                                                                      [100%]

===================================================== 1 passed in 0.02s =====================================================

I'll have a look. Thanks