Description
Describe the bug
Several unit tests rely inadvertently on behavior that varies by operating system, in ways that cause them not to work on Windows. These tests work on other platforms, just not Windows. This is a bug in the tests, not the code under test. I've opened #537 to fix them.
The affected tests are:
File | Test |
---|---|
test_file_cli.py |
test_file_cli |
test_long_examples_validator.py |
test_long_examples_validator |
test_util.py |
test_openai_api_key_path |
test_util.py |
test_openai_api_key_path_with_malformed_key |
The causes of incompatibility with Windows (some applying to multiple tests) are that, on Windows:
- Attempting to open a file created by
NamedTemporaryFile
while it is already open raisesPermissionError
. (This is the incompatibility that affects the most tests.) - Giving
subprocess.run
a multiple-argument command to run using a shell, when done by passing a one-element list whose element is the whole command, gives thecmd.exe
shell a command it cannot parse correctly. This shell fails with "The filename, directory name, or volume label syntax is incorrect." (Doing it in this specific way was probably unintentional, and the other ways of passing commands throughsubprocess.run
do work.) - The system default encoding is not UTF-8. It is usually CP-1252 (Windows-1252), at least on English-language versions of Windows. This can often be addressed by passing
encoding="utf-8"
where applicable, but as an argument tosubprocess.run
that can cause (rather than avoid) aUnicodeDecodeError
. When the child process is another Python process, its standard streams almost always use the system default encoding.
To Reproduce
- Set up a Python virtual environment on Windows for any version of Python this library supports.
- In it, install the project by running
pip install -e '.[dev,datalib]'
. (Thedatalib
extra is needed fortest_long_examples_validator
, which is skipped ifnumpy
andpandas
are unavailable. For the others, it would be sufficient to runpip install -e '.[dev]'
.) - Run
pytest
to run the tests. (To verify exactly what's going on, it may help to runpytest -vv
.)
This shows the first two kinds of problems described above; the third is only observable once these problems are fixed (and a fix or workaround has been applied for #535).
Developers without access to a Windows system who want to partially verify the details of this bug can examine this CI output (from a workflow based on tuliren's).
Code snippets
As one example of the first problem, test_util.py
uses NamedTemporaryFile
in a fixture, api_key_file
, that allows it to be used this way:
def test_openai_api_key_path(api_key_file) -> None:
print("sk-foo", file=api_key_file)
api_key_file.flush()
assert util.default_api_key() == "sk-foo"
However, the code under test separately attempts to open that file to read an API key. On Windows, this raises PermissionError
.
OS
Windows
Python version
Python 3.7.9, 3.8.10, 3.9.13, 3.10.11, and 3.11.4.
Library version
Tested on b82a3f7 (main branch), though the problem was not introduced recently.