forked from danger/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
163 lines (130 loc) · 5.01 KB
/
Copy pathtest_cli.py
File metadata and controls
163 lines (130 loc) · 5.01 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import json
import pytest
from click.testing import CliRunner
from danger_python.cli import cli
DANGER_PR_ARGUMENTS = (
"pr https://github.com/microsoft/TypeScript/pull/34806 "
"--use-github-checks -p danger-python -u"
)
@pytest.mark.parametrize("danger_js_path", ["/usr/bin/danger-js"])
@pytest.mark.parametrize("danger_js_output", ["danger-js output"])
@pytest.mark.parametrize("danger_js_arguments", [DANGER_PR_ARGUMENTS])
@pytest.mark.usefixtures("run_subprocess")
def test_pr_command_invokes_danger_js_passing_arguments():
"""
Test that pr command invokes danger_js passing correct arguments.
"""
runner = CliRunner()
arguments = [
"pr",
"https://github.com/microsoft/TypeScript/pull/34806",
"--use-github-checks",
]
result = runner.invoke(cli, arguments)
assert result.exit_code == 0
assert result.output == "danger-js output\n"
DANGER_LOCAL_ARGUMENTS = "local -i fake_danger_id -t -p danger-python -u"
@pytest.mark.parametrize("danger_js_path", ["/usr/bin/js-danger"])
@pytest.mark.parametrize("danger_js_output", ["parsed local output"])
@pytest.mark.parametrize("danger_js_arguments", [DANGER_LOCAL_ARGUMENTS])
@pytest.mark.usefixtures("run_subprocess")
def test_local_command_invokes_danger_js_passing_arguments():
"""
Test that local command invokes danger_js passing correct arguments.
"""
runner = CliRunner()
arguments = ["local", "-i", "fake_danger_id", "-t"]
result = runner.invoke(cli, arguments)
assert result.exit_code == 0
assert result.output == "parsed local output\n"
DANGER_CI_ARGUMENTS = "ci -v --no-publish-check -p danger-python -u"
@pytest.mark.parametrize("danger_js_path", ["/usr/bin/very-danger"])
@pytest.mark.parametrize("danger_js_arguments", [DANGER_CI_ARGUMENTS])
@pytest.mark.parametrize("danger_js_output", ["The output!"])
@pytest.mark.usefixtures("run_subprocess")
def test_ci_command_invokes_danger_js_passing_arguments():
"""
Test that ci command invokes danger_js passing correct arguments.
"""
runner = CliRunner()
arguments = ["ci", "-v", "--no-publish-check"]
result = runner.invoke(cli, arguments)
assert result.exit_code == 0
assert result.output == "The output!\n"
@pytest.mark.parametrize(
"dangerfile", ['print("Hello world")\n' 'print("Goodbye world")']
)
@pytest.mark.usefixtures("danger")
def test_run_command_invokes_dangerfile():
"""
Test that run command invokes dangerfile.py contents.
"""
runner = CliRunner()
result = runner.invoke(cli, ["run"])
assert result.exit_code == 0
assert result.output.startswith("Hello world\nGoodbye world\n")
@pytest.mark.parametrize("dangerfile", ["This is not a valid syntax of Python"])
@pytest.mark.usefixtures("danger")
def test_run_command_shows_traceback_when_dangerfile_fails():
"""
Test that run command shows traceback when dangerfile.py fails.
"""
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, ["run"])
expected_error = (
"There was an error when executing dangerfile.py:\n"
"SyntaxError at line 1: invalid syntax\n\n"
"Stacktrace:\n"
)
assert result.exit_code == -1
assert result.stderr.startswith(expected_error)
@pytest.mark.parametrize("dangerfile", ['print("Default command")'])
@pytest.mark.usefixtures("danger")
def test_default_command_invokes_dangerfile():
"""
Test that default command invokes dangerfily.py contents.
"""
runner = CliRunner()
result = runner.invoke(cli)
assert result.exit_code == 0
assert result.output.startswith("Default command\n")
@pytest.mark.parametrize("modified_files", [["a.py", "b.py"]])
@pytest.mark.parametrize("dangerfile", ["print(danger.git.modified_files)"])
@pytest.mark.usefixtures("danger")
def test_executing_dangerfile_passes_danger_instance_to_the_script():
"""
Test that executing run command passes danger instance with parsed input
to the Dangerfile locals.
"""
runner = CliRunner()
result = runner.invoke(cli, ["run"])
assert result.exit_code == 0
assert result.output.startswith("['a.py', 'b.py']\n")
@pytest.mark.parametrize(
"dangerfile",
[
'warn("This is the final warning")\n'
'fail("You are going to fail")\n'
'message("Hello world")\n'
'markdown("Test markdown")\n'
],
)
@pytest.mark.usefixtures("danger")
def test_executing_dangerfile_prints_results_to_stdout():
"""
Test that executing run command reports results back to output.
"""
runner = CliRunner()
result = runner.invoke(cli, ["run"])
expected_json = {
"fails": [{"message": "You are going to fail"}],
"warnings": [{"message": "This is the final warning"}],
"messages": [{"message": "Hello world"}],
"markdowns": [{"message": "Test markdown"}],
"meta": {
"runtimeHref": "https://danger.systems/python",
"runtimeName": "danger-python",
},
}
assert result.exit_code == 0
assert result.output == json.dumps(expected_json) + "\n"