Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit a91c4f7

Browse files
Add check to prevent forgetting GTK initialization checks
1 parent c4e91b7 commit a91c4f7

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

gtk/tests/check_subclass_panic.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
// We want to ensure that all subclasses have:
4+
//
5+
// ```
6+
// if !crate::rt::is_initialized() {
7+
// panic!("GTK has to be initialized first");
8+
// }
9+
// ```
10+
11+
use std::fs::{read_dir, read_to_string};
12+
use std::path::Path;
13+
14+
fn check_file(f: &Path) -> usize {
15+
let s = read_to_string(f).expect("cannot read file");
16+
let mut checking_type: Option<String> = None;
17+
let mut found_is_initialized_check = false;
18+
19+
for line in s.lines() {
20+
if checking_type.is_none() {
21+
if !line.starts_with("unsafe impl") || !line.contains(" IsSubclassable<") {
22+
continue;
23+
}
24+
if let Some(for_) = line.split(" for ").nth(1).and_then(|s| s.split(' ').next()) {
25+
checking_type = Some(for_.to_owned());
26+
}
27+
continue;
28+
}
29+
if line == "}" {
30+
// Analysis complete!
31+
break;
32+
}
33+
let trimmed = line.trim();
34+
if trimmed.contains("is_initialized()") {
35+
found_is_initialized_check = true;
36+
}
37+
}
38+
if !found_is_initialized_check {
39+
if let Some(for_) = checking_type {
40+
if for_ == "Application" {
41+
return 0;
42+
}
43+
eprintln!(
44+
"[{}] Missing `is_initialized()` check in subclass implementation (`{}`)",
45+
f.display(),
46+
for_
47+
);
48+
1
49+
} else {
50+
0
51+
}
52+
} else {
53+
0
54+
}
55+
}
56+
57+
#[test]
58+
fn check_subclass_panic() {
59+
let mut errors = 0;
60+
61+
for entry in read_dir("src/subclass").unwrap() {
62+
let entry = entry.expect("invalid entry");
63+
let path = entry.path();
64+
if path.is_file() {
65+
errors += check_file(&path);
66+
}
67+
}
68+
assert!(errors == 0);
69+
}

0 commit comments

Comments
 (0)