|
2 | 2 |
|
3 | 3 | # `pytest` Plugin |
4 | 4 |
|
5 | | -With libvcs's pytest plugin for [pytest], you can easily create Git, SVN, and Mercurial repositories on the fly. |
| 5 | +:::{doc-pytest-plugin} libvcs.pytest_plugin |
| 6 | +:project: libvcs |
| 7 | +:package: libvcs |
| 8 | +:summary: libvcs ships a pytest plugin for creating isolated Git, Mercurial, and Subversion repositories during tests. |
| 9 | +:tests-url: https://github.com/vcs-python/libvcs/tree/master/tests |
6 | 10 |
|
7 | | -```{seealso} Are you using libvcs? |
| 11 | +Use these fixtures when your tests need disposable repositories, config files, |
| 12 | +and home-directory setup without repeating bootstrap code in every suite. |
8 | 13 |
|
9 | | -Looking for more flexibility, correctness, or power? Need different defaults? [Connect with us] on GitHub. We'd love to hear about your use case—APIs won't be stabilized until we're confident everything meets expectations. |
| 14 | +## Recommended fixtures |
10 | 15 |
|
11 | | -[connect with us]: https://github.com/vcs-python/libvcs/discussions |
12 | | -``` |
13 | | - |
14 | | -[pytest]: https://docs.pytest.org/ |
15 | | - |
16 | | -## Usage |
17 | | - |
18 | | -Install `libvcs` using your preferred Python package manager: |
19 | | - |
20 | | -```console |
21 | | -$ pip install libvcs |
22 | | -``` |
23 | | - |
24 | | -Pytest will automatically detect the plugin, and its fixtures will be available. |
25 | | - |
26 | | -## Fixtures |
27 | | - |
28 | | -This pytest plugin works by providing [pytest fixtures](https://docs.pytest.org/en/stable/how-to/fixtures.html). The plugin's fixtures ensure that a fresh Git, Subversion, or Mercurial repository is available for each test. It utilizes [session-scoped fixtures] to cache initial repositories, improving performance across tests. |
29 | | - |
30 | | -[session-scoped fixtures]: https://docs.pytest.org/en/8.3.x/how-to/fixtures.html#fixture-scopes |
31 | | - |
32 | | -(recommended-fixtures)= |
33 | | - |
34 | | -## Recommended Fixtures |
35 | | - |
36 | | -When the plugin is enabled and `pytest` is run, these overridable fixtures are automatically used: |
| 16 | +These fixtures are the usual starting point when enabling the plugin: |
37 | 17 |
|
38 | | -- Create temporary test directories for: |
39 | | - - `/home/` ({func}`home_path`) |
40 | | - - `/home/${user}` ({func}`user_path`) |
41 | | -- Set the home directory: |
42 | | - - Patch `$HOME` to point to {func}`user_path` using ({func}`set_home`) |
43 | | -- Create configuration files: |
44 | | - - `.gitconfig` via {func}`gitconfig` |
45 | | - - `.hgrc` via {func}`hgconfig` |
46 | | -- Set default VCS configurations: |
47 | | - - Use {func}`hgconfig` for [`HGRCPATH`] via {func}`set_hgconfig` |
48 | | - - Use {func}`gitconfig` for [`GIT_CONFIG`] via {func}`set_gitconfig` |
49 | | -- Set default commit names and emails: |
50 | | - - Name: {func}`vcs_name` |
51 | | - - Email: {func}`vcs_email` |
52 | | - - User (e.g. _`user <email@tld>`_): {func}`vcs_user` |
53 | | - - For git only: {func}`git_commit_envvars` |
| 18 | +- {fixture}`set_home` patches `$HOME` to point at {fixture}`user_path`. |
| 19 | +- {fixture}`set_gitconfig` and {fixture}`set_hgconfig` apply stable VCS |
| 20 | + configuration. |
| 21 | +- {fixture}`vcs_name`, {fixture}`vcs_email`, and {fixture}`vcs_user` let you |
| 22 | + override commit identity defaults. |
| 23 | +- {fixture}`git_commit_envvars` helps when Git ignores `GIT_CONFIG` in a |
| 24 | + subprocess-heavy test. |
54 | 25 |
|
55 | | -These ensure that repositories can be cloned and created without unnecessary warnings. |
| 26 | +## Bootstrapping in `conftest.py` |
56 | 27 |
|
57 | | -[`HGRCPATH`]: https://www.mercurial-scm.org/doc/hg.1.html#:~:text=UNIX%2Dlike%20environments.-,HGRCPATH,-If%20not%20set |
58 | | -[`GIT_CONFIG`]: https://git-scm.com/docs/git-config#Documentation/git-config.txt-GITCONFIG |
59 | | - |
60 | | -## Bootstrapping pytest in `conftest.py` |
61 | | - |
62 | | -To configure the above fixtures with `autouse=True`, add them to your `conftest.py` file or test file, depending on the desired scope. |
63 | | - |
64 | | -_Why aren't these fixtures added automatically by the plugin?_ This design choice promotes explicitness, adhering to best practices for pytest plugins and Python packages. |
65 | | - |
66 | | -### Setting a Temporary Home Directory |
67 | | - |
68 | | -To set a temporary home directory, use the {func}`set_home` fixture with `autouse=True`: |
| 28 | +Keep autouse setup explicit in your own `conftest.py` instead of having the |
| 29 | +plugin force global side effects. |
69 | 30 |
|
70 | 31 | ```python |
71 | 32 | import pytest |
72 | 33 |
|
73 | | -@pytest.fixture(autouse=True) |
74 | | -def setup(set_home: None): |
75 | | - pass |
76 | | -``` |
77 | | - |
78 | | -### VCS Configuration |
79 | | - |
80 | | -#### Git |
81 | | - |
82 | | -You can override the default author used in {func}`git_remote_repo` and other |
83 | | -fixtures via {func}`vcs_name`, {func}`vcs_email`, and {func}`vcs_user`: |
84 | | - |
85 | | -``` |
86 | | -@pytest.fixture(scope="session") |
87 | | -def vcs_name() -> str: |
88 | | - return "My custom name" |
89 | | -``` |
90 | | - |
91 | | -Use the {func}`set_gitconfig` fixture with `autouse=True`: |
92 | | - |
93 | | -```python |
94 | | -import pytest |
95 | 34 |
|
96 | 35 | @pytest.fixture(autouse=True) |
97 | | -def setup(set_gitconfig: None): |
| 36 | +def setup( |
| 37 | + set_home: None, |
| 38 | + set_gitconfig: None, |
| 39 | + set_hgconfig: None, |
| 40 | +) -> None: |
98 | 41 | pass |
99 | 42 | ``` |
100 | | - |
101 | | -Sometimes, `set_getconfig` via `GIT_CONFIG` doesn't apply as expected. For those |
102 | | -cases, you can use {func}`git_commit_envvars`: |
103 | | - |
104 | | -```python |
105 | | -import pytest |
106 | | - |
107 | | -@pytest.fixture |
108 | | -def my_git_repo( |
109 | | - create_git_remote_repo: CreateRepoPytestFixtureFn, |
110 | | - gitconfig: pathlib.Path, |
111 | | - git_commit_envvars: "_ENV", |
112 | | -) -> pathlib.Path: |
113 | | - """Copy the session-scoped Git repository to a temporary directory.""" |
114 | | - repo_path = create_git_remote_repo() |
115 | | - git_remote_repo_single_commit_post_init( |
116 | | - remote_repo_path=repo_path, |
117 | | - env=git_commit_envvars, |
118 | | - ) |
119 | | - return repo_path |
120 | | -``` |
121 | | - |
122 | | -#### Mercurial |
123 | | - |
124 | | -Use the {func}`set_hgconfig` fixture with `autouse=True`: |
125 | | - |
126 | | -```python |
127 | | -import pytest |
128 | | - |
129 | | -@pytest.fixture(autouse=True) |
130 | | -def setup(set_hgconfig: None): |
131 | | - pass |
132 | | -``` |
133 | | - |
134 | | -## Examples |
135 | | - |
136 | | -For usage examples, refer to libvcs's own [tests/](https://github.com/vcs-python/libvcs/tree/master/tests). |
137 | | - |
138 | | -## API Reference |
139 | | - |
140 | | -```{eval-rst} |
141 | | -.. automodule:: libvcs.pytest_plugin |
142 | | - :members: |
143 | | - :inherited-members: |
144 | | - :private-members: |
145 | | - :show-inheritance: |
146 | | - :member-order: bysource |
147 | | -``` |
| 43 | +::: |
0 commit comments