镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Stop doctests polluting current dir with temporary files #264

Open

Description

@pp-mo

At present, when you run any doctests temporary netCDF files are created in the current directory.

This is slightly tiresome, and could be avoided by dumping files to a temporary directory, like pytest does.

Unfortunately, adding specific code to do that in every module / docs-sourcefile could also be tiresome and intrusive.

in run-doctests

Possibly, a way to control this could be added to the run_doctests tool ?

  • e.g., run by default in a temporary cwd -
  • but that could affect doctest execution, and make some things difficult (e.g. importing local code) ??

using tempfile

instead of e.g. data.to_nc4('tmp.nc'),

import pathlib
import testfile

tempdir = pathlib.Path(testfile.TemporaryDirectory())
# ...
path = tempdir / "tmp.nc"
to_nc4(data, path)
result = from_nc4(path)

with a test utility

Or.. A utility to create a temp directory that will be consistently destroyed afterwards might be useful
(as opposed to tempdir which does not arrange for destruction).

# in <testutils.py>
import pathlib

class TempDir(pathlib.Path):
    def __init__(self):
        # self.context is deleted when self is deleted --> destroys directory
        self.context = tempfile.TemporaryDirectory()
        self.path_str = self.context.__enter__()
        self.path = pathlib.Path(self.dirpath_str)

# a single common per-import object
tempdir = TempDir()

# ... 
# in docstrings / docs
# Usage is generally shorter, and directory is removed on program exit
>>> from testutils import tempdir

...

>>> filepath = tempdir.path / "tmp.nc"
>>> to_nc4(data, filepath)
>>> result = from_nc4(filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions