Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: write a usage text for dir_helpers #2908

Merged
merged 5 commits into from
Dec 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion tests/dir_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
"""
The goal of this module is making dvc functional tests setup a breeze. This
includes a temporary dir, initializing git and dvc repos and bootstrapping some
file structure.

The cornerstone of these fixtures is `tmp_dir`, which creates a temporary dir
and changes path to it, it might be combined with `scm` and `dvc` to initialize
empty git and dvc repos. `tmp_dir` returns a Path instance, which should save
you from using `open()`, `os` and `os.path` utils many times:

(tmp_dir / "some_file").write_text("some text")
# ...
assert "some text" == (tmp_dir / "some_file").read_text()
assert (tmp_dir / "some_file").exists()

Additionally it provides `.gen()`, `.scm_gen()` and `.dvc_gen()` methods to
bootstrap a required file structure in a single call:

# Generate a dir with files
tmp_dir.gen({"dir": {"file": "file text", "second_file": "..."}})

# Generate a single file, dirs will be created along the way
tmp_dir.gen("dir/file", "file text")

# Generate + git add
tmp_dir.scm_gen({"file1": "...", ...})

# Generate + git add + git commit
tmp_dir.scm_gen({"file1": "...", ...}, commit="add files")

# Generate + dvc add
tmp_dir.dvc_gen({"file1": "...", ...})

# Generate + dvc add + git commit -am "..."
# This commits stages to git not the generated files.
tmp_dir.dvc_gen({"file1": "...", ...}, commit="add files")

Making it easier to bootstrap things has a supergoal of incentivizing a move
Suor marked this conversation as resolved.
Show resolved Hide resolved
from global repo template to creating everything inplace, which:
Suor marked this conversation as resolved.
Show resolved Hide resolved

- makes all path references local to test, enhancing readability
- allows using telling filenames, e.g. "git_tracked_file" instead of "foo"
- does not create unnecessary files
"""
from __future__ import unicode_literals

import os
Expand Down Expand Up @@ -44,7 +88,7 @@ def _require(self, name):
)

def gen(self, struct, text=""):
if isinstance(struct, basestring):
if isinstance(struct, (basestring, pathlib.PurePath)):
struct = {struct: text}

self._gen(struct)
Expand Down