forked from pytest-dev/pytest-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_manage_py_scan.py
148 lines (115 loc) · 5.56 KB
/
test_manage_py_scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pytest
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found(django_testdir) -> None:
# XXX: Important: Do not chdir() to django_project_root since runpytest_subprocess
# will call "python /path/to/pytest.py", which will implicitly add cwd to
# the path. By instead calling "python /path/to/pytest.py
# django_project_root", we avoid implicitly adding the project to sys.path
# This matches the behaviour when pytest is called directly as an
# executable (cwd is not added to the Python path)
django_testdir.create_test_module(
"""
def test_foobar():
assert 1 + 1 == 2
"""
)
result = django_testdir.runpytest_subprocess("django_project_root")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_with_k(django_testdir, monkeypatch) -> None:
"""Test that cwd is checked as fallback with non-args via '-k foo'."""
testfile = django_testdir.create_test_module(
"""
def test_foobar():
assert True
""",
"sub/test_in_sub.py",
)
monkeypatch.chdir(testfile.dirname)
result = django_testdir.runpytest_subprocess("-k", "test_foobar")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_with_k_and_cwd(django_testdir, monkeypatch) -> None:
"""Cover cwd not used as fallback if present already in args."""
testfile = django_testdir.create_test_module(
"""
def test_foobar():
assert True
""",
"sub/test_in_sub.py",
)
monkeypatch.chdir(testfile.dirname)
result = django_testdir.runpytest_subprocess(testfile.dirname, "-k", "test_foobar")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_absolute(django_testdir, monkeypatch) -> None:
"""This only tests that "." is added as an absolute path (#637)."""
django_testdir.create_test_module(
"""
def test_dot_not_in_syspath():
import sys
assert '.' not in sys.path[:5]
"""
)
monkeypatch.chdir("django_project_root")
# NOTE: the "." here is important to test for an absolute path being used.
result = django_testdir.runpytest_subprocess("-s", ".")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_invalid_settings(django_testdir, monkeypatch) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST")
result = django_testdir.runpytest_subprocess("django_project_root")
assert result.ret != 0
result.stderr.fnmatch_lines(["*ImportError:*DOES_NOT_EXIST*"])
result.stderr.fnmatch_lines(["*pytest-django found a Django project*"])
def test_django_project_scan_disabled_invalid_settings(django_testdir, monkeypatch) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST")
django_testdir.makeini(
"""
[pytest]
django_find_project = false
"""
)
result = django_testdir.runpytest_subprocess("django_project_root")
assert result.ret != 0
result.stderr.fnmatch_lines(["*ImportError*DOES_NOT_EXIST*"])
result.stderr.fnmatch_lines(
["*pytest-django did not search for " "Django projects*"]
)
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_invalid_settings_version(django_testdir, monkeypatch) -> None:
"""Invalid DSM should not cause an error with --help or --version."""
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST")
result = django_testdir.runpytest_subprocess("django_project_root", "--version", "--version")
assert result.ret == 0
if result.errlines:
version_out = result.stderr
else:
# Pytest 7.0+
version_out = result.stdout
version_out.fnmatch_lines(["*This is pytest version*"])
result = django_testdir.runpytest_subprocess("django_project_root", "--help")
assert result.ret == 0
result.stdout.fnmatch_lines(["*usage:*"])
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_runs_without_error_on_long_args(django_testdir) -> None:
django_testdir.create_test_module(
"""
def test_this_is_a_long_message_which_caused_a_bug_when_scanning_for_manage_py_12346712341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234112341234112451234123412341234123412341234123412341234123412341234123412341234123412341234123412341234():
assert 1 + 1 == 2
""" # noqa: E501
)
result = django_testdir.runpytest_subprocess(
"-k",
"this_is_a_long_message_which_caused_a_bug_when_scanning_for_manage_py_12346712341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234112341234112451234123412341234123412341234123412341234123412341234123412341234123412341234123412341234", # noqa: E501
"django_project_root",
)
assert result.ret == 0