Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/lint_mutable.rs

6.6 KiB, 1 run

created by r1870400018:75235, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Fixture tests for the mutable-literal gate, `lint::mutable`: one source per flagged class and
2//! one per exemption, each asserting what is and is not a hit, then the ratchet baseline.
3
4use oxedyne_fe2o3_core::prelude::*;
5use oxedyne_fe2o3_text::lint::mutable::{
6 self,
7 Allow,
8 Baseline,
9 Class,
10 Hit,
11 Lang,
12 Scan,
13};
14
15use std::path::PathBuf;
16
17
18fn fixture(name: &str) -> Scan {
19 let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("lint_mutable");
20 let src = match std::fs::read_to_string(dir.join(name)) {
21 Ok(s) => s,
22 Err(e) => panic!("Reading fixture {}: {}", name, e),
23 };
24 let lang = if name.ends_with(".js") { Lang::Js } else { Lang::Rust };
25 match mutable::scan(&src, lang, name) {
26 Ok(scan) => scan,
27 Err(e) => panic!("Scanning fixture {}: {}", name, e),
28 }
29}
30
31fn texts(scan: &Scan, class: Class) -> Vec<String> {
32 scan.hits.iter().filter(|h| h.class == class).map(|h| h.text.clone()).collect()
33}
34
35#[test]
36fn test_lint_path_strings_hit() -> Outcome<()> {
37 let scan = fixture("hit_path.rs");
38 assert_eq!(texts(&scan, Class::Path),
39 vec!["/var/lib/app/state", "~/data", "./config.jdat", "store.jdat"]);
40 assert_eq!(scan.hits.len(), 4);
41 assert_eq!(scan.hits[0].line, 2);
42 Ok(())
43}
44
45#[test]
46fn test_lint_host_strings_hit() -> Outcome<()> {
47 let scan = fixture("hit_host.rs");
48 assert_eq!(texts(&scan, Class::Host),
49 vec!["https://api.example.com/v1", "127.0.0.1:8443", "pool.ntp.org", "localhost:8080"]);
50 assert_eq!(scan.hits.len(), 4);
51 Ok(())
52}
53
54#[test]
55fn test_lint_model_strings_hit() -> Outcome<()> {
56 let scan = fixture("hit_model.rs");
57 assert_eq!(texts(&scan, Class::Model), vec!["gpt-4o-mini"]);
58 assert_eq!(scan.hits.len(), 1);
59 Ok(())
60}
61
62#[test]
63fn test_lint_named_consts_hit_by_vocabulary() -> Outcome<()> {
64 let scan = fixture("hit_const.rs");
65 assert_eq!(texts(&scan, Class::Named),
66 vec!["REQUEST_TIMEOUT_SECS", "MAX_BODY", "RETRY_COUNT", "DEFAULT_PORT"]);
67 assert_eq!(scan.hits.len(), 4);
68 Ok(())
69}
70
71#[test]
72fn test_lint_timing_calls_hit_bare_numbers_do_not() -> Outcome<()> {
73 let scan = fixture("hit_call.rs");
74 assert_eq!(texts(&scan, Class::Timing), vec!["Duration::from_millis(250)"]);
75 assert_eq!(scan.hits.len(), 1);
76 Ok(())
77}
78
79#[test]
80fn test_lint_js_hits() -> Outcome<()> {
81 let scan = fixture("hit_js.js");
82 assert_eq!(texts(&scan, Class::Named), vec!["POLL_INTERVAL_MS"]);
83 assert_eq!(texts(&scan, Class::Host), vec!["https://api.example.com"]);
84 assert_eq!(texts(&scan, Class::Timing), vec!["setTimeout(1500)"]);
85 assert_eq!(scan.hits.len(), 3);
86 Ok(())
87}
88
89#[test]
90fn test_lint_js_throw_skip_stops_at_the_statement() -> Outcome<()> {
91 let scan = fixture("hit_throw.js");
92 assert_eq!(texts(&scan, Class::Host),
93 vec!["https://evil.example.com/x", "https://evil.example.com/y"]);
94 assert_eq!(scan.hits.len(), 2);
95 Ok(())
96}
97
98#[test]
99fn test_lint_rust_concatenation_is_folded() -> Outcome<()> {
100 let scan = fixture("hit_concat.rs");
101 assert_eq!(texts(&scan, Class::Host),
102 vec!["https://api.evil.example.com", "https://api.evil.net"]);
103 assert_eq!(scan.hits.len(), 2);
104 assert_eq!(scan.hits[0].line, 2);
105 Ok(())
106}
107
108#[test]
109fn test_lint_js_concatenation_is_folded() -> Outcome<()> {
110 let scan = fixture("hit_concat.js");
111 assert_eq!(texts(&scan, Class::Host),
112 vec!["https://api.example.com", "wss://relay.example.net"]);
113 assert_eq!(scan.hits.len(), 2);
114 Ok(())
115}
116
117#[test]
118fn test_lint_timing_arithmetic_new_and_const_args_hit() -> Outcome<()> {
119 let scan = fixture("hit_timing.rs");
120 assert_eq!(texts(&scan, Class::Timing), vec![
121 "Duration::from_secs(5 * 60)", "Duration::new(30, 0)", "Duration::from_secs(WAIT)",
122 "sleep(WAIT)",
123 ]);
124 assert_eq!(texts(&scan, Class::Named), vec!["RATE_TIMEOUT_SECS"]);
125 assert_eq!(scan.hits.len(), 5);
126 Ok(())
127}
128
129#[test]
130fn test_lint_comments_are_skipped() -> Outcome<()> {
131 assert!(fixture("skip_comment.rs").hits.is_empty());
132 Ok(())
133}
134
135#[test]
136fn test_lint_message_macros_are_skipped() -> Outcome<()> {
137 let scan = fixture("skip_macro.rs");
138 assert!(scan.hits.is_empty(), "{:?}", scan.hits);
139 Ok(())
140}
141
142#[test]
143fn test_lint_test_items_are_skipped() -> Outcome<()> {
144 let scan = fixture("skip_test.rs");
145 assert!(scan.hits.is_empty(), "{:?}", scan.hits);
146 Ok(())
147}
148
149#[test]
150fn test_lint_config_default_is_the_one_home() -> Outcome<()> {
151 // The `Default` of a `FromDatMap` type is skipped; that of a plain type is not.
152 let scan = fixture("skip_default.rs");
153 assert_eq!(texts(&scan, Class::Host), vec!["https://plain.example.com"]);
154 assert_eq!(scan.hits.len(), 1);
155 Ok(())
156}
157
158#[test]
159fn test_lint_fixed_marker_trailing_and_banner() -> Outcome<()> {
160 let scan = fixture("skip_fixed.rs");
161 assert_eq!(texts(&scan, Class::Path), vec!["/etc/app/x.jdat"]);
162 assert_eq!(scan.hits.len(), 1);
163 // A one-word why is itself a failure.
164 assert_eq!(scan.bad_fixed.len(), 1);
165 assert_eq!(scan.bad_fixed[0].line, 8);
166 Ok(())
167}
168
169fn hit(file: &str, class: Class, text: &str) -> Hit {
170 Hit { file: file.to_string(), line: 1, class, text: text.to_string() }
171}
172
173#[test]
174fn test_lint_baseline_ratchet() -> Outcome<()> {
175 let hits = vec![
176 hit("a.rs", Class::Path, "/x"),
177 hit("a.rs", Class::Path, "/x"),
178 hit("b.rs", Class::Named, "MAX_N"),
179 ];
180 // A baseline made from the hits passes them.
181 let base = Baseline::from_hits(&hits);
182 let rep = mutable::check(&hits, &[], &base);
183 assert!(rep.is_clean(), "{}", rep);
184
185 // One more copy of a baselined literal is new.
186 let mut more = hits.clone();
187 more.push(hit("a.rs", Class::Path, "/x"));
188 let rep = mutable::check(&more, &[], &base);
189 assert_eq!(rep.new.len(), 1);
190 assert!(!rep.is_clean());
191
192 // A literal migrated away leaves a stale entry, which also fails.
193 let rep = mutable::check(&hits[..2], &[], &base);
194 assert_eq!(rep.stale.len(), 1);
195 assert_eq!(rep.stale[0].text, "MAX_N");
196 assert!(!rep.is_clean());
197
198 // An allow entry covers a hit, but only with a non-empty why.
199 let mut base2 = Baseline::from_hits(&hits[..2]);
200 base2.allow.push(Allow {
201 file: "b.rs".to_string(),
202 class: Class::Named,
203 text: "MAX_N".to_string(),
204 why: "Generated from the Unicode tables.".to_string(),
205 });
206 assert!(mutable::check(&hits, &[], &base2).is_clean());
207 base2.allow[0].why = String::new();
208 let rep = mutable::check(&hits, &[], &base2);
209 assert_eq!(rep.bad_allow.len(), 1);
210 assert!(!rep.is_clean());
211 Ok(())
212}