Skip to content

Enhancement suggestion: Directive to change working directory #264

Closed

Description

@peterjc

I am suggesting adding support for another directive, something like .. doctest-cd:: ../tests/example/ which would triggering the insert of a snippet like import os; os.chdir(new_path) into the docstring where the new path is absolute and pre-computed by interpreting the requested path relative to the RST filename and location.

Brief comparison to Biopython's home-grown mechanism for extracting doctests from RST files

For background, I see pytest-doctestplus dates back about 13 years to the needs of the astropy project. Going back slightly earlier to 14 years ago, for Biopython I started something conceptually similar for running doctests extracted from our documentation (first commit). We currently use a unittest based framework, with the a single script handling this.

Up until Biopython 1.83 this was based on LaTeX https://github.com/biopython/biopython/blob/biopython-183/Tests/test_Tutorial.py with special comment lines preceding code blocks to be tested as doctests.

As of Biopython 1.84 this is based on RST (which is rendered with Sphinx), again done special comments (so that Sphinx ignores our directives) interpreted as marking .. code:: pycon blocks to be tested: https://github.com/biopython/biopython/blob/biopython-184/Tests/test_Tutorial.py

This is very specific to our needs, so switching to a more widely used and maintained project like pytest-doctestplus is potentially attractive.

Our current solution enabled the following:

  • pycon blocks are either ignored and not tested (default), become a new self-contained doctest, or are appended to the current doctest (allowing for text part way through an example)
  • declare python dependencies (and the test is skipped if not met)
    • This is handled here by .. doctest-requires:: asdf (and is more flexible)
  • declare internet access is required (and the test is skipped otherwise)
    • This is handled here by .. doctest-remote-data::
  • declare a relative working directory (to allow shorter paths in the examples, for example using a test sub-directory specific to a topic)
    • This does not appear to be possible in pytest-doctestplus yet.

The scoping choice looks difficult to change here, but the final point about specifying a working directory for the tests looks like it could be added.

Activity

  1. pllim commented on Sep 19, 2024

    @pllim
    Contributor

    @peterjc , thank you for your interest!

    I really appreciate your detailed explanation above but I am not sure if I completely understand the need for specifying path. The way doctestplus works (if I remember correctly), any rendered API or user doc with >>> prefix would be considered for doctest. It does not matter where that file is as long as Sphinx is collecting it. So there is no need to change directory. Can you please point me to one specific doctest requiring this? Is it the commented code at the beginning of test_Tutorial.py?

  2. peterjc commented on Sep 19, 2024

    @peterjc
    Author

    For the how: My implementation inserts the path change into the beginning of the docstring here (bad past-Peter using single letter variable names):

    https://github.com/biopython/biopython/blob/biopython-184/Tests/test_Tutorial.py#L235

    For the why: Most of our sample data files files are under {repo}/Tests/ in assorted subfolders, some are under the {repo}/Docs/examples/, but rarely do we want to expose the potentially long path to the user in an example snippet of code.

    Here we specifically run a doctest under {repo}/Docs/examples/ allowing the example to parse a file with a local filename "ls_orchid.fasta" (no path component needed):

    https://github.com/biopython/biopython/blob/biopython-184/Doc/Tutorial/chapter_seqio.rst?plain=1#L395
    https://github.com/biopython/biopython/blob/biopython-184/Doc/examples/ls_orchid.fasta

    While here run another doctest under {repo}/Tests/Quality/ instead, again so that the example can just use a local filename, in this case "example.fastq":

    https://github.com/biopython/biopython/blob/biopython-184/Doc/Tutorial/chapter_seqio.rst?plain=1#L1664
    https://github.com/biopython/biopython/blob/biopython-184/Tests/Quality/example.fastq

  3. pllim commented on Sep 19, 2024

    @pllim
    Contributor

    I have a counter proposal, I think what you need is one of the following possible alternatives:

    1. Have a helper function like astropy.utils.data.get_pkg_data_filename that allows you to access a data file already registered with your package without hardcoding long paths.
    2. Use testsetup directive provided by Sphinx to hide the long path to your file. What I usually do is read the actual file in testsetup and then have a dummy code with pretty filename with +skip that gets rendered. The subsequent code use the file object that user thinks is from pretty name but actually was created from testsetup.

    Would any of these alternatives work for you?

  4. peterjc commented on Sep 19, 2024

    @peterjc
    Author
    1. We don't have anything like get_pkg_data_filename in Biopython, and adding something like that seems quite an ask.
    2. That might work. Or we might be able to handle changing the directory in the testsetup directive, or perhaps more cleanly mocking this with a symlink (and removing it in the tear-down).

    So I think we can justify turning this suggestion down. But thank you!

  5. bsipocz commented on Sep 19, 2024

    @bsipocz
    Member

    I would second Pey Lian's suggestions for workaround solutions. The testsetup directive is rather convenient.

    It's rather unlikely that any of the current maintainers will implement this feature request, but I would like to be explicit on the fact that if you're willing to implement and PR a new directive, it would be more than welcome.

  6. pllim commented on Sep 19, 2024

    @pllim
    Contributor

    I also haven't thought about any potential security implication of a feature that would allow changing into arbitrary directories.

  7. pllim commented on Sep 19, 2024

    @pllim
    Contributor

    quite an ask

    Granted the code in astropy is a bit too much to stare at, but if you don't need remote data server support or caching, I think it is not that many lines of code. The logic for path is mainly in https://github.com/astropy/astropy/blob/d09ba92c28a0e22c27e7dfcdd2ed45d3bb7fa80d/astropy/utils/data.py#L989 and you can totally ignore if package == "astropy.utils.iers" block there.

    Anyway, if you are interested to pursue this, I will be happy to consult. Good luck!

  8. peterjc commented on Sep 19, 2024

    @peterjc
    Author

    I felt the security issues were moot (given the risks in running the example code), as that can do this anyway:

    >>> import os
    >>> os.chdir("/any/path/you/like/")
  9. bsipocz commented on Sep 19, 2024

    @bsipocz
    Member

    I don't see any added security issues either, the directive would be a shortcut to hide existing, albeit ugly directory changes. (Although I wonder if the correct version of the library code would be picked up locally for this case, e.g. when one runs pytest in the source directory without installing it.)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions