ferridriver-test 0.4.0

E2E test runner for ferridriver. Playwright-compatible API, parallel workers, auto-retrying expect, fixtures, snapshots.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Test discovery: inventory-based collection for Rust, glob-based file scanning.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use crate::config::TestConfig;
use crate::fixture::FixturePool;
use std::fmt;
use std::path::Path;

use crate::model::{
  ExpectedStatus, Hooks, TestAnnotation, TestCase, TestFailure, TestId, TestInfo, TestPlan, TestSuite,
};

// ── Inventory-based registration (populated by #[ferritest] macro) ──

/// What the `#[ferritest]` proc macro submits via `inventory::submit!`.
pub struct TestRegistration {
  pub file: &'static str,
  /// The `module_path!()` of the test function.
  /// Used to derive the suite name from the Rust module structure.
  pub module_path: &'static str,
  pub name: &'static str,
  pub fixture_requests: &'static [&'static str],
  pub annotations: &'static [TestAnnotation],
  pub timeout_ms: Option<u64>,
  pub retries: Option<u32>,
  /// Raw JSON string for fixture/context overrides (viewport, locale, etc.)
  pub use_options: Option<&'static str>,
  pub test_fn: fn(FixturePool) -> Pin<Box<dyn Future<Output = Result<(), TestFailure>> + Send>>,
}

inventory::collect!(TestRegistration);

/// Hook kind tag for inventory registration (no closures — just the discriminant).
#[derive(Debug, Clone, Copy)]
pub enum HookKindTag {
  BeforeAll,
  AfterAll,
  BeforeEach,
  AfterEach,
}

/// What `#[before_all]` / `#[after_all]` / `#[before_each]` / `#[after_each]` submit.
pub struct HookRegistration {
  pub module_path: &'static str,
  /// For before_all/after_all: `fn(FixturePool) -> Future<Result<(), TestFailure>>`
  pub suite_hook_fn: Option<fn(FixturePool) -> Pin<Box<dyn Future<Output = Result<(), TestFailure>> + Send>>>,
  /// For before_each/after_each: `fn(FixturePool, Arc<TestInfo>) -> Future<Result<(), TestFailure>>`
  pub each_hook_fn:
    Option<fn(FixturePool, Arc<TestInfo>) -> Pin<Box<dyn Future<Output = Result<(), TestFailure>> + Send>>>,
  pub kind: HookKindTag,
}

inventory::collect!(HookRegistration);

/// What the `#[fixture]` proc macro submits via `inventory::submit!`.
///
/// `build` is a plain fn pointer (const-storable in the inventory static)
/// that constructs the heap-allocated [`crate::fixture::FixtureDef`] at collection time.
pub struct FixtureRegistration {
  pub name: &'static str,
  pub module_path: &'static str,
  pub build: fn() -> crate::fixture::FixtureDef,
}

inventory::collect!(FixtureRegistration);

/// What the `#[ferritest_suite]` proc macro submits via `inventory::submit!`.
/// Sets the execution mode of the suite derived from `module_path`.
pub struct SuiteModeRegistration {
  pub module_path: &'static str,
  pub mode: crate::model::SuiteMode,
}

inventory::collect!(SuiteModeRegistration);

/// Collect every `#[fixture]`-registered custom fixture into a defs map,
/// keyed by fixture name. Merged into the worker fixture pool so tests and
/// other fixtures can resolve them via `ctx.get::<T>(name)`.
pub fn collect_rust_fixtures() -> rustc_hash::FxHashMap<String, crate::fixture::FixtureDef> {
  let mut defs = rustc_hash::FxHashMap::default();
  for reg in inventory::iter::<FixtureRegistration> {
    defs.insert(reg.name.to_string(), (reg.build)());
  }
  defs
}

// ── Discovery ──

