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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
extern crate pulldown_cmark as cmark;
extern crate tempdir;

use std::env;
use std::fs::File;
use std::io::Error as IoError;
use std::io::{Read, Write};
use std::path::{PathBuf, Path};
use cmark::{Parser, Event, Tag};

pub fn generate_doc_tests(docs: &[&str]) {
    // This shortcut is specifically so examples in skeptic's on
    // readme can call this function in non-build.rs contexts, without
    // panicking below.
    if docs.is_empty() {
        return;
    }

    let out_dir = env::var("OUT_DIR").unwrap();
    let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

    let mut out_file = PathBuf::from(out_dir.clone());
    out_file.push("skeptic-tests.rs");

    let config = Config {
        out_dir: PathBuf::from(out_dir),
        root_dir: PathBuf::from(cargo_manifest_dir),
        out_file: out_file,
        docs: docs.iter().map(|s| s.to_string()).collect(),
    };

    run(config);
}

struct Config {
    out_dir: PathBuf,
    root_dir: PathBuf,
    out_file: PathBuf,
    docs: Vec<String>,
}

fn run(ref config: Config) {
    let tests = extract_tests(config).unwrap();
    emit_tests(config, tests).unwrap();
}

struct Test {
    name: String,
    text: Vec<String>,
    ignore: bool,
    no_run: bool,
    should_panic: bool,
}

struct DocTestSuite {
    doc_tests: Vec<DocTest>,
}

struct DocTest {
    template: Option<String>,
    tests: Vec<Test>,
}

fn extract_tests(config: &Config) -> Result<DocTestSuite, IoError> {
    let mut doc_tests = Vec::new();
    for doc in &config.docs {
        let ref mut path = config.root_dir.clone();
        path.push(doc);
        let new_tests = try!(extract_tests_from_file(path));
        doc_tests.push(new_tests);
    }
    return Ok(DocTestSuite { doc_tests: doc_tests });
}

fn extract_tests_from_file(path: &Path) -> Result<DocTest, IoError> {
    let mut tests = Vec::new();
    let mut template = None;

    let mut file = try!(File::open(path));
    let ref mut s = String::new();
    try!(file.read_to_string(s));
    let parser = Parser::new(s);

    let mut test_name_gen = TestNameGen::new(path);
    let mut code_buffer = None;

    for event in parser {
        match event {
            Event::Start(Tag::CodeBlock(ref info)) => {
                let code_block_info = parse_code_block_info(info);
                if code_block_info.is_rust {
                    code_buffer = Some(Vec::new());
                }
            }
            Event::Text(text) => {
                if let Some(ref mut buf) = code_buffer {
                    buf.push(text.to_string());
                }
            }
            Event::End(Tag::CodeBlock(ref info)) => {
                let code_block_info = parse_code_block_info(info);
                if let Some(buf) = code_buffer.take() {
                    if code_block_info.is_template {
                        template = Some(join_strings(buf))
                    } else {
                        tests.push(Test {
                            name: test_name_gen.advance(),
                            text: buf,
                            ignore: code_block_info.ignore,
                            no_run: code_block_info.no_run,
                            should_panic: code_block_info.should_panic,
                        });
                    }
                }
            }
            _ => (),
        }
    }

    Ok(DocTest {
        template: template,
        tests: tests,
    })
}

fn join_strings(ss: Vec<String>) -> String {
    let mut s_ = String::new();
    for s in ss {
        s_.push_str(&s)
    }

    s_
}

struct TestNameGen {
    root: String,
    count: i32,
}

impl TestNameGen {
    fn new(path: &Path) -> TestNameGen {
        let ref file_stem = path.file_stem().unwrap().to_str().unwrap().to_string();
        TestNameGen {
            root: sanitize_test_name(file_stem),
            count: 0,
        }
    }

    fn advance(&mut self) -> String {
        let count = self.count;
        self.count += 1;
        format!("{}_{}", self.root, count)
    }
}

fn sanitize_test_name(s: &str) -> String {
    to_lowercase(s)
        .chars()
        .map(|c| {
            if c.is_alphanumeric() {
                c
            } else {
                '_'
            }
        })
        .collect()
}

// Only converting test names to lowercase to avoid style lints
// against test functions.
fn to_lowercase(s: &str) -> String {
    use std::ascii::AsciiExt;
    // FIXME: unicode
    s.to_ascii_lowercase()
}

fn parse_code_block_info(info: &str) -> CodeBlockInfo {
    // Same as rustdoc
    let tokens = info.split(|c: char| !(c == '_' || c == '-' || c.is_alphanumeric()));

    let mut seen_rust_tags = false;
    let mut seen_other_tags = false;
    let mut info = CodeBlockInfo {
        is_rust: false,
        should_panic: false,
        ignore: false,
        no_run: false,
        is_template: false,
    };

    for token in tokens {
        match token {
            "" => {}
            "rust" => {
                info.is_rust = true;
                seen_rust_tags = true
            }
            "should_panic" => {
                info.should_panic = true;
                seen_rust_tags = true
            }
            "ignore" => {
                info.ignore = true;
                seen_rust_tags = true
            }
            "no_run" => {
                info.no_run = true;
                seen_rust_tags = true;
            }
            "skeptic-template" => {
                info.is_template = true;
                seen_rust_tags = true
            }
            _ => seen_other_tags = true,
        }
    }

    info.is_rust &= !seen_other_tags || seen_rust_tags;

    info
}

