Skip to content

Commit

Permalink
feat(check): check all rust files in project
Browse files Browse the repository at this point in the history
REFERENCE #10
  • Loading branch information
hobofan committed Jan 28, 2019
1 parent c45cd4d commit cf0603b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/check_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,39 @@ impl fmt::Display for UseStdStmt {
}
}

pub fn get_crate_support_from_source(src_path: &PathBuf) -> CrateSupport {
let mut file = File::open(&src_path).expect("Unable to open file");
pub fn get_crate_support_from_source(main_src_path: &PathBuf) -> CrateSupport {
let main_file_support = check_source(main_src_path, true);

let mut offenses = vec![];
match main_file_support {
CrateSupport::OnlyWithoutFeature(_) => return main_file_support,
CrateSupport::ProcMacro => return main_file_support,
CrateSupport::SourceOffenses(mut off) => offenses.append(&mut off),
CrateSupport::NoOffenseDetected => {}
};

let other_source_files_pattern = format!(
"{}/**/*.rs",
main_src_path.parent().unwrap().to_str().unwrap(),
);
let other_source_files = glob::glob(&other_source_files_pattern).unwrap();

for other_source_file in other_source_files {
let file_support = check_source(&other_source_file.unwrap(), false);
match file_support {
CrateSupport::SourceOffenses(mut off) => offenses.append(&mut off),
_ => {}
}
}

match offenses.is_empty() {
true => CrateSupport::NoOffenseDetected,
false => CrateSupport::SourceOffenses(offenses),
}
}

fn check_source(source_path: &PathBuf, is_main_file: bool) -> CrateSupport {
let mut file = File::open(&source_path).expect("Unable to open file");

let mut src = String::new();
file.read_to_string(&mut src).expect("Unable to read file");
Expand Down Expand Up @@ -176,7 +207,7 @@ pub fn get_crate_support_from_source(src_path: &PathBuf) -> CrateSupport {
let first_ident = &first_path.ident;
if first_ident == &std_ident {
let stmt = UseStdStmt {
src_path: src_path.clone(),
src_path: source_path.clone(),
item_tree: use_statement.tree.clone(),
};
offenses.push(SourceOffense::UseStdStatement(stmt));
Expand All @@ -186,10 +217,12 @@ pub fn get_crate_support_from_source(src_path: &PathBuf) -> CrateSupport {
}
}

let always_no_std: syn::Attribute = syn::parse_quote!(#![no_std]);
let contains_always_no_std = syntax.attrs.contains(&always_no_std);
if !contains_always_no_std {
offenses.push(SourceOffense::MissingNoStdAttribute);
if is_main_file {
let always_no_std: syn::Attribute = syn::parse_quote!(#![no_std]);
let contains_always_no_std = syntax.attrs.contains(&always_no_std);
if !contains_always_no_std {
offenses.push(SourceOffense::MissingNoStdAttribute);
}
}

match offenses.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions tests/detect_explicit_use_std_all_files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
4 changes: 4 additions & 0 deletions tests/detect_explicit_use_std_all_files/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/detect_explicit_use_std_all_files/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "detect_explicit_use_std_all_files"
version = "0.1.0"
authors = ["Maximilian Goisser <[email protected]>"]
edition = "2018"

[dependencies]
7 changes: 7 additions & 0 deletions tests/detect_explicit_use_std_all_files/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![no_std]

mod some_module;

fn main() {
println!("Hello, world!");
}
4 changes: 4 additions & 0 deletions tests/detect_explicit_use_std_all_files/src/some_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use std::ops::Add;
use std::string::String;

fn some_fn() {}
44 changes: 44 additions & 0 deletions tests/detect_explict_use_std_all_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extern crate assert_cmd;

use assert_cmd::prelude::*;
use std::process::Command;

#[test]
fn it_fails_with_exit_code_1() {
Command::main_binary()
.unwrap()
.arg("check")
.current_dir("./tests/detect_explicit_use_std_all_files")
.assert()
.code(1);
}

#[test]
fn it_prints_cause() {
let output = Command::main_binary()
.unwrap()
.arg("check")
.current_dir("./tests/detect_explicit_use_std_all_files")
.output()
.unwrap()
.stdout;
let output = String::from_utf8(output).unwrap();

let expected_cause = "Source code contains an explicit `use std::` statement";
assert!(output.contains(expected_cause));
}

#[test]
fn it_does_not_warn_about_no_std_statement() {
let output = Command::main_binary()
.unwrap()
.arg("check")
.current_dir("./tests/detect_explicit_use_std_all_files")
.output()
.unwrap()
.stdout;
let output = String::from_utf8(output).unwrap();

let expected_cause = "Did not find a #![no_std] attribute";
assert!(!output.contains(expected_cause));
}

0 comments on commit cf0603b

Please sign in to comment.