split main function and test function into their own files
small modifications to make executable and test compile properly
This commit is contained in:
parent
e6e7ce178f
commit
62e583d4cf
42
src/bin/rustfmt.rs
Normal file
42
src/bin/rustfmt.rs
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(exit_status)]
|
||||
|
||||
extern crate rustfmt;
|
||||
|
||||
use rustfmt::{WriteMode, run};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
//run(args, WriteMode::Display);
|
||||
run(args, WriteMode::Overwrite);
|
||||
std::env::set_exit_status(0);
|
||||
|
||||
// TODO unit tests
|
||||
// let fmt = ListFormatting {
|
||||
// tactic: ListTactic::Horizontal,
|
||||
// separator: ",",
|
||||
// trailing_separator: SeparatorTactic::Vertical,
|
||||
// indent: 2,
|
||||
// h_width: 80,
|
||||
// v_width: 100,
|
||||
// };
|
||||
// let inputs = vec![(format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new())];
|
||||
// let s = write_list(&inputs, &fmt);
|
||||
// println!(" {}", s);
|
||||
}
|
99
src/lib.rs
99
src/lib.rs
@ -14,8 +14,6 @@
|
||||
#![feature(collections)]
|
||||
#![feature(str_char)]
|
||||
|
||||
#![cfg_attr(not(test), feature(exit_status))]
|
||||
|
||||
// TODO we're going to allocate a whole bunch of temp Strings, is it worth
|
||||
// keeping some scratch mem for this and running our own StrPool?
|
||||
// TODO for lint violations of names, emit a refactor script
|
||||
@ -244,102 +242,7 @@ impl<'a> CompilerCalls<'a> for RustFmtCalls {
|
||||
}
|
||||
}
|
||||
|
||||
fn run(args: Vec<String>, write_mode: WriteMode) {
|
||||
pub fn run(args: Vec<String>, write_mode: WriteMode) {
|
||||
let mut call_ctxt = RustFmtCalls { input_path: None, write_mode: write_mode };
|
||||
rustc_driver::run_compiler(&args, &mut call_ctxt);
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn main() {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
//run(args, WriteMode::Display);
|
||||
run(args, WriteMode::Overwrite);
|
||||
std::env::set_exit_status(0);
|
||||
|
||||
// TODO unit tests
|
||||
// let fmt = ListFormatting {
|
||||
// tactic: ListTactic::Horizontal,
|
||||
// separator: ",",
|
||||
// trailing_separator: SeparatorTactic::Vertical,
|
||||
// indent: 2,
|
||||
// h_width: 80,
|
||||
// v_width: 100,
|
||||
// };
|
||||
// let inputs = vec![(format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new()),
|
||||
// (format!("foo"), String::new())];
|
||||
// let s = write_list(&inputs, &fmt);
|
||||
// println!(" {}", s);
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::sync::atomic;
|
||||
use super::*;
|
||||
use super::run;
|
||||
|
||||
// For now, the only supported regression tests are idempotent tests - the input and
|
||||
// output must match exactly.
|
||||
// FIXME(#28) would be good to check for error messages and fail on them, or at least report.
|
||||
#[test]
|
||||
fn idempotent_tests() {
|
||||
println!("Idempotent tests:");
|
||||
FAILURES.store(0, atomic::Ordering::Relaxed);
|
||||
|
||||
// Get all files in the tests/idem directory
|
||||
let files = fs::read_dir("tests/idem").unwrap();
|
||||
// For each file, run rustfmt and collect the output
|
||||
let mut count = 0;
|
||||
for entry in files {
|
||||
let path = entry.unwrap().path();
|
||||
let file_name = path.to_str().unwrap();
|
||||
println!("Testing '{}'...", file_name);
|
||||
run(vec!["rustfmt".to_owned(), file_name.to_owned()], WriteMode::Return(HANDLE_RESULT));
|
||||
count += 1;
|
||||
}
|
||||
// And also dogfood ourselves!
|
||||
println!("Testing 'src/main.rs'...");
|
||||
run(vec!["rustfmt".to_string(), "src/main.rs".to_string()],
|
||||
WriteMode::Return(HANDLE_RESULT));
|
||||
count += 1;
|
||||
|
||||
// Display results
|
||||
let fails = FAILURES.load(atomic::Ordering::Relaxed);
|
||||
println!("Ran {} idempotent tests; {} failures.", count, fails);
|
||||
assert!(fails == 0, "{} idempotent tests failed", fails);
|
||||
}
|
||||
|
||||
// 'global' used by sys_tests and handle_result.
|
||||
static FAILURES: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
|
||||
// Ick, just needed to get a &'static to handle_result.
|
||||
static HANDLE_RESULT: &'static Fn(HashMap<String, String>) = &handle_result;
|
||||
|
||||
// Compare output to input.
|
||||
fn handle_result(result: HashMap<String, String>) {
|
||||
let mut fails = 0;
|
||||
|
||||
for file_name in result.keys() {
|
||||
let mut f = fs::File::open(file_name).unwrap();
|
||||
let mut text = String::new();
|
||||
f.read_to_string(&mut text).unwrap();
|
||||
if result[file_name] != text {
|
||||
fails += 1;
|
||||
println!("Mismatch in {}.", file_name);
|
||||
println!("{}", result[file_name]);
|
||||
}
|
||||
}
|
||||
|
||||
if fails > 0 {
|
||||
FAILURES.fetch_add(1, atomic::Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
73
tests/idem.rs
Normal file
73
tests/idem.rs
Normal file
@ -0,0 +1,73 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
extern crate rustfmt;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::sync::atomic;
|
||||
use rustfmt::*;
|
||||
|
||||
// For now, the only supported regression tests are idempotent tests - the input and
|
||||
// output must match exactly.
|
||||
// FIXME(#28) would be good to check for error messages and fail on them, or at least report.
|
||||
#[test]
|
||||
fn idempotent_tests() {
|
||||
println!("Idempotent tests:");
|
||||
FAILURES.store(0, atomic::Ordering::Relaxed);
|
||||
|
||||
// Get all files in the tests/idem directory
|
||||
let files = fs::read_dir("tests/idem").unwrap();
|
||||
// For each file, run rustfmt and collect the output
|
||||
let mut count = 0;
|
||||
for entry in files {
|
||||
let path = entry.unwrap().path();
|
||||
let file_name = path.to_str().unwrap();
|
||||
println!("Testing '{}'...", file_name);
|
||||
run(vec!["rustfmt".to_owned(), file_name.to_owned()], WriteMode::Return(HANDLE_RESULT));
|
||||
count += 1;
|
||||
}
|
||||
// And also dogfood ourselves!
|
||||
println!("Testing 'src/lib.rs'...");
|
||||
run(vec!["rustfmt".to_string(), "src/lib.rs".to_string()],
|
||||
WriteMode::Return(HANDLE_RESULT));
|
||||
count += 1;
|
||||
|
||||
// Display results
|
||||
let fails = FAILURES.load(atomic::Ordering::Relaxed);
|
||||
println!("Ran {} idempotent tests; {} failures.", count, fails);
|
||||
assert!(fails == 0, "{} idempotent tests failed", fails);
|
||||
}
|
||||
|
||||
// 'global' used by sys_tests and handle_result.
|
||||
static FAILURES: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
|
||||
// Ick, just needed to get a &'static to handle_result.
|
||||
static HANDLE_RESULT: &'static Fn(HashMap<String, String>) = &handle_result;
|
||||
|
||||
// Compare output to input.
|
||||
fn handle_result(result: HashMap<String, String>) {
|
||||
let mut fails = 0;
|
||||
|
||||
for file_name in result.keys() {
|
||||
let mut f = fs::File::open(file_name).unwrap();
|
||||
let mut text = String::new();
|
||||
f.read_to_string(&mut text).unwrap();
|
||||
if result[file_name] != text {
|
||||
fails += 1;
|
||||
println!("Mismatch in {}.", file_name);
|
||||
println!("{}", result[file_name]);
|
||||
}
|
||||
}
|
||||
|
||||
if fails > 0 {
|
||||
FAILURES.fetch_add(1, atomic::Ordering::Relaxed);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user