Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Tests with temporary files or subprocesses are broken on Windows #536

Closed
@EliahKagan

Description

@EliahKagan

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:

  1. Attempting to open a file created by NamedTemporaryFile while it is already open raises PermissionError. (This is the incompatibility that affects the most tests.)
  2. 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 the cmd.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 through subprocess.run do work.)
  3. 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 to subprocess.run that can cause (rather than avoid) a UnicodeDecodeError. When the child process is another Python process, its standard streams almost always use the system default encoding.

To Reproduce

  1. Set up a Python virtual environment on Windows for any version of Python this library supports.
  2. In it, install the project by running pip install -e '.[dev,datalib]'. (The datalib extra is needed for test_long_examples_validator, which is skipped if numpy and pandas are unavailable. For the others, it would be sufficient to run pip install -e '.[dev]'.)
  3. Run pytest to run the tests. (To verify exactly what's going on, it may help to run pytest -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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions