@@ -74,14 +74,33 @@ fn default_config(input_crate: str) -> config {
74
74
}
75
75
}
76
76
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
+
77
89
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 > {
78
97
let args = vec:: tail ( args) ;
79
98
let opts = tuple:: first ( vec:: unzip ( opts ( ) ) ) ;
80
99
alt getopts:: getopts ( args, opts) {
81
100
result:: ok( match ) {
82
101
if vec:: len( match . free) == 1 u {
83
102
let input_crate = vec:: head( match . free) ;
84
- config_from_opts( input_crate, match )
103
+ config_from_opts ( input_crate, match , program_output )
85
104
} else if vec:: is_empty ( match . free) {
86
105
result:: err ( "no crates specified" )
87
106
} else {
@@ -96,7 +115,8 @@ fn parse_config(args: [str]) -> result::t<config, str> {
96
115
97
116
fn config_from_opts(
98
117
input_crate: str ,
99
- match: getopts:: match
118
+ match: getopts:: match,
119
+ program_output: program_output
100
120
) -> result:: t < config , str > {
101
121
102
122
let config = default_config( input_crate) ;
@@ -133,7 +153,8 @@ fn config_from_opts(
133
153
} ;
134
154
let result = result:: chain( result) { |config|
135
155
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) ;
137
158
result:: chain( pandoc_cmd) { |pandoc_cmd|
138
159
result:: ok( {
139
160
pandoc_cmd: pandoc_cmd
@@ -161,113 +182,172 @@ fn parse_output_style(output_style: str) -> result::t<output_style, str> {
161
182
}
162
183
163
184
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
166
188
) -> 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)
172
252
}
173
253
}
174
254
175
255
#[ test]
176
256
fn should_error_with_no_crates ( ) {
177
- let config = parse_config( [ "rustdoc" ] ) ;
257
+ let config = test :: parse_config ( [ "rustdoc" ] ) ;
178
258
assert result:: get_err ( config) == "no crates specified" ;
179
259
}
180
260
181
261
#[ test]
182
262
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" ] ) ;
184
264
assert result:: get_err ( config) == "multiple crates specified" ;
185
265
}
186
266
187
267
#[ test]
188
268
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" ] ) ;
190
270
assert result:: get ( config) . output_dir == "." ;
191
271
}
192
272
193
273
#[ test]
194
274
fn should_set_output_dir_if_provided ( ) {
195
- let config = parse_config( [
275
+ let config = test :: parse_config ( [
196
276
"rustdoc" , "crate.rc" , "--output-dir" , "snuggles"
197
277
] ) ;
198
278
assert result:: get ( config) . output_dir == "snuggles" ;
199
279
}
200
280
201
281
#[ test]
202
282
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" ] ) ;
204
284
assert result:: get ( config) . output_format == pandoc_html;
205
285
}
206
286
207
287
#[ test]
208
288
fn should_set_output_format_to_markdown_if_requested ( ) {
209
- let config = parse_config( [
289
+ let config = test :: parse_config ( [
210
290
"rustdoc" , "crate.rc" , "--output-format" , "markdown"
211
291
] ) ;
212
292
assert result:: get ( config) . output_format == markdown;
213
293
}
214
294
215
295
#[ test]
216
296
fn should_set_output_format_to_pandoc_html_if_requested ( ) {
217
- let config = parse_config( [
297
+ let config = test :: parse_config ( [
218
298
"rustdoc" , "crate.rc" , "--output-format" , "html"
219
299
] ) ;
220
300
assert result:: get ( config) . output_format == pandoc_html;
221
301
}
222
302
223
303
#[ test]
224
304
fn should_error_on_bogus_format ( ) {
225
- let config = parse_config( [
305
+ let config = test :: parse_config ( [
226
306
"rustdoc" , "crate.rc" , "--output-format" , "bogus"
227
307
] ) ;
228
308
assert result:: get_err ( config) == "unknown output format 'bogus'" ;
229
309
}
230
310
231
311
#[ test]
232
312
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" ] ) ;
234
314
assert result:: get ( config) . output_style == doc_per_mod;
235
315
}
236
316
237
317
#[ test]
238
318
fn should_set_output_style_to_one_doc_if_requested ( ) {
239
- let config = parse_config( [
319
+ let config = test :: parse_config ( [
240
320
"rustdoc" , "crate.rc" , "--output-style" , "doc-per-crate"
241
321
] ) ;
242
322
assert result:: get ( config) . output_style == doc_per_crate;
243
323
}
244
324
245
325
#[ test]
246
326
fn should_set_output_style_to_doc_per_mod_if_requested ( ) {
247
- let config = parse_config( [
327
+ let config = test :: parse_config ( [
248
328
"rustdoc" , "crate.rc" , "--output-style" , "doc-per-mod"
249
329
] ) ;
250
330
assert result:: get ( config) . output_style == doc_per_mod;
251
331
}
252
332
253
333
#[ test]
254
334
fn should_error_on_bogus_output_style ( ) {
255
- let config = parse_config( [
335
+ let config = test :: parse_config ( [
256
336
"rustdoc" , "crate.rc" , "--output-style" , "bogus"
257
337
] ) ;
258
338
assert result:: get_err ( config) == "unknown output style 'bogus'" ;
259
339
}
260
340
261
341
#[ test]
262
342
fn should_set_pandoc_command_if_requested ( ) {
263
- let config = parse_config( [
343
+ let config = test :: parse_config ( [
264
344
"rustdoc" , "crate.rc" , "--pandoc-cmd" , "panda-bear-doc"
265
345
] ) ;
266
346
assert result:: get ( config) . pandoc_cmd == some ( "panda-bear-doc" ) ;
267
347
}
268
348
269
349
#[ test]
270
350
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" ] ) ;
272
352
assert result:: get ( config) . pandoc_cmd == some ( "pandoc" ) ;
273
353
}
0 commit comments