-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_high_level_cli.py
203 lines (167 loc) · 6.42 KB
/
test_high_level_cli.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pytest
import subprocess
import sys
import os
import glob
from paignion.definitions import __version__
from paignion.tools import color_message
from paignion.exceptions import PaignionException
from paignion.__main__ import paignion_build
from argparse import Namespace
class TestHighLevelCLI:
def test_version(self):
res = subprocess.run(
[sys.executable, "-m", "paignion", "--version"],
stdout=subprocess.PIPE,
)
assert res.stdout.decode("utf-8")[:-1] == __version__
def test_no_arguments(self):
res = subprocess.run(
[sys.executable, "-m", "paignion"],
stdout=subprocess.PIPE,
)
# When called with no arguments, it should print help and exit gracefully
assert res.returncode == 0
def test_init_without_project_dir(self):
with open(os.devnull, "w") as stderr:
res = subprocess.run(
[sys.executable, "-m", "paignion", "init"],
stderr=stderr,
)
assert res.returncode == 2
def test_build_without_project_dir(self):
with open(os.devnull, "w") as stderr:
res = subprocess.run(
[sys.executable, "-m", "paignion", "build"],
stderr=stderr,
)
assert res.returncode == 2
def test_init_with_default_project(self):
res = subprocess.run(
[sys.executable, "-m", "paignion", "init", "tests/default_game"],
stdout=subprocess.PIPE,
)
# Make sure we got the right log message
assert res.stdout.decode("utf-8")[:-1] == color_message(
"[paignion] Initialized new Paignion game at " "`tests/default_game`",
color="yellow",
)
# Check that the project dir exists
assert os.path.exists("tests/default_game") and os.path.isdir(
"tests/default_game"
)
# Check that the rooms dir exists
assert os.path.exists("tests/default_game/rooms") and os.path.isdir(
"tests/default_game/rooms"
)
# Check that the room files exist
assert (
os.path.exists("tests/default_game/rooms/origin.md")
and os.path.isfile("tests/default_game/rooms/origin.md")
) and (
os.path.exists("tests/default_game/rooms/second_room.md")
and os.path.isfile("tests/default_game/rooms/second_room.md")
)
# Clean up
subprocess.run(["rm", "-r", "tests/default_game/"])
def test_build_with_default_project(self):
self.maxDiff = None
# Init project first
with open(os.devnull, "w") as d:
subprocess.run(
[
sys.executable,
"-m",
"paignion",
"init",
"tests/default_game",
],
stdout=d,
)
# Attempt to build it
res = subprocess.run(
[sys.executable, "-m", "paignion", "build", "tests/default_game"],
stdout=subprocess.PIPE,
)
# Make sure we got the right log message
log_messages = res.stdout.decode("utf-8")[:-1].split("\n")
assert log_messages[0] == color_message(
"[paignion] Building game `tests/default_game`", color="yellow"
)
assert log_messages[1] == color_message(
"[paignion] Done! Your game can be found at "
"`tests/default_game/build/index.html`",
color="yellow",
)
# Check that the project dir still exists
assert os.path.exists("tests/default_game") and os.path.isdir(
"tests/default_game"
)
# Check that the rooms dir still exists
assert os.path.exists("tests/default_game/rooms") and os.path.isdir(
"tests/default_game/rooms"
)
# Check that the room files still exist
assert (
os.path.exists("tests/default_game/rooms/origin.md")
and os.path.isfile("tests/default_game/rooms/origin.md")
) and (
os.path.exists("tests/default_game/rooms/second_room.md")
and os.path.isfile("tests/default_game/rooms/second_room.md")
)
# Check that the build dir was created
assert os.path.exists("tests/default_game/build") and os.path.isdir(
"tests/default_game/build"
)
# Check that the build dir only contains index.html
build_files = [f for f in glob.glob("tests/default_game/build/*")]
assert len(build_files) == 1
assert build_files[0] == "tests/default_game/build/index.html"
# Check the content of the game that was just built
with open("tests/default_game/build/index.html", "r") as f:
built_game = f.read()
with open("tests/test_data/default_game_index.html", "r") as f:
expected_game = f.read()
assert built_game == expected_game
# Clean up
subprocess.run(["rm", "-r", "tests/default_game/"])
def test_build_fail_no_rooms(self):
# Init project first
with open(os.devnull, "w") as d:
subprocess.run(
[
sys.executable,
"-m",
"paignion",
"init",
"tests/default_game",
],
stdout=d,
)
# Remove rooms directory
subprocess.run(["rm", "-r", "tests/default_game/rooms"])
# Try to build, should fail
with pytest.raises(PaignionException):
paignion_build(Namespace(project_dir="tests/default_game"))
# Clean up
subprocess.run(["rm", "-r", "tests/default_game/"])
def test_build_fail_no_origin_room(self):
# Init project first
with open(os.devnull, "w") as d:
subprocess.run(
[
sys.executable,
"-m",
"paignion",
"init",
"tests/default_game",
],
stdout=d,
)
# Remove the origin room
subprocess.run(["rm", "tests/default_game/rooms/origin.md"])
# Try to build, should fail
with pytest.raises(PaignionException):
paignion_build(Namespace(project_dir="tests/default_game"))
# Clean up
subprocess.run(["rm", "-r", "tests/default_game/"])