]>
git.proxmox.com Git - rustc.git/blob - src/doc/book/tools/src/bin/lfp.rs
32b2ab297590c7e2248a59d017eab897513940aa
1 // We have some long regex literals, so:
2 // ignore-tidy-linelength
5 extern crate rustc_serialize
;
9 use std
::{path, fs, io}
;
13 let args
: Args
= Docopt
::new(USAGE
)
14 .and_then(|d
| d
.decode())
15 .unwrap_or_else(|e
| e
.exit());
17 let src_dir
= &path
::Path
::new(&args
.arg_src_dir
);
18 let found_errs
= walkdir
::WalkDir
::new(src_dir
)
25 eprintln
!("{:?}", err
);
26 std
::process
::exit(911)
31 let path
= entry
.path();
32 if is_file_of_interest(path
) {
33 let err_vec
= lint_file(path
);
36 LintingError
::LineOfInterest(line_num
, ref line
) =>
37 eprintln
!("{}:{}\t{}", path
.display(), line_num
, line
),
38 LintingError
::UnableToOpenFile
=>
39 eprintln
!("Unable to open {}.", path
.display()),
49 .any(|result
| *result
);
58 const USAGE
: &'
static str = "
64 -h --help Show this screen.
67 #[derive(Debug, RustcDecodable)]
72 fn lint_file(path
: &path
::Path
) -> Vec
<LintingError
> {
73 match fs
::File
::open(path
) {
74 Ok(file
) => lint_lines(io
::BufReader
::new(&file
).lines()),
75 Err(_
) => vec
![LintingError
::UnableToOpenFile
],
79 fn lint_lines
<I
>(lines
: I
) -> Vec
<LintingError
>
80 where I
: Iterator
<Item
=io
::Result
<String
>> {
83 .map(|(line_num
, line
)| {
84 let raw_line
= line
.unwrap();
85 if is_line_of_interest(&raw_line
) {
86 Err(LintingError
::LineOfInterest(line_num
, raw_line
))
91 .filter(|result
| result
.is_err())
92 .map(|result
| result
.unwrap_err())
96 fn is_file_of_interest(path
: &path
::Path
) -> bool
{
98 .map_or(false, |ext
| ext
== "md")
101 fn is_line_of_interest(line
: &str) -> bool
{
102 !line
.split_whitespace()
104 sub_string
.contains("file://") &&
105 !sub_string
.contains("file:///projects/")
114 LineOfInterest(usize, String
)
123 fn lint_file_returns_a_vec_with_errs_when_lines_of_interest_are_found() {
126 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
127 Running `target/guessing_game`
129 The secret number is: 61
130 Please input your guess.
134 Please input your guess.
138 Please input your guess.
140 Please input your guess.
145 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
146 Running `target/debug/guessing_game`
148 The secret number is: 7
149 Please input your guess.
153 Running `target/debug/guessing_game`
155 The secret number is: 83
156 Please input your guess.
159 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
160 Running `target/debug/guessing_game`
164 let raw_lines
= string
.to_string();
165 let lines
= raw_lines
.lines().map(|line
| {
169 let result_vec
= super::lint_lines(lines
);
171 assert
!(!result_vec
.is_empty());
172 assert_eq
!(3, result_vec
.len());
176 fn lint_file_returns_an_empty_vec_when_no_lines_of_interest_are_found() {
179 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
180 Running `target/guessing_game`
182 The secret number is: 61
183 Please input your guess.
187 Please input your guess.
191 Please input your guess.
193 Please input your guess.
199 let raw_lines
= string
.to_string();
200 let lines
= raw_lines
.lines().map(|line
| {
204 let result_vec
= super::lint_lines(lines
);
206 assert
!(result_vec
.is_empty());
210 fn is_file_of_interest_returns_false_when_the_path_is_a_directory() {
211 let uninteresting_fn
= "src/img";
213 assert
!(!super::is_file_of_interest(path
::Path
::new(uninteresting_fn
)));
217 fn is_file_of_interest_returns_false_when_the_filename_does_not_have_the_md_extension() {
218 let uninteresting_fn
= "src/img/foo1.png";
220 assert
!(!super::is_file_of_interest(path
::Path
::new(uninteresting_fn
)));
224 fn is_file_of_interest_returns_true_when_the_filename_has_the_md_extension() {
225 let interesting_fn
= "src/ch01-00-introduction.md";
227 assert
!(super::is_file_of_interest(path
::Path
::new(interesting_fn
)));
231 fn is_line_of_interest_does_not_report_a_line_if_the_line_contains_a_file_url_which_is_directly_followed_by_the_project_path() {
232 let sample_line
= "Compiling guessing_game v0.1.0 (file:///projects/guessing_game)";
234 assert
!(!super::is_line_of_interest(sample_line
));
238 fn is_line_of_interest_reports_a_line_if_the_line_contains_a_file_url_which_is_not_directly_followed_by_the_project_path() {
239 let sample_line
= "Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)";
241 assert
!(super::is_line_of_interest(sample_line
));