Skip to content

Commit dc80eb5

Browse files
committed
---
yaml --- r: 14676 b: refs/heads/try c: dea19b2 h: refs/heads/master v: v3
1 parent 5568b68 commit dc80eb5

File tree

2 files changed

+105
-25
lines changed

2 files changed

+105
-25
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: b8c8e43381b2b5814ca4965d2e883f78a771f262
5+
refs/heads/try: dea19b25c90fe7aeb0408214eb1165d0d0081433
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/rustdoc/config.rs

Lines changed: 104 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,33 @@ fn default_config(input_crate: str) -> config {
7474
}
7575
}
7676

77+
type program_output = fn~(str, [str]) -> {status: int, out: str, err: str};
78+
79+
fn mock_program_output(_prog: str, _args: [str]) -> {
80+
status: int, out: str, err: str
81+
} {
82+
{
83+
status: 0,
84+
out: "",
85+
err: ""
86+
}
87+
}
88+
7789
fn parse_config(args: [str]) -> result::t<config, str> {
90+
parse_config_(args, std::run::program_output)
91+
}
92+
93+
fn parse_config_(
94+
args: [str],
95+
program_output: program_output
96+
) -> result::t<config, str> {
7897
let args = vec::tail(args);
7998
let opts = tuple::first(vec::unzip(opts()));
8099
alt getopts::getopts(args, opts) {
81100
result::ok(match) {
82101
if vec::len(match.free) == 1u {
83102
let input_crate = vec::head(match.free);
84-
config_from_opts(input_crate, match)
103+
config_from_opts(input_crate, match, program_output)
85104
} else if vec::is_empty(match.free) {
86105
result::err("no crates specified")
87106
} else {
@@ -96,7 +115,8 @@ fn parse_config(args: [str]) -> result::t<config, str> {
96115

97116
fn config_from_opts(
98117
input_crate: str,
99-
match: getopts::match
118+
match: getopts::match,
119+
program_output: program_output
100120
) -> result::t<config, str> {
101121

102122
let config = default_config(input_crate);
@@ -133,7 +153,8 @@ fn config_from_opts(
133153
};
134154
let result = result::chain(result) {|config|
135155
let pandoc_cmd = getopts::opt_maybe_str(match, opt_pandoc_cmd());
136-
let pandoc_cmd = maybe_find_pandoc(config, pandoc_cmd);
156+
let pandoc_cmd = maybe_find_pandoc(
157+
config, pandoc_cmd, program_output);
137158
result::chain(pandoc_cmd) {|pandoc_cmd|
138159
result::ok({
139160
pandoc_cmd: pandoc_cmd
@@ -161,113 +182,172 @@ fn parse_output_style(output_style: str) -> result::t<output_style, str> {
161182
}
162183

163184
fn maybe_find_pandoc(
164-
_config: config,
165-
maybe_pandoc_cmd: option<str>
185+
config: config,
186+
maybe_pandoc_cmd: option<str>,
187+
program_output: program_output
166188
) -> result::t<option<str>, str> {
167-
// FIXME: When we actually need pandoc then look for it first
168-
// on the path, then in cabal; test to make sure pandoc works
169-
alt maybe_pandoc_cmd {
170-
some(pandoc_cmd) { result::ok(some(pandoc_cmd)) }
171-
none { result::ok(some("pandoc")) }
189+
if config.output_format != pandoc_html {
190+
ret result::ok(maybe_pandoc_cmd);
191+
}
192+
193+
let possible_pandocs = alt maybe_pandoc_cmd {
194+
some(pandoc_cmd) { [pandoc_cmd] }
195+
none {
196+
// FIXME (1936): Need to be able to expand ~ to look in .cabal
197+
["pandoc"]
198+
}
199+
};
200+
201+
let pandoc = vec::find(possible_pandocs) {|pandoc|
202+
let output = program_output(pandoc, ["--version"]);
203+
#debug("testing pandoc cmd %s: %?", pandoc, output);
204+
output.status == 0
205+
};
206+
207+
if option::is_some(pandoc) {
208+
result::ok(pandoc)
209+
} else {
210+
result::err("couldn't find pandoc")
211+
}
212+
}
213+
214+
#[test]
215+
fn should_find_pandoc() {
216+
let config = {
217+
output_format: pandoc_html
218+
with default_config("test")
219+
};
220+
let mock_program_output = fn~(_prog: str, _args: [str]) -> {
221+
status: int, out: str, err: str
222+
} {
223+
{
224+
status: 0, out: "pandoc 1.8.2.1", err: ""
225+
}
226+
};
227+
let result = maybe_find_pandoc(config, none, mock_program_output);
228+
assert result == result::ok(some("pandoc"));
229+
}
230+
231+
#[test]
232+
fn should_error_with_no_pandoc() {
233+
let config = {
234+
output_format: pandoc_html
235+
with default_config("test")
236+
};
237+
let mock_program_output = fn~(_prog: str, _args: [str]) -> {
238+
status: int, out: str, err: str
239+
} {
240+
{
241+
status: 1, out: "", err: ""
242+
}
243+
};
244+
let result = maybe_find_pandoc(config, none, mock_program_output);
245+
assert result == result::err("couldn't find pandoc");
246+
}
247+
248+
#[cfg(test)]
249+
mod test {
250+
fn parse_config(args: [str]) -> result::t<config, str> {
251+
parse_config_(args, mock_program_output)
172252
}
173253
}
174254

175255
#[test]
176256
fn should_error_with_no_crates() {
177-
let config = parse_config(["rustdoc"]);
257+
let config = test::parse_config(["rustdoc"]);
178258
assert result::get_err(config) == "no crates specified";
179259
}
180260

181261
#[test]
182262
fn should_error_with_multiple_crates() {
183-
let config = parse_config(["rustdoc", "crate1.rc", "crate2.rc"]);
263+
let config = test::parse_config(["rustdoc", "crate1.rc", "crate2.rc"]);
184264
assert result::get_err(config) == "multiple crates specified";
185265
}
186266

187267
#[test]
188268
fn should_set_output_dir_to_cwd_if_not_provided() {
189-
let config = parse_config(["rustdoc", "crate.rc"]);
269+
let config = test::parse_config(["rustdoc", "crate.rc"]);
190270
assert result::get(config).output_dir == ".";
191271
}
192272

193273
#[test]
194274
fn should_set_output_dir_if_provided() {
195-
let config = parse_config([
275+
let config = test::parse_config([
196276
"rustdoc", "crate.rc", "--output-dir", "snuggles"
197277
]);
198278
assert result::get(config).output_dir == "snuggles";
199279
}
200280

201281
#[test]
202282
fn should_set_output_format_to_pandoc_html_if_not_provided() {
203-
let config = parse_config(["rustdoc", "crate.rc"]);
283+
let config = test::parse_config(["rustdoc", "crate.rc"]);
204284
assert result::get(config).output_format == pandoc_html;
205285
}
206286

207287
#[test]
208288
fn should_set_output_format_to_markdown_if_requested() {
209-
let config = parse_config([
289+
let config = test::parse_config([
210290
"rustdoc", "crate.rc", "--output-format", "markdown"
211291
]);
212292
assert result::get(config).output_format == markdown;
213293
}
214294

215295
#[test]
216296
fn should_set_output_format_to_pandoc_html_if_requested() {
217-
let config = parse_config([
297+
let config = test::parse_config([
218298
"rustdoc", "crate.rc", "--output-format", "html"
219299
]);
220300
assert result::get(config).output_format == pandoc_html;
221301
}
222302

223303
#[test]
224304
fn should_error_on_bogus_format() {
225-
let config = parse_config([
305+
let config = test::parse_config([
226306
"rustdoc", "crate.rc", "--output-format", "bogus"
227307
]);
228308
assert result::get_err(config) == "unknown output format 'bogus'";
229309
}
230310

231311
#[test]
232312
fn should_set_output_style_to_doc_per_mod_by_default() {
233-
let config = parse_config(["rustdoc", "crate.rc"]);
313+
let config = test::parse_config(["rustdoc", "crate.rc"]);
234314
assert result::get(config).output_style == doc_per_mod;
235315
}
236316

237317
#[test]
238318
fn should_set_output_style_to_one_doc_if_requested() {
239-
let config = parse_config([
319+
let config = test::parse_config([
240320
"rustdoc", "crate.rc", "--output-style", "doc-per-crate"
241321
]);
242322
assert result::get(config).output_style == doc_per_crate;
243323
}
244324

245325
#[test]
246326
fn should_set_output_style_to_doc_per_mod_if_requested() {
247-
let config = parse_config([
327+
let config = test::parse_config([
248328
"rustdoc", "crate.rc", "--output-style", "doc-per-mod"
249329
]);
250330
assert result::get(config).output_style == doc_per_mod;
251331
}
252332

253333
#[test]
254334
fn should_error_on_bogus_output_style() {
255-
let config = parse_config([
335+
let config = test::parse_config([
256336
"rustdoc", "crate.rc", "--output-style", "bogus"
257337
]);
258338
assert result::get_err(config) == "unknown output style 'bogus'";
259339
}
260340

261341
#[test]
262342
fn should_set_pandoc_command_if_requested() {
263-
let config = parse_config([
343+
let config = test::parse_config([
264344
"rustdoc", "crate.rc", "--pandoc-cmd", "panda-bear-doc"
265345
]);
266346
assert result::get(config).pandoc_cmd == some("panda-bear-doc");
267347
}
268348

269349
#[test]
270350
fn should_set_pandoc_command_when_using_pandoc() {
271-
let config = parse_config(["rustdoc", "crate.rc"]);
351+
let config = test::parse_config(["rustdoc", "crate.rc"]);
272352
assert result::get(config).pandoc_cmd == some("pandoc");
273353
}

0 commit comments

Comments
 (0)