This repository was archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Refactor tests to use explicit fixtures #44
Merged
+273
−900
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c649f6e
No crate tests no more (for now)
killercup 926fc5d
Refactor tests to use explicit fixtures
killercup f1ce4fb
Ooops, rustc exits with 0 when lint is not error!
killercup 9353527
Add record mode for quickly getting JSON output
killercup 2396ea5
Add test for needless_borrow lint
killercup 7dc1352
Yeah yeah, let's actually generate new tests
killercup 2e2e26a
Don't be too clever
killercup 16a25ba
Test some typeck suggestions
killercup 6fab747
How about we install clippy on CI?
killercup 4ca7457
Build scripts? Not actually that clever
killercup 2ff9e32
Rename fixtures do original dash-case
killercup 3c61332
Add some more rustc test cases
killercup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "tests/fixtures/libui-rs"] | ||
path = tests/crates/libui-rs | ||
url = https://github.com/pcwalton/libui-rs.git | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule libui-rs
deleted from
13299d
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#[macro_use] extern crate duct; | ||
#[macro_use] extern crate pretty_assertions; | ||
extern crate tempdir; | ||
#[macro_use] extern crate log; | ||
extern crate env_logger; | ||
extern crate serde_json; | ||
extern crate rustfix; | ||
|
||
use std::fs; | ||
use std::error::Error; | ||
use std::path::{Path, PathBuf}; | ||
use std::collections::HashSet; | ||
use tempdir::TempDir; | ||
|
||
use rustfix::Replacement; | ||
|
||
fn compile_and_get_json_errors(file: &Path) -> Result<String, Box<Error>> { | ||
let tmp = TempDir::new("rustfix-tests")?; | ||
let better_call_clippy = cmd!( | ||
"clippy-driver", "rustc", file, | ||
"--error-format=json", "--emit=metadata", | ||
"--out-dir", tmp.path() | ||
); | ||
let res = better_call_clippy | ||
.stdout_capture() | ||
.stderr_capture() | ||
.unchecked() | ||
.run()?; | ||
let stderr = String::from_utf8(res.stderr)?; | ||
|
||
use std::io::{Error, ErrorKind}; | ||
match res.status.code() { | ||
Some(0) | Some(1) => Ok(stderr), | ||
_ => Err(Box::new(Error::new( | ||
ErrorKind::Other, | ||
format!("failed with status {:?}: {}", res.status.code(), stderr), | ||
))) | ||
} | ||
} | ||
|
||
fn read_file(path: &Path) -> Result<String, Box<Error>> { | ||
use std::io::Read; | ||
|
||
let mut buffer = String::new(); | ||
let mut file = fs::File::open(path)?; | ||
file.read_to_string(&mut buffer)?; | ||
Ok(buffer) | ||
} | ||
|
||
fn apply_suggestion(file_content: &mut String, suggestion: &Replacement) -> Result<String, Box<Error>> { | ||
use std::cmp::max; | ||
|
||
let mut new_content = String::new(); | ||
|
||
// Add the lines before the section we want to replace | ||
new_content.push_str(&file_content.lines() | ||
.take(max(suggestion.snippet.line_range.start.line - 1, 0) as usize) | ||
.collect::<Vec<_>>() | ||
.join("\n")); | ||
new_content.push_str("\n"); | ||
|
||
// Parts of line before replacement | ||
new_content.push_str(&file_content.lines() | ||
.nth(suggestion.snippet.line_range.start.line - 1) | ||
.unwrap_or("") | ||
.chars() | ||
.take(suggestion.snippet.line_range.start.column - 1) | ||
.collect::<String>()); | ||
|
||
// Insert new content! Finally! | ||
new_content.push_str(&suggestion.replacement); | ||
|
||
// Parts of line after replacement | ||
new_content.push_str(&file_content.lines() | ||
.nth(suggestion.snippet.line_range.end.line - 1) | ||
.unwrap_or("") | ||
.chars() | ||
.skip(suggestion.snippet.line_range.end.column - 1) | ||
.collect::<String>()); | ||
|
||
// Add the lines after the section we want to replace | ||
new_content.push_str("\n"); | ||
new_content.push_str(&file_content.lines() | ||
.skip(suggestion.snippet.line_range.end.line as usize) | ||
.collect::<Vec<_>>() | ||
.join("\n")); | ||
new_content.push_str("\n"); | ||
|
||
Ok(new_content) | ||
} | ||
|
||
fn test_rustfix_with_file<P: AsRef<Path>>(file: P) -> Result<(), Box<Error>> { | ||
let file: &Path = file.as_ref(); | ||
debug!("{:?}", file); | ||
let code = read_file(&file)?; | ||
let errors = compile_and_get_json_errors(file)?; | ||
|
||
if std::env::var("RUSTFIX_TEST_RECORD_JSON").is_ok() { | ||
use std::io::Write; | ||
let mut recorded_json = fs::File::create(&file.with_extension("recorded.json"))?; | ||
recorded_json.write_all(errors.as_bytes())?; | ||
} | ||
|
||
let expected_json = read_file(&file.with_extension("json"))?; | ||
|
||
assert_eq!( | ||
errors.trim(), | ||
expected_json.trim(), | ||
"got unexpected json from clippy" | ||
); | ||
|
||
let suggestions = rustfix::get_suggestions_from_json(&errors, &HashSet::new()); | ||
let mut fixed = code.clone(); | ||
|
||
for sug in suggestions { | ||
trace!("{:?}", sug); | ||
for sol in sug.solutions { | ||
trace!("{:?}", sol); | ||
for r in sol.replacements { | ||
info!("replaced."); | ||
trace!("{:?}", r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useful for debugging: |
||
fixed = apply_suggestion(&mut fixed, &r)?; | ||
} | ||
} | ||
} | ||
|
||
let expected_fixed = read_file(&file.with_extension("fixed.rs"))?; | ||
assert_eq!(fixed.trim(), expected_fixed.trim(), "file doesn't look fixed"); | ||
Ok(()) | ||
} | ||
|
||
fn get_fixture_files() -> Result<Vec<PathBuf>, Box<Error>> { | ||
Ok(fs::read_dir("./tests/fixtures")? | ||
.into_iter() | ||
.map(|e| e.unwrap().path()) | ||
.filter(|p| p.is_file()) | ||
.filter(|p| { | ||
let x = p.to_string_lossy(); | ||
x.ends_with(".rs") && !x.ends_with(".fixed.rs") | ||
}) | ||
.collect()) | ||
} | ||
|
||
#[test] | ||
fn fixtures() { | ||
let _ = env_logger::try_init(); | ||
|
||
for file in &get_fixture_files().unwrap() { | ||
test_rustfix_with_file(file).unwrap(); | ||
info!("passed: {:?}", file); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.recorded.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
trait Foo {} | ||
|
||
struct Bar<'a> { | ||
w: &'a (Foo + Copy), | ||
} | ||
|
||
fn main() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"message":"expected a path on the left-hand side of `+`, not `&'a Foo`","code":{"code":"E0178","explanation":"\nIn types, the `+` type operator has low precedence, so it is often necessary\nto use parentheses.\n\nFor example:\n\n```compile_fail,E0178\ntrait Foo {}\n\nstruct Bar<'a> {\n w: &'a Foo + Copy, // error, use &'a (Foo + Copy)\n x: &'a Foo + 'a, // error, use &'a (Foo + 'a)\n y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)\n z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)\n}\n```\n\nMore details can be found in [RFC 438].\n\n[RFC 438]: https://github.com/rust-lang/rfcs/pull/438\n"},"level":"error","spans":[{"file_name":"./tests/fixtures/E0178.rs","byte_start":38,"byte_end":52,"line_start":4,"line_end":4,"column_start":8,"column_end":22,"is_primary":true,"text":[{"text":" w: &'a Foo + Copy,","highlight_start":8,"highlight_end":22}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"try adding parentheses","code":null,"level":"help","spans":[{"file_name":"./tests/fixtures/E0178.rs","byte_start":38,"byte_end":52,"line_start":4,"line_end":4,"column_start":8,"column_end":22,"is_primary":true,"text":[{"text":" w: &'a Foo + Copy,","highlight_start":8,"highlight_end":22}],"label":null,"suggested_replacement":"&'a (Foo + Copy)","expansion":null}],"children":[],"rendered":null}],"rendered":"error[E0178]: expected a path on the left-hand side of `+`, not `&'a Foo`\n --> ./tests/fixtures/E0178.rs:4:8\n |\n4 | w: &'a Foo + Copy,\n | ^^^^^^^^^^^^^^ help: try adding parentheses: `&'a (Foo + Copy)`\n\n"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
trait Foo {} | ||
|
||
struct Bar<'a> { | ||
w: &'a Foo + Copy, | ||
} | ||
|
||
fn main() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2017 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. | ||
|
||
// Point at the captured immutable outer variable | ||
|
||
fn foo(mut f: Box<FnMut()>) { | ||
f(); | ||
} | ||
|
||
fn main() { | ||
let mut y = true; | ||
foo(Box::new(move || y = false) as Box<_>); //~ ERROR cannot assign to captured outer variable | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"message":"cannot assign to captured outer variable in an `FnMut` closure","code":{"code":"E0594","explanation":null},"level":"error","spans":[{"file_name":"./tests/fixtures/closure-immutable-outer-variable.rs","byte_start":615,"byte_end":624,"line_start":19,"line_end":19,"column_start":26,"column_end":35,"is_primary":true,"text":[{"text":" foo(Box::new(move || y = false) as Box<_>); //~ ERROR cannot assign to captured outer variable","highlight_start":26,"highlight_end":35}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"consider making `y` mutable","code":null,"level":"help","spans":[{"file_name":"./tests/fixtures/closure-immutable-outer-variable.rs","byte_start":580,"byte_end":581,"line_start":18,"line_end":18,"column_start":9,"column_end":10,"is_primary":true,"text":[{"text":" let y = true;","highlight_start":9,"highlight_end":10}],"label":null,"suggested_replacement":"mut y","expansion":null}],"children":[],"rendered":null}],"rendered":"error[E0594]: cannot assign to captured outer variable in an `FnMut` closure\n --> ./tests/fixtures/closure-immutable-outer-variable.rs:19:26\n |\n18 | let y = true;\n | - help: consider making `y` mutable: `mut y`\n19 | foo(Box::new(move || y = false) as Box<_>); //~ ERROR cannot assign to captured outer variable\n | ^^^^^^^^^\n\n"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2017 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. | ||
|
||
// Point at the captured immutable outer variable | ||
|
||
fn foo(mut f: Box<FnMut()>) { | ||
f(); | ||
} | ||
|
||
fn main() { | ||
let y = true; | ||
foo(Box::new(move || y = false) as Box<_>); //~ ERROR cannot assign to captured outer variable | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
let _x: &i32 = &&&&&5; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"message":"this expression borrows a reference that is immediately dereferenced by the compiler","code":{"code":"needless_borrow","explanation":null},"level":"warning","spans":[{"file_name":"./tests/fixtures/needless_borrow.rs","byte_start":31,"byte_end":38,"line_start":2,"line_end":2,"column_start":20,"column_end":27,"is_primary":true,"text":[{"text":" let _x: &i32 = &&&&&&5;","highlight_start":20,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"#[warn(needless_borrow)] on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.177/index.html#needless_borrow","code":null,"level":"help","spans":[],"children":[],"rendered":null},{"message":"change this to","code":null,"level":"help","spans":[{"file_name":"./tests/fixtures/needless_borrow.rs","byte_start":31,"byte_end":38,"line_start":2,"line_end":2,"column_start":20,"column_end":27,"is_primary":true,"text":[{"text":" let _x: &i32 = &&&&&&5;","highlight_start":20,"highlight_end":27}],"label":null,"suggested_replacement":"&&&&&5","expansion":null}],"children":[],"rendered":null}],"rendered":"warning: this expression borrows a reference that is immediately dereferenced by the compiler\n --> ./tests/fixtures/needless_borrow.rs:2:20\n |\n2 | let _x: &i32 = &&&&&&5;\n | ^^^^^^^ help: change this to: `&&&&&5`\n |\n = note: #[warn(needless_borrow)] on by default\n = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.177/index.html#needless_borrow\n\n"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
let _x: &i32 = &&&&&&5; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should note that this expects rustc's JSON output. Cargo puts each document in a
{"message": rustc_doc}
wrapper