This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check to prevent forgetting GTK initialization checks
- Loading branch information
1 parent
c4e91b7
commit 50e5e7f
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,76 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
// We want to ensure that all subclasses have: | ||
// | ||
// ``` | ||
// if !crate::rt::is_initialized() { | ||
// panic!("GTK has to be initialized first"); | ||
// } | ||
// ``` | ||
|
||
use std::fs::{read_dir, read_to_string}; | ||
use std::path::Path; | ||
|
||
fn check_file(f: &Path) -> usize { | ||
let s = read_to_string(f).expect("cannot read file"); | ||
let mut checking_type: Option<String> = None; | ||
let mut found_is_initialized_check = false; | ||
|
||
for line in s.lines() { | ||
if checking_type.is_none() { | ||
if !line.starts_with("unsafe impl") || !line.contains(" IsSubclassable<") { | ||
continue; | ||
} | ||
if let Some(for_) = line | ||
.split(" for ") | ||
.nth(1) | ||
.and_then(|s| s.split(' ').next()) | ||
{ | ||
checking_type = Some(for_.to_owned()); | ||
} | ||
continue; | ||
} | ||
if line == "}" { | ||
// Analysis complete! | ||
break; | ||
} | ||
let trimmed = line.trim(); | ||
if trimmed.contains("is_initialized()") { | ||
found_is_initialized_check = true; | ||
} | ||
} | ||
if !found_is_initialized_check { | ||
if let Some(for_) = checking_type { | ||
if for_ == "Application" { | ||
return 0; | ||
} | ||
eprintln!( | ||
"[{}] Missing `is_initialized()` check in subclass implementation (`{}`)", | ||
f.display(), | ||
for_ | ||
); | ||
} else { | ||
eprintln!( | ||
"Missing `IsSubclassable` implementation in `{}`", | ||
f.display() | ||
) | ||
} | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
#[test] | ||
fn check_subclass_panic() { | ||
let mut errors = 0; | ||
|
||
for entry in read_dir("src/subclass").unwrap() { | ||
let entry = entry.expect("invalid entry"); | ||
let path = entry.path(); | ||
if path.is_file() { | ||
errors += check_file(&path); | ||
} | ||
} | ||
assert!(errors == 0); | ||
} |