struct CodeBlockInfo {
    is_rust: bool,
    should_panic: bool,
    ignore: bool,
    no_run: bool,
    is_template: bool,
}

fn emit_tests(config: &Config, suite: DocTestSuite) -> Result<(), IoError> {
    let mut file = try!(File::create(&config.out_file));

    // Test cases use the api from skeptic::rt
    try!(writeln!(file, "extern crate skeptic;\n"));

    for doc_test in suite.doc_tests {
        for test in &doc_test.tests {
            let test_string = try!(create_test_string(config, &doc_test.template, test));
            try!(writeln!(file, "{}", test_string));
        }
    }

    Ok(())
}

fn create_test_string(config: &Config,
                      template: &Option<String>,
                      test: &Test)
                      -> Result<String, IoError> {

    let template = template.clone().unwrap_or_else(|| String::from("{}"));
    let test_text = test.text.iter().fold(String::new(), |a, b| format!("{}{}", a, b));

    let mut s: Vec<u8> = Vec::new();
    if test.ignore {
        try!(writeln!(s, "#[ignore]"));
    }
    if test.should_panic {
        try!(writeln!(s, "#[should_panic]"));
    }

    try!(writeln!(s, "#[test] fn {}() {{", test.name));
    try!(writeln!(s,
                  "    let ref s = format!(\"{}\", r####\"{}\"####);",
                  template,
                  test_text));

    // if we are not running, just compile the test without running it
    if test.no_run {
        try!(writeln!(s,
            "    skeptic::rt::compile_test(r#\"{}\"#, s);",
            config.out_dir.to_str().unwrap()));
    } else {
        try!(writeln!(s,
            "    skeptic::rt::run_test(r#\"{}\"#, s);",
            config.out_dir.to_str().unwrap()));
    }

    try!(writeln!(s, "}}"));
    try!(writeln!(s, ""));

    Ok(String::from_utf8(s).unwrap())
}

pub mod rt {
    use std::env;
    use std::fs::File;
    use std::io::{self, Write};
    use std::path::{Path, PathBuf};
    use std::process::{Command, Output};
    use tempdir::TempDir;

    pub fn compile_test(out_dir: &str, test_text: &str) {
        let ref rustc = env::var("RUSTC").unwrap_or(String::from("rustc"));
        let ref outdir = TempDir::new("rust-skeptic").unwrap();
        let ref testcase_path = outdir.path().join("test.rs");
        let ref binary_path = outdir.path().join("out.exe");

        write_test_case(testcase_path, test_text);
        compile_test_case(testcase_path, binary_path, rustc, out_dir);
    }

    pub fn run_test(out_dir: &str, test_text: &str) {
        let ref rustc = env::var("RUSTC").unwrap_or(String::from("rustc"));
        let ref outdir = TempDir::new("rust-skeptic").unwrap();
        let ref testcase_path = outdir.path().join("test.rs");
        let ref binary_path = outdir.path().join("out.exe");

        write_test_case(testcase_path, test_text);
        compile_test_case(testcase_path, binary_path, rustc, out_dir);
        run_test_case(binary_path);
    }

    fn write_test_case(path: &Path, test_text: &str) {
        let mut file = File::create(path).unwrap();
        file.write_all(test_text.as_bytes()).unwrap();
    }

    fn compile_test_case(in_path: &Path, out_path: &Path, rustc: &str, out_dir: &str) {

        // FIXME: Hack. Because the test runner uses rustc to build
        // tests and those tests expect access to the crate this
        // project builds and its deps, we need to find the directory
        // containing Cargo's deps to pass as a `-L` flag to
        // rustc. Cargo does not give us this directly, but we know
        // relative to OUT_DIR where to look.
        let mut target_dir = PathBuf::from(out_dir);
        target_dir.pop();
        target_dir.pop();
        target_dir.pop();
        let mut deps_dir = target_dir.clone();
        deps_dir.push("deps");

        interpret_output(Command::new(rustc)
                             .arg(in_path)
                             .arg("-o")
                             .arg(out_path)
                             .arg("--crate-type=bin")
                             .arg("-L")
                             .arg(target_dir)
                             .arg("-L")
                             .arg(deps_dir)
                             .output()
                             .unwrap());
    }
    fn run_test_case(out_path: &Path) {
        interpret_output(Command::new(out_path)
                             .output()
                             .unwrap());
    }

    fn interpret_output(output: Output) {
        write!(io::stdout(),
               "{}",
               String::from_utf8(output.stdout).unwrap())
            .unwrap();
        write!(io::stderr(),
               "{}",
               String::from_utf8(output.stderr).unwrap())
            .unwrap();
        if !output.status.success() {
            panic!("command failed");
        }
    }
}