Skip to content

Commit

Permalink
Fix worker file handles being held during test runtime (#18)
Browse files Browse the repository at this point in the history
* Fix worker file handles being held during test runtime

`tempfile.mkstemp` is creating a file handle which is never closed. This doesn't matter on linux, but when running these tests on Windows machines, will cause an `OSError` when trying to remove the files.

This fixes the problem by using `mktemp` instead, which only returns a name, and then using `Path.touch()` to create the file but not keep an open handle.

* Fix calling os.remove inside an open() context

This also causes errors on Windows, and for good reason. De-denting these lines should fix the problem.
  • Loading branch information
Tom Thorogood authored Jul 7, 2020
1 parent 7bfcb53 commit 4072c81
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions webdriver_recorder/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import warnings
from string import ascii_uppercase
from typing import List, Any, Dict, Optional, Callable
from pathlib import Path

import jinja2
import pytest
Expand Down Expand Up @@ -122,7 +123,8 @@ def report_dir(report_generator, request) -> str:
# Ensures we don't generate reports while tests running (unless explicitly requested) using report_generator().
# The worker file is created at the beginning of the test session, and removed at the end (after the `yield` below).
# If multiple testing threads are running concurrently, a report will not be generated until the last one completes.
_, worker_file = tempfile.mkstemp(prefix='worker.', dir=tempdir)
worker_file = tempfile.mktemp(prefix='worker.', dir=tempdir)
Path(worker_file).touch()
yield tempdir
os.remove(worker_file)
workers = (f for f in os.listdir(tempdir) if f.startswith('worker.'))
Expand Down Expand Up @@ -241,5 +243,5 @@ def iterfiles(dir, prefix):
filename = os.path.join(dir, filename)
with open(filename) as fd:
data = json.load(fd)
os.remove(filename)
yield data
yield data
os.remove(filename)

0 comments on commit 4072c81

Please sign in to comment.