Skip to content

STY: Prefer using pytest temporary path fixtures in tests - #126

Merged
arokem merged 2 commits into
tee-ar-ex:masterfrom
jhlegarreta:sty/use-pytest-temp-path-fixture
Aug 21, 2026
Merged

STY: Prefer using pytest temporary path fixtures in tests#126
arokem merged 2 commits into
tee-ar-ex:masterfrom
jhlegarreta:sty/use-pytest-temp-path-fixture

Conversation

@jhlegarreta

Copy link
Copy Markdown
Contributor

Prefer using the pytest tmp_path fixture in tests:

  • Makes the code more readable as no additional context (and the associated indentation) required by the use of TemporaryDirectory is needed.
  • Simplifies path manipulation as they return a pathlib.Path object and thus, no recurrent casting to Path is necessary.
  • Increases consistency in temporary directory naming, and thus, eases maintenance.
  • Fixes Shadows name 'tmp_dir' from outer scope warnings across test_memmap.py.
  • Remove the unused/unnecessary tmp_dir = get_trx_tmp_dir() statement.

@jhlegarreta
jhlegarreta force-pushed the sty/use-pytest-temp-path-fixture branch 5 times, most recently from 7c9fa9b to 87b3a41 Compare August 11, 2026 07:23
@jhlegarreta

Copy link
Copy Markdown
Contributor Author

This requires some more thinking. The memmap test file was doing

tmp_dir = get_trx_tmp_dir()

But then all test functions requiring a temporary directory were using the same variable name in the context:

with tempfile.TemporaryDirectory() as tmp_dir:

So I do not believe the design was that correct, and this PR is probably uncovering that.

@jhlegarreta
jhlegarreta force-pushed the sty/use-pytest-temp-path-fixture branch 9 times, most recently from 79f3fae to 1fe0c5c Compare August 21, 2026 17:32
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.49%. Comparing base (9e33b8a) to head (0296979).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #126      +/-   ##
==========================================
- Coverage   86.53%   86.49%   -0.05%     
==========================================
  Files          13       13              
  Lines        2896     2887       -9     
==========================================
- Hits         2506     2497       -9     
  Misses        390      390              
Flag Coverage Δ
unittests 86.49% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhlegarreta
jhlegarreta force-pushed the sty/use-pytest-temp-path-fixture branch from 1fe0c5c to 98aa419 Compare August 21, 2026 17:33
Prefer using the `pytest` `tmp_path` fixture in tests over the standard
library `tempfile.TemporaryDirectory()`:
- Makes the code more readable as no additional context (and the
  associated indentation) required by the use of `TemporaryDirectory` is
  needed.
- Simplifies path manipulation as they return a `pathlib.Path` object
  and thus, no recurrent casting to `Path` is necessary.
- Increases consistency in temporary directory naming, and thus, eases
  maintenance.
- Fixes
  ```
  Shadows name 'tmp_dir' from outer scope
  ```
  warnings across `test_memmap.py`.
- Remove the unused/unnecessary `tmp_dir = get_trx_tmp_dir()` statement.
@jhlegarreta
jhlegarreta force-pushed the sty/use-pytest-temp-path-fixture branch from 98aa419 to f99628c Compare August 21, 2026 17:35
Refactor `trx_tmp_dir` test:
- Do not parametrize an argument named `tmp_path`: that name is reserved
  for `pytest`'s built-in fixture, and reusing it as a string parameter
  is misleading/shadowing-prone.
- Clarify test intent by separating concerns:
  - Keep the existing integration-style coverage (through load path
    behavior),
  - add a direct unit test for `get_trx_tmp_dir()` so the env-variable
    contract is verified explicitly.
  The previous test did not clearly test `get_trx_tmp_dir` itself; it
  inferred behavior indirectly via `tmm.load(...)`.
- Replace symbolic "~" handling with explicit env-driven expectations
  (home path value, `use_working_dir`, and unset env -> system temp
  dir), which makes the “home” case non-misleading and properly
  asserted.
- Deduplicate branch-specific assertions by deriving a single expected
  parent directory per parametrized case.
- Use `monkeypatch` for `TRX_TMPDIR` setup/teardown to guarantee env
  isolation and automatic cleanup, preventing cross-test leakage and
  order-dependent failures.
@jhlegarreta
jhlegarreta force-pushed the sty/use-pytest-temp-path-fixture branch from f99628c to 0296979 Compare August 21, 2026 17:40
@jhlegarreta

Copy link
Copy Markdown
Contributor Author

Re #126 (comment) No need for further action. Tests are passing. Pinging @arokem.

@arokem

arokem commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

LGTM

@arokem
arokem merged commit f036b41 into tee-ar-ex:master Aug 21, 2026
19 checks passed
@jhlegarreta
jhlegarreta deleted the sty/use-pytest-temp-path-fixture branch August 21, 2026 20:31
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.

2 participants