-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathtest_cli.py
More file actions
205 lines (164 loc) · 6.47 KB
/
test_cli.py
File metadata and controls
205 lines (164 loc) · 6.47 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
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
204
205
import logging
import os
from os.path import join
from click.testing import CliRunner
from sigal.__main__ import build, init, serve, set_meta
TESTGAL = join(os.path.abspath(os.path.dirname(__file__)), "sample")
def test_init(tmpdir):
config_file = str(tmpdir.join("sigal.conf.py"))
runner = CliRunner()
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
assert result.output.startswith("Sample config file created:")
assert os.path.isfile(config_file)
result = runner.invoke(init, [config_file])
assert result.exit_code == 1
assert (
result.output == "Found an existing config file, will abort to keep it safe.\n"
)
def test_build(tmpdir, disconnect_signals):
runner = CliRunner()
config_file = str(tmpdir.join("sigal.conf.py"))
tmpdir.mkdir("pictures")
tmpdir = str(tmpdir)
cwd = os.getcwd()
try:
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
os.symlink(join(TESTGAL, "watermark.png"), join(tmpdir, "watermark.png"))
os.symlink(
join(TESTGAL, "pictures", "dir2", "KeckObservatory20071020.jpg"),
join(tmpdir, "pictures", "KeckObservatory20071020.jpg"),
)
result = runner.invoke(build, ["-n", 1, "--debug", "--verbose"])
assert (
result.output
== "Only one option of debug, verbose and quiet should be used\n"
)
assert result.exit_code == 1
result = runner.invoke(build, ["-n", 1, "--verbose"])
assert result.output == "Settings file not found: sigal.conf.py\n"
assert result.exit_code == 1
os.chdir(tmpdir)
result = runner.invoke(build, ["foo", "-n", 1, "--quiet"])
assert result.exit_code == 1
assert "Input directory not found" in result.output
result = runner.invoke(build, ["pictures", "pictures/out", "-n", 1, "--debug"])
assert result.exit_code == 1
assert (
"Output directory should be outside of the input directory" in result.output
)
with open(config_file) as f:
text = f.read()
text += """
theme = 'colorbox'
files_to_copy = (('../watermark.png', 'watermark.png'),)
plugins = ['sigal.plugins.adjust', 'sigal.plugins.copyright',
'sigal.plugins.watermark', 'sigal.plugins.feeds',
'sigal.plugins.media_page', 'sigal.plugins.nomedia',
'sigal.plugins.extended_caching']
copyright = "An example copyright message"
copyright_text_font = "foobar"
watermark = "watermark.png"
watermark_position = "scale"
watermark_opacity = 0.3
rss_feed = {'feed_url': 'http://example.org/feed.rss', 'nb_items': 10}
atom_feed = {'feed_url': 'http://example.org/feed.atom', 'nb_items': 10}
"""
with open(config_file, "w") as f:
f.write(text)
result = runner.invoke(
build,
["pictures", "build", "--title", "Testing build", "-n", 1, "--debug"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert os.path.isfile(
join(tmpdir, "build", "thumbnails", "KeckObservatory20071020.jpg")
)
assert os.path.isfile(join(tmpdir, "build", "feed.atom"))
assert os.path.isfile(join(tmpdir, "build", "feed.rss"))
assert os.path.isfile(join(tmpdir, "build", "watermark.png"))
finally:
os.chdir(cwd)
# Reset logger
logger = logging.getLogger("sigal")
logger.handlers[:] = []
logger.setLevel(logging.INFO)
def test_build_only_album(tmpdir, disconnect_signals):
runner = CliRunner()
config_file = str(tmpdir.join("sigal.conf.py"))
tmpdir.mkdir("pictures")
tmpdir = str(tmpdir)
cwd = os.getcwd()
try:
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
os.symlink(
join(TESTGAL, "pictures", "dir2"),
join(tmpdir, "pictures", "dir1"),
)
os.chdir(tmpdir)
with open(config_file) as f:
text = f.read()
text += """
theme = 'colorbox'
plugins = ['sigal.plugins.media_page', 'sigal.plugins.nomedia',
'sigal.plugins.extended_caching']
"""
with open(config_file, "w") as f:
f.write(text)
result = runner.invoke(
build,
["pictures", "build", "--title", "Testing build", "-n", 1, "--debug", "--only-album", "dir1"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert os.path.isfile(
join(tmpdir, "build", "dir1", "index.html")
)
assert not os.path.isfile(
join(tmpdir, "build", "dir2", "index.html")
)
finally:
os.chdir(cwd)
# Reset logger
logger = logging.getLogger("sigal")
logger.handlers[:] = []
logger.setLevel(logging.INFO)
def test_serve(tmpdir):
config_file = str(tmpdir.join("sigal.conf.py"))
runner = CliRunner()
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
result = runner.invoke(serve)
assert result.exit_code == 2
assert result.output.startswith("The _build directory doesn't exist")
result = runner.invoke(serve, ["-c", config_file])
assert result.exit_code == 1
assert result.output.endswith("maybe try building first?\n")
def test_set_meta(tmpdir):
testdir = tmpdir.mkdir("test")
testfile = tmpdir.join("test.jpg")
testfile.write("")
runner = CliRunner()
result = runner.invoke(set_meta, [str(testdir), "title", "testing"])
assert result.exit_code == 0
assert result.output.startswith("1 metadata key(s) written to")
assert os.path.isfile(str(testdir.join("index.md")))
assert testdir.join("index.md").read() == "Title: testing\n"
# Run again, should give file exists error
result = runner.invoke(set_meta, [str(testdir), "title", "testing"])
assert result.exit_code == 2
result = runner.invoke(
set_meta, [str(testdir.join("non-existant.jpg")), "title", "testing"]
)
assert result.exit_code == 1
result = runner.invoke(set_meta, [str(testfile), "title", "testing"])
assert result.exit_code == 0
assert result.output.startswith("1 metadata key(s) written to")
assert os.path.isfile(str(tmpdir.join("test.md")))
assert tmpdir.join("test.md").read() == "Title: testing\n"
result = runner.invoke(set_meta, [str(testfile), "title"])
assert result.exit_code == 1
assert result.output.startswith("Need an even number of arguments")