/// Derive suite name from `module_path!()`.
///
/// `module_path!()` expands to the MODULE path (it never includes the
/// function name), so `"my_crate::login_tests"` -> `"login_tests"`. We strip
/// only the crate root; the remainder is the suite. A top-level test (no
/// enclosing module) keeps the crate name as its suite.
fn suite_from_module_path(mp: &str) -> &str {
  mp.split_once("::").map_or(mp, |(_, rest)| rest)
}

/// Collect all registered tests and build a `TestPlan`.
pub fn collect_rust_tests(config: &TestConfig) -> TestPlan {
  let mut suites: rustc_hash::FxHashMap<String, TestSuite> = rustc_hash::FxHashMap::default();

  for reg in inventory::iter::<TestRegistration> {
    let file = reg.file.to_string();
    // Derive suite name from module_path: strip the last segment (fn name).
    let suite_name = suite_from_module_path(reg.module_path);
    let suite_key = format!("{}::{}", file, suite_name);

    let test_fn_ptr = reg.test_fn;
    let test_case: TestCase = TestCase {
      id: TestId {
        file: file.clone(),
        suite: Some(suite_name.to_string()),
        name: reg.name.to_string(),
        line: None,
      },
      test_fn: Arc::new(move |pool| test_fn_ptr(pool)),
      fixture_requests: reg.fixture_requests.iter().map(|s| (*s).to_string()).collect(),
      annotations: reg.annotations.to_vec(),
      timeout: reg.timeout_ms.map(std::time::Duration::from_millis),
      retries: reg.retries,
      expected_status: ExpectedStatus::Pass,
      use_options: reg.use_options.map(|s| serde_json::from_str(s).unwrap_or_default()),
    };

    let suite = suites.entry(suite_key).or_insert_with(|| TestSuite {
      name: suite_name.to_string(),
      file: file.clone(),
      tests: Vec::new(),
      hooks: Hooks::default(),
      annotations: Vec::new(),
      mode: crate::model::SuiteMode::default(),
    });
    suite.tests.push(test_case);
  }

  // Collect hooks and attach them to matching suites.
  for reg in inventory::iter::<HookRegistration> {
    let hook_suite = suite_from_module_path(reg.module_path);
    // Find the matching suite — hooks attach to the suite derived from their module.
    for suite in suites.values_mut() {
      if suite.name == hook_suite {
        match reg.kind {
          HookKindTag::BeforeAll => {
            if let Some(f) = reg.suite_hook_fn {
              suite.hooks.before_all.push(Arc::new(move |pool| f(pool)));
            }
          },
          HookKindTag::AfterAll => {
            if let Some(f) = reg.suite_hook_fn {
              suite.hooks.after_all.push(Arc::new(move |pool| f(pool)));
            }
          },
          HookKindTag::BeforeEach => {
            if let Some(f) = reg.each_hook_fn {
              suite.hooks.before_each.push(Arc::new(move |pool, info| f(pool, info)));
            }
          },
          HookKindTag::AfterEach => {
            if let Some(f) = reg.each_hook_fn {
              suite.hooks.after_each.push(Arc::new(move |pool, info| f(pool, info)));
            }
          },
        }
      }
    }
  }

  // Apply explicit suite modes from `#[ferritest_suite]`, keyed by the same
  // module-derived suite name `#[ferritest]` registrations use.
  for reg in inventory::iter::<SuiteModeRegistration> {
    let reg_suite = suite_from_module_path(reg.module_path);
    for suite in suites.values_mut() {
      if suite.name == reg_suite {
        suite.mode = reg.mode;
      }
    }
  }

  let suites: Vec<TestSuite> = suites.into_values().collect();
  let total_tests = suites.iter().map(|s| s.tests.len()).sum();

  apply_filters(
    TestPlan {
      suites,
      total_tests,
      shard: None,
    },
    config,
  )
}

/// Discover test files on disk using glob patterns.
///
/// # Errors
///
/// Returns an error if glob pattern is invalid.
pub fn find_test_files(root: &str, patterns: &[String], ignore: &[String]) -> Result<Vec<String>, String> {
  let mut files = Vec::new();

  for pattern in patterns {
    let full_pattern = if pattern.starts_with('/') || pattern.starts_with('.') {
      pattern.clone()
    } else {
      format!("{root}/{pattern}")
    };

    let entries = glob::glob(&full_pattern).map_err(|e| format!("invalid glob pattern '{full_pattern}': {e}"))?;

    for entry in entries {
      let path = entry.map_err(|e| format!("glob error: {e}"))?;
      let path_str = path.display().to_string();

      // Check ignore patterns.
      let ignored = ignore
        .iter()
        .any(|ig| glob::Pattern::new(ig).map(|p| p.matches(&path_str)).unwrap_or(false));

      if !ignored {
        files.push(path_str);
      }
    }
  }

  files.sort();
  files.dedup();
  Ok(files)
}

/// Apply grep, tag, and other filters to a test plan.
fn apply_filters(mut plan: TestPlan, _config: &TestConfig) -> TestPlan {
  // Grep is applied at runtime via CLI, not from config file typically.
  // This is a placeholder for the runner to call with CliOverrides.
  plan.total_tests = plan.suites.iter().map(|s| s.tests.len()).sum();
  plan
}

/// Filter a test plan by grep pattern.
pub fn filter_by_grep(plan: &mut TestPlan, pattern: &str, invert: bool) {
  // Build a case-insensitive regex. If the pattern has invalid regex syntax,
  // fall back to case-insensitive literal substring match.
  let re = regex::RegexBuilder::new(pattern).case_insensitive(true).build().ok();
  let pattern_lower = pattern.to_lowercase();

  for suite in &mut plan.suites {
    suite.tests.retain(|test| {
      let full_name = test.id.full_name();
      let matches = if let Some(ref r) = re {
        r.is_match(&full_name)
      } else {
        // Fallback: case-insensitive substring search.
        full_name.to_lowercase().contains(&pattern_lower)
      };
      if invert { !matches } else { matches }
    });
  }
  plan.suites.retain(|s| !s.tests.is_empty());
  plan.total_tests = plan.suites.iter().map(|s| s.tests.len()).sum();
}

/// Error returned when `--forbid-only` is set and `.only()` markers are found.
pub struct ForbidOnlyError {
  pub tests: Vec<String>,
}

impl fmt::Display for ForbidOnlyError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    writeln!(f, "Error: test.only() found in {} test(s):", self.tests.len())?;
    for name in &self.tests {
      writeln!(f, "  {name}")?;
    }
    Ok(())
  }
}

/// Check that no tests or suites have `Only` annotations.
/// Returns `Err` listing all offending tests if any are found.
pub fn check_forbid_only(plan: &TestPlan) -> Result<(), ForbidOnlyError> {
  let mut only_tests: Vec<String> = Vec::new();

  for suite in &plan.suites {
    let suite_is_only = suite.annotations.iter().any(|a| matches!(a, TestAnnotation::Only));
    for test in &suite.tests {
      let test_is_only = test.annotations.iter().any(|a| matches!(a, TestAnnotation::Only));
      if suite_is_only || test_is_only {
        only_tests.push(test.id.full_name());
      }
    }
  }

  if only_tests.is_empty() {
    Ok(())
  } else {
    Err(ForbidOnlyError { tests: only_tests })
  }
}

/// Filter a test plan to only `Only`-marked tests/suites.
/// If no `Only` annotations exist, the plan is unchanged.
pub fn filter_by_only(plan: &mut TestPlan) {
  let has_only = plan.suites.iter().any(|suite| {
    suite.annotations.iter().any(|a| matches!(a, TestAnnotation::Only))
      || suite
        .tests
        .iter()
        .any(|t| t.annotations.iter().any(|a| matches!(a, TestAnnotation::Only)))
  });

  if !has_only {
    return;
  }

  for suite in &mut plan.suites {
    let suite_is_only = suite.annotations.iter().any(|a| matches!(a, TestAnnotation::Only));
    if !suite_is_only {
      suite
        .tests
        .retain(|t| t.annotations.iter().any(|a| matches!(a, TestAnnotation::Only)));
    }
  }
  plan.suites.retain(|s| !s.tests.is_empty());
  plan.total_tests = plan.suites.iter().map(|s| s.tests.len()).sum();
}

/// Filter a test plan to only tests listed in a rerun file.
/// The rerun file contains one `file:line` or `file > suite > name` entry per line.
/// If the file doesn't exist or is empty, logs a warning and runs all tests.
pub fn filter_by_rerun(plan: &mut TestPlan, rerun_path: &Path) {
  let content = match std::fs::read_to_string(rerun_path) {
    Ok(c) if !c.trim().is_empty() => c,
    Ok(_) => {
      tracing::warn!("rerun file {} is empty, running all tests", rerun_path.display());
      return;
    },
    Err(_) => {
      tracing::warn!("rerun file {} not found, running all tests", rerun_path.display());
      return;
    },
  };

  let rerun_set: rustc_hash::FxHashSet<String> = content
    .lines()
    .map(|l| l.trim().to_string())
    .filter(|l| !l.is_empty())
    .collect();

  for suite in &mut plan.suites {
    suite
      .tests
      .retain(|test| rerun_set.contains(&test.id.file_location()) || rerun_set.contains(&test.id.full_name()));
  }
  plan.suites.retain(|s| !s.tests.is_empty());
  plan.total_tests = plan.suites.iter().map(|s| s.tests.len()).sum();
}

/// Filter a test plan by tag.
pub fn filter_by_tag(plan: &mut TestPlan, tag: &str) {
  for suite in &mut plan.suites {
    suite.tests.retain(|test| {
      test
        .annotations
        .iter()
        .any(|a| matches!(a, TestAnnotation::Tag(t) if t == tag))
    });
  }
  plan.suites.retain(|s| !s.tests.is_empty());
  plan.total_tests = plan.suites.iter().map(|s| s.tests.len()).sum();
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::model::{ExpectedStatus, Hooks, TestCase, TestPlan, TestSuite};

  fn dummy_test(name: &str, annotations: Vec<TestAnnotation>) -> TestCase {
    TestCase {
      id: TestId {
        file: "test.rs".into(),
        suite: Some("suite".into()),
        name: name.into(),
        line: None,
      },
      test_fn: Arc::new(|_| Box::pin(async { Ok(()) })),
      fixture_requests: vec![],
      annotations,
      timeout: None,
      retries: None,
      expected_status: ExpectedStatus::Pass,
      use_options: None,
    }
  }

  fn make_plan(tests: Vec<TestCase>, suite_annotations: Vec<TestAnnotation>) -> TestPlan {
    let total = tests.len();
    TestPlan {
      suites: vec![TestSuite {
        name: "suite".into(),
        file: "test.rs".into(),
        tests,
        hooks: Hooks::default(),
        annotations: suite_annotations,
        mode: crate::model::SuiteMode::default(),
      }],
      total_tests: total,
      shard: None,
    }
  }

  #[test]
  fn forbid_only_no_only_markers() {
    let plan = make_plan(vec![dummy_test("test1", vec![]), dummy_test("test2", vec![])], vec![]);
    assert!(check_forbid_only(&plan).is_ok());
  }

  #[test]
  fn forbid_only_detects_test_level_only() {
    let plan = make_plan(
      vec![
        dummy_test("normal", vec![]),
        dummy_test("focused", vec![TestAnnotation::Only]),
      ],
      vec![],
    );
    let err = check_forbid_only(&plan).unwrap_err();
    assert_eq!(err.tests.len(), 1);
    assert!(err.tests[0].contains("focused"));
  }

  #[test]
  fn forbid_only_detects_suite_level_only() {
    let plan = make_plan(
      vec![dummy_test("test1", vec![]), dummy_test("test2", vec![])],
      vec![TestAnnotation::Only],
    );
    let err = check_forbid_only(&plan).unwrap_err();
    assert_eq!(err.tests.len(), 2);
  }

  #[test]
  fn filter_by_only_keeps_only_marked_tests() {
    let mut plan = make_plan(
      vec![
        dummy_test("normal1", vec![]),
        dummy_test("focused", vec![TestAnnotation::Only]),
        dummy_test("normal2", vec![]),
      ],
      vec![],
    );
    filter_by_only(&mut plan);
    assert_eq!(plan.total_tests, 1);
    assert_eq!(plan.suites[0].tests[0].id.name, "focused");
  }

  #[test]
  fn filter_by_only_no_only_keeps_all() {
    let mut plan = make_plan(vec![dummy_test("test1", vec![]), dummy_test("test2", vec![])], vec![]);
    filter_by_only(&mut plan);
    assert_eq!(plan.total_tests, 2);
  }

  #[test]
  fn filter_by_only_suite_level_keeps_all_in_suite() {
    let mut plan = make_plan(
      vec![dummy_test("test1", vec![]), dummy_test("test2", vec![])],
      vec![TestAnnotation::Only],
    );
    filter_by_only(&mut plan);
    assert_eq!(plan.total_tests, 2);
  }

  #[test]
  fn forbid_only_error_message_format() {
    let plan = make_plan(vec![dummy_test("focused", vec![TestAnnotation::Only])], vec![]);
    let err = check_forbid_only(&plan).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("test.only() found in 1 test(s)"));
    assert!(msg.contains("focused"));
  }

  #[test]
  fn filter_by_rerun_keeps_matching_tests() {
    let dir = std::env::temp_dir().join("ferritest_rerun_test");
    std::fs::create_dir_all(&dir).unwrap();
    let rerun_path = dir.join("@rerun.txt");
    std::fs::write(&rerun_path, "test.rs:10\n").unwrap();

    let mut plan = make_plan(
      vec![
        {
          let mut t = dummy_test("match", vec![]);
          t.id.line = Some(10);
          t
        },
        dummy_test("nomatch", vec![]),
      ],
      vec![],
    );
    filter_by_rerun(&mut plan, &rerun_path);
    assert_eq!(plan.total_tests, 1);
    assert_eq!(plan.suites[0].tests[0].id.name, "match");

    std::fs::remove_dir_all(&dir).ok();
  }

  #[test]
  fn filter_by_rerun_missing_file_keeps_all() {
    let mut plan = make_plan(vec![dummy_test("test1", vec![]), dummy_test("test2", vec![])], vec![]);
    filter_by_rerun(&mut plan, Path::new("/nonexistent/@rerun.txt"));
    assert_eq!(plan.total_tests, 2);
  }

  #[test]
  fn filter_by_rerun_empty_file_keeps_all() {
    let dir = std::env::temp_dir().join("ferritest_rerun_empty");
    std::fs::create_dir_all(&dir).unwrap();
    let rerun_path = dir.join("@rerun.txt");
    std::fs::write(&rerun_path, "  \n").unwrap();

    let mut plan = make_plan(vec![dummy_test("test1", vec![]), dummy_test("test2", vec![])], vec![]);
    filter_by_rerun(&mut plan, &rerun_path);
    assert_eq!(plan.total_tests, 2);

    std::fs::remove_dir_all(&dir).ok();
  }

  #[test]
  fn filter_by_rerun_matches_full_name() {
    let dir = std::env::temp_dir().join("ferritest_rerun_fullname");
    std::fs::create_dir_all(&dir).unwrap();
    let rerun_path = dir.join("@rerun.txt");
    std::fs::write(&rerun_path, "test.rs > suite > focused\n").unwrap();

    let mut plan = make_plan(vec![dummy_test("focused", vec![]), dummy_test("other", vec![])], vec![]);
    filter_by_rerun(&mut plan, &rerun_path);
    assert_eq!(plan.total_tests, 1);
    assert_eq!(plan.suites[0].tests[0].id.name, "focused");

    std::fs::remove_dir_all(&dir).ok();
  }
}