Debian Codegraph @

hypothesis-python/tests/cover/test_filestorage.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import os
12 import subprocess
13 import sys
14 import textwrap
15
16 import pytest
17
18 from hypothesis import configuration as fs
19
20 from tests.common.utils import skipif_emscripten
21
22 previous_home_dir = None
23
24
25 def setup_function(function):
26 global previous_home_dir
27 previous_home_dir = fs.storage_directory().path
28 fs.set_hypothesis_home_dir(None)
29
30
31 def teardown_function(function):
32 global previous_home_dir
33 fs.set_hypothesis_home_dir(previous_home_dir)
34 previous_home_dir = None
35
36
37 def test_defaults_to_the_default():
38 assert fs.storage_directory().path == fs.__hypothesis_home_directory_default
39
40
41 def test_can_set_homedir(tmp_path):
42 fs.set_hypothesis_home_dir(tmp_path)
43 assert fs.storage_directory("kittens").path == tmp_path / "kittens"
44
45
46 def test_will_pick_up_location_from_env(monkeypatch, tmp_path):
47 monkeypatch.setattr(os, "environ", {"HYPOTHESIS_STORAGE_DIRECTORY": str(tmp_path)})
48 assert fs.storage_directory().path == tmp_path
49
50
51 def test_storage_directories_are_not_created_automatically(tmp_path):
52 fs.set_hypothesis_home_dir(tmp_path)
53 assert not fs.storage_directory("badgers").path.exists()
54
55
56 def _gitignore_storage_dir_script(*, home_dir=None):
57 return textwrap.dedent(f"""
58 from hypothesis import given, settings, strategies as st
59 from hypothesis.configuration import set_hypothesis_home_dir
60
61 home_dir = {repr(str(home_dir)) if home_dir is not None else None}
62 if home_dir:
63 set_hypothesis_home_dir(home_dir)
64
65 # in CI we use the "ci" profile which sets database=None. But we actually want
66 # disk writes to happen here so we can test the .hypothesis/.gitignore behavior.
67 settings.load_profile("default")
68
69 @given(st.integers())
70 def f(n):
71 # fail, in order to guarantee we write a file to .hypothesis/examples
72 raise ValueError()
73
74 try:
75 f()
76 except Exception:
77 pass
78 """)
79
80
81 @skipif_emscripten
82 @pytest.mark.parametrize("set_home_dir", [False, True])
83 def test_writes_gitignore_to_new_storage_dir(tmp_path, set_home_dir):
84 subprocess.check_call(["git", "init", str(tmp_path)])
85
86 home_dir = tmp_path / ("custom_storage_dir" if set_home_dir else ".hypothesis")
87 (tmp_path / "test_a.py").write_text(
88 _gitignore_storage_dir_script(home_dir=home_dir if set_home_dir else None),
89 encoding="utf-8",
90 )
91
92 subprocess.check_call([sys.executable, "test_a.py"], cwd=tmp_path)
93 assert home_dir.is_dir()
94 assert (home_dir / ".gitignore").exists()
95
96 status = subprocess.check_output(
97 ["git", "status", "--porcelain"], cwd=tmp_path, encoding="utf-8"
98 )
99 assert home_dir.name not in status
100
101
102 @skipif_emscripten
103 @pytest.mark.parametrize("set_home_dir", [False, True])
104 def test_skips_gitignore_for_existing_storage_dir(tmp_path, set_home_dir):
105 home_dir = tmp_path / ("custom_storage_dir" if set_home_dir else ".hypothesis")
106 home_dir.mkdir()
107
108 (tmp_path / "test_a.py").write_text(
109 _gitignore_storage_dir_script(home_dir=home_dir if set_home_dir else None),
110 encoding="utf-8",
111 )
112
113 subprocess.check_call([sys.executable, "test_a.py"], cwd=tmp_path)
114 assert not (home_dir / ".gitignore").exists()