Skip to content

Conversation

@73junito
Copy link

tests: fix Windows tempfile re-open by using delete=False and explicit cleanup

Summary

Fix Windows-specific test failures caused by reopening files created with tempfile.NamedTemporaryFile(); on Windows these files are locked while open. Tests now use tempfile.NamedTemporaryFile(delete=False) and explicit os.unlink() cleanup so they can be reopened cross-platform.

Files changed

  • Test updates in lib/ollama-python/tests/test_type_serialization.py and lib/ollama-python/tests/test_client.py.
  • Patch included at patches/0001-tests-fix-Windows-tempfile-re-open-by-using-delete-F.patch in this repo.
  • Documentation added: PATCHES.md.

Validation

Ran the full test suite on Windows with OLLAMA_AVAILABLE=1 and a project-local TMP/TEMP directory; all tests passed (89 passed). Coverage generated: htmlcov/ and coverage.xml.

Motivation

Prevents platform-specific test flakes and makes CI robust across Windows runners.

Reproduce locally

PowerShell:

$env:OLLAMA_AVAILABLE=1
$env:TMP=$pwd.Path + '\tmp'
$env:TEMP=$env:TMP
pytest --basetemp=tmp/pytest --cov --cov-branch --cov-report=xml --cov-report=html

CI notes

  • Add windows-latest to the test matrix in GitHub Actions to catch platform-specific file-locking issues.
  • Example strategy snippet:
strategy:
  matrix:
    os: [ubuntu-latest, windows-latest, macos-latest]
steps:
  - uses: actions/checkout@v4
  - name: Set up Python
    uses: actions/setup-python@v4
    with:
      python-version: '3.11'
  - name: Install deps
    run: pip install -r requirements.txt
  - name: Run tests
    run: |
      pytest --basetemp=tmp/pytest --cov --cov-branch --cov-report=xml
  - name: Upload coverage
    uses: codecov/codecov-action@v4

Notes

  • Suggested reviewers: maintainers of ollama/ollama-python.

@73junito
Copy link
Author

CI notes and patch link

This PR fixes Windows-specific test failures by making tests use
tempfile.NamedTemporaryFile(delete=False) and explicit os.unlink() cleanup so
files can be reopened on Windows.

Reproduction

PowerShell:

$env:OLLAMA_AVAILABLE=1
$env:TMP=$pwd.Path + '\tmp'
$env:TEMP=$env:TMP
pytest --basetemp=tmp/pytest --cov --cov-branch --cov-report=xml --cov-report=html

Patch (in the parent repo):
https://github.com/73junito/autolearnpro/blob/fix/ollama-python-windows-tests/patches/0001-tests-fix-Windows-tempfile-re-open-by-using-delete-F.patch

Suggested CI change: add windows-latest to the matrix to catch OS-specific file-locking issues.

Example matrix snippet:

strategy:
  matrix:
    os: [ubuntu-latest, windows-latest, macos-latest]

Notes

  • Patch origin: autolearnpro branch fix/ollama-python-windows-tests.

  • If maintainers prefer, we can expand tests or add a Windows job that runs only the relevant tests.
    Reviewer checklist

  • Confirm the updated coverage behavior is acceptable (we added COVERAGE_OPTIONAL to opt out)

  • Confirm the shim behavior is acceptable for your runners (installs to $HOME/bin and is intentionally conservative)

  • Run CI on this branch and validate coverage artifact upload + threshold step

  • (Optional) Suggest alternate installation location if your org requires a different policy

Notes

  • COVERAGE_OPTIONAL defaults to 0. Set it to 1 only for jobs where coverage generation is intentionally skipped (docs-only changes, etc.).
  • The shim is intentionally minimal: it returns empty output for list/ps and exits non-zero for commands that would run models.

@73junito
Copy link
Author

Requesting review from maintainers: this PR fixes Windows-specific test flakes by making tests reopenable on Windows. CI suggestion: add windows-latest to the test matrix. Patch available in autolearnpro branch fix/ollama-python-windows-tests: https://github.com/73junito/autolearnpro/blob/fix/ollama-python-windows-tests/patches/0001-tests-fix-Windows-tempfile-re-open-by-using-delete-F.patch. If you'd like, I can update the PR with additional CI changes or tests.

@73junito
Copy link
Author

Note: I added a CI matrix workflow that includes a Windows job (lib/ollama-python/.github/workflows/ci-matrix.yml) to run tests on ubuntu, windows, and macOS. This should detect Windows-specific tempfile file-lock flakes. The workflow is committed to branch 73junito:fix/windows-tempfile-tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant