cuke-dedup 0.14.0

Static analysis for duplicate and reusable Cucumber step definitions
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use super::*;
use std::collections::BTreeSet;
use std::fs;

/// Creates a temporary project with a local Cucumber registration re-export.
fn session_project() -> tempfile::TempDir {
    let directory = tempfile::tempdir().unwrap();
    fs::write(directory.path().join("package.json"), "{}").unwrap();
    fs::write(
        directory.path().join("support.ts"),
        "export { Given } from '@cucumber/cucumber';",
    )
    .unwrap();
    directory
}

/// Extracts a synthetic source through its suffix-selected adapter and supplied session, panicking on failure.
fn session_extract(
    session: &mut SourceExtractionSession,
    root: &Path,
    suffix: &str,
    source: &str,
) -> Extraction {
    let path = root.join(format!("steps.{suffix}"));
    let adapter = adapter_for_path(&path).unwrap();
    adapter
        .extract_with_session(
            source,
            &SourceFile {
                path,
                language: adapter.language(),
            },
            session,
        )
        .unwrap()
}

const IMPORTED_STEP: &str =
    "import { Given } from './support'; Given('shared step', () => work());";

/// Checks backend state initializes once per session and invalid or missing state returns an error.
#[test]
fn session_state_initializes_once_per_backend_and_per_run() {
    let ruby = SourceAdapterRegistration::with_session(".rb", &crate::ruby::RUBY_ADAPTER);
    assert_eq!(ruby.adapter.language(), SourceLanguage::Ruby);
    assert!(ruby.discover_dependencies.is_some());

    struct Counter(usize);
    impl AdapterSessionState for Counter {
        /// Creates fresh state for the test backend without using project configuration.
        fn initialize(_: Option<&Path>, _: &[String], _: &[String]) -> Self {
            Self(0)
        }
    }
    struct Other;
    impl AdapterSessionState for Other {
        /// Creates fresh state for the test backend without using project configuration.
        fn initialize(_: Option<&Path>, _: &[String], _: &[String]) -> Self {
            Self
        }
    }
    let mut session = SourceExtractionSession::default();
    session.state::<Counter>().unwrap().0 = 7;
    session.initialize::<Counter>();
    session.state::<Other>().unwrap();
    assert_eq!(session.states.len(), 2);
    assert_eq!(session.state::<Counter>().unwrap().0, 7);
    assert_eq!(
        SourceExtractionSession::default()
            .state::<Counter>()
            .unwrap()
            .0,
        0
    );
    let project = tempfile::tempdir().unwrap();
    let mut explicit = SourceExtractionSession::new(project.path());
    assert!(explicit.state::<Counter>().is_err());
    assert!(!explicit.states.contains_key(&TypeId::of::<Counter>()));
    // Even a violated internal key/value invariant must return an error, not panic.
    session
        .states
        .insert(TypeId::of::<Counter>(), Box::new(Other));
    assert!(session.state::<Counter>().is_err());
}

/// Checks JS, TS, and TSX share a resolver snapshot and retain duplicate findings in either extraction order.
#[test]
fn session_mixed_languages_share_cache_and_preserve_findings_in_either_order() {
    for suffixes in [["js", "ts", "tsx"], ["tsx", "ts", "js"]] {
        let project = session_project();
        let root = project.path();
        let mut session = SourceExtractionSession::new(root);
        assert_eq!(session.states.len(), 2);
        let mut definitions = Vec::new();
        for suffix in suffixes {
            let extracted = session_extract(&mut session, root, suffix, IMPORTED_STEP);
            assert_eq!(extracted.definitions.len(), 1, "{suffix}");
            assert!(extracted.diagnostics.is_empty(), "{suffix}");
            // A second resolver would observe this edit; a shared run keeps cached exports.
            fs::write(
                root.join("support.ts"),
                "export { When } from '@cucumber/cucumber';",
            )
            .unwrap();
            definitions.extend(extracted.definitions);
        }
        fs::write(
            root.join("support.ts"),
            "export { Given } from '@cucumber/cucumber';",
        )
        .unwrap();
        let mut expected = Vec::new();
        for suffix in suffixes {
            let extracted = session_extract(
                &mut SourceExtractionSession::default(),
                root,
                suffix,
                IMPORTED_STEP,
            );
            assert!(extracted.diagnostics.is_empty());
            expected.extend(extracted.definitions);
        }
        assert_eq!(definitions, expected);
        let config = crate::config::Config::load(root, Default::default()).unwrap();
        let result = crate::analysis::analyze(definitions, vec![], &config).unwrap();
        assert_eq!(
            result
                .findings
                .iter()
                .filter(|finding| finding.rule == crate::model::Rule::DuplicateMatcher)
                .count(),
            2
        );
    }
}

/// Checks a session retains its failed-root fallback after that root is created, while a new session observes it.
#[test]
fn session_keeps_failed_root_resolution_snapshot_and_default_fallback() {
    let project = session_project();
    let missing = project.path().join("initially-missing");
    let mut session = SourceExtractionSession::new(&missing);
    fs::create_dir(&missing).unwrap();
    let actual = session_extract(&mut session, project.path(), "ts", IMPORTED_STEP);
    let expected = session_extract(
        &mut SourceExtractionSession::default(),
        project.path(),
        "ts",
        IMPORTED_STEP,
    );
    assert_eq!(actual, expected);
    assert_eq!(actual.definitions.len(), 1);
    let rejected = session_extract(
        &mut SourceExtractionSession::new(&missing),
        project.path(),
        "ts",
        IMPORTED_STEP,
    );
    assert!(rejected.definitions.is_empty());
    assert!(!rejected.diagnostics.is_empty());
}

/// Checks an existing session retains its canonical project root after a symlink is retargeted.
#[cfg(unix)]
#[test]
fn session_keeps_valid_root_snapshot_when_symlink_is_retargeted() {
    let original = session_project();
    let other = session_project();
    let links = tempfile::tempdir().unwrap();
    let link = links.path().join("root");
    std::os::unix::fs::symlink(original.path(), &link).unwrap();
    let mut session = SourceExtractionSession::new(&link);
    fs::remove_file(&link).unwrap();
    std::os::unix::fs::symlink(other.path(), &link).unwrap();
    let actual = session_extract(&mut session, original.path(), "ts", IMPORTED_STEP);
    assert_eq!(actual.definitions.len(), 1);
    assert!(actual.diagnostics.is_empty());
    let rejected = session_extract(
        &mut SourceExtractionSession::new(&link),
        original.path(),
        "ts",
        IMPORTED_STEP,
    );
    assert!(rejected.definitions.is_empty());
    assert!(!rejected.diagnostics.is_empty());
}

/// Checks separate project sessions isolate custom registration names and local export caches.
#[test]
fn session_roots_and_configured_registrations_are_isolated() {
    let project = session_project();
    let other = session_project();
    let mut first = SourceExtractionSession::with_registrations(project.path(), &["SetupA".into()]);
    let mut second = SourceExtractionSession::with_registrations(other.path(), &["SetupB".into()]);
    let source = "SetupA('custom', () => work()); SetupB('custom', () => work());";
    for (session, root, name) in [
        (&mut first, project.path(), "SetupA"),
        (&mut second, other.path(), "SetupB"),
    ] {
        let result = session_extract(session, root, "ts", source);
        assert_eq!(result.definitions.len(), 1);
        assert_eq!(result.definitions[0].registration, name);
        assert!(result.diagnostics.is_empty());
    }
    fs::write(
        other.path().join("support.ts"),
        "export { When } from '@cucumber/cucumber';",
    )
    .unwrap();
    assert_eq!(
        session_extract(&mut first, project.path(), "ts", IMPORTED_STEP)
            .definitions
            .len(),
        1
    );
    assert!(
        session_extract(&mut second, other.path(), "ts", IMPORTED_STEP)
            .definitions
            .is_empty()
    );
}

struct DefaultSessionAdapter;

impl SourceAdapter for DefaultSessionAdapter {
    /// Returns the default-delegation test adapter's diagnostic name.
    fn name(&self) -> &'static str {
        "default-session-test"
    }

    /// Identifies the default-delegation test adapter as JavaScript.
    fn language(&self) -> SourceLanguage {
        SourceLanguage::JavaScript
    }

    /// Returns a sentinel error so tests can detect delegation to the legacy extraction method.
    fn extract(&self, _source: &str, _file: &SourceFile) -> Result<Extraction> {
        anyhow::bail!("default extract delegation reached")
    }
}

/// Checks every supported suffix routes to the expected language and named adapter.
#[test]
fn registry_routes_supported_suffixes() {
    let cases = [
        ("steps.js", SourceLanguage::JavaScript),
        ("steps.mjs", SourceLanguage::JavaScript),
        ("steps.cjs", SourceLanguage::JavaScript),
        ("steps.jsx", SourceLanguage::JavaScript),
        ("steps.ts", SourceLanguage::TypeScript),
        ("steps.mts", SourceLanguage::TypeScript),
        ("steps.cts", SourceLanguage::TypeScript),
        ("steps.tsx", SourceLanguage::Tsx),
        ("steps.rb", SourceLanguage::Ruby),
    ];
    for (path, expected) in cases {
        let adapter = adapter_for_path(Path::new(path)).expect(path);
        assert_eq!(adapter.language(), expected, "{path}");
        let by_language = adapter_for_language(expected);
        assert_eq!(by_language.language(), expected, "{path}");
        assert_eq!(by_language.name(), adapter.name(), "{path}");
        assert!(!adapter.name().is_empty());
    }
    assert_eq!(registered_suffixes().len(), cases.len());
}

/// Checks each registered language grammar parses a representative source without syntax errors.
#[test]
fn source_languages_select_their_registered_tree_sitter_grammar() {
    let cases = [
        (SourceLanguage::JavaScript, "const value = 1;"),
        (SourceLanguage::TypeScript, "const value: number = 1;"),
        (SourceLanguage::Tsx, "const value = <div />;"),
        (SourceLanguage::Ruby, "Given('a') { work() }"),
    ];

    for (language, source) in cases {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&grammar_for_language(language))
            .unwrap();
        let tree = parser.parse(source, None).unwrap();
        assert!(!tree.root_node().has_error(), "{language:?}");
    }
}

/// Checks registry suffixes are unique, initialize sessions, and extract one definition into shared IR.
#[test]
fn every_registration_has_a_unique_suffix_and_produces_shared_ir() {
    let mut suffixes = BTreeSet::new();
    for registration in SOURCE_ADAPTER_REGISTRY {
        assert!(suffixes.insert(registration.suffix), "duplicate suffix");
        assert!(
            registration.initialize_session.is_some(),
            "{} is missing session initialization",
            registration.suffix
        );
        let file = SourceFile {
            path: PathBuf::from(format!("steps{}", registration.suffix)),
            language: registration.adapter.language(),
        };
        let extraction = registration
            .adapter
            .extract(
                if file.language == SourceLanguage::Ruby {
                    "Given('a registered step') { run() }"
                } else {
                    "Given('a registered step', () => run())"
                },
                &file,
            )
            .unwrap();
        assert_eq!(extraction.definitions.len(), 1, "{}", registration.suffix);
        assert!(extraction.diagnostics.is_empty(), "{}", registration.suffix);
    }
}

/// Checks declaration files, source maps, and unsupported language suffixes are excluded from adapter routing.
#[test]
fn registry_rejects_declarations_maps_and_unknown_languages() {
    for path in [
        "types.d.ts",
        "types.d.mts",
        "types.d.cts",
        ".d.ts",
        ".d.mts",
        ".d.cts",
        "steps.js.map",
        "steps.py",
    ] {
        assert!(adapter_for_path(Path::new(path)).is_none(), "{path}");
    }
}

/// Checks non-UTF-8 Unix filenames route by ASCII extension while declaration files remain excluded.
#[cfg(unix)]
#[test]
fn registry_classifies_non_utf8_names_by_their_ascii_extension() {
    use std::ffi::OsString;
    use std::os::unix::ffi::OsStringExt;

    let source = PathBuf::from(OsString::from_vec(b"steps-\xff.ts".to_vec()));
    assert_eq!(language_for_path(&source), Some(SourceLanguage::TypeScript));

    let declaration = PathBuf::from(OsString::from_vec(b"types-\xff.d.ts".to_vec()));
    assert!(adapter_for_path(&declaration).is_none());
}

/// Checks the default session extraction method delegates to an adapter's existing extract implementation.
#[test]
fn adapter_session_default_preserves_existing_extract_implementations() {
    let directory = tempfile::tempdir().unwrap();
    let file = SourceFile {
        path: directory.path().join("steps.js"),
        language: SourceLanguage::JavaScript,
    };
    let mut session = SourceExtractionSession::new(directory.path());

    let adapter = DefaultSessionAdapter;
    assert_eq!(adapter.name(), "default-session-test");
    assert_eq!(adapter.language(), SourceLanguage::JavaScript);

    let error = adapter
        .extract_with_session("", &file, &mut session)
        .unwrap_err();

    assert_eq!(error.to_string(), "default extract delegation reached");
}

/// Checks language and suffix lookup select the identical registered adapter instances.
#[test]
fn language_lookup_and_suffix_routing_agree_on_one_adapter_table() {
    // Data-address identity (not `ptr::eq`, which compares wide-pointer vtables) is what
    // "one authoritative mapping" means here: both routing surfaces must select the same
    // adapter static, not merely two adapters that agree on name and language.
    for registration in SOURCE_ADAPTER_REGISTRY {
        let expected = registration.adapter.language();
        let name = format!("steps{}", registration.suffix);
        let routed = adapter_for_path(Path::new(&name)).expect(&name);
        assert!(
            std::ptr::addr_eq(
                routed as *const dyn SourceAdapter,
                registration.adapter as *const dyn SourceAdapter
            ),
            "{}",
            registration.suffix
        );
        let by_language = adapter_for_language(expected);
        assert!(
            std::ptr::addr_eq(
                by_language as *const dyn SourceAdapter,
                registration.adapter as *const dyn SourceAdapter
            ),
            "{}",
            registration.suffix
        );
        assert_eq!(language_for_path(Path::new(&name)), Some(expected));
    }
    for language in [
        SourceLanguage::JavaScript,
        SourceLanguage::TypeScript,
        SourceLanguage::Tsx,
    ] {
        assert_eq!(adapter_for_language(language).language(), language);
    }
}

/// Checks either routing surface can warm shared session state without changing subsequent extraction.
#[test]
fn language_routed_and_suffix_routed_extraction_share_session_state() {
    // The registry's session initializers must prepare exactly the backend state that
    // `adapter_for_language` routes to: the same resolver-backed import has to extract
    // identically through either surface, whichever warms the shared cache first.
    let project = session_project();
    for suffix in ["js", "ts", "tsx"] {
        for language_route_first in [true, false] {
            let path = project.path().join(format!("steps.{suffix}"));
            let language = language_for_path(&path).expect(suffix);
            let file = SourceFile { path, language };
            let suffix_routed = adapter_for_path(&file.path).expect(suffix);
            let language_routed = adapter_for_language(language);
            let mut session = SourceExtractionSession::new(project.path());
            let (first, second) = if language_route_first {
                (language_routed, suffix_routed)
            } else {
                (suffix_routed, language_routed)
            };
            let results = [
                first.extract_with_session(IMPORTED_STEP, &file, &mut session),
                second.extract_with_session(IMPORTED_STEP, &file, &mut session),
            ];
            let [first, second] = results;
            let extraction = first.unwrap();
            assert_eq!(&extraction, second.as_ref().unwrap(), "{suffix}");
            assert_eq!(extraction.definitions.len(), 1, "{suffix}");
            assert!(extraction.diagnostics.is_empty(), "{suffix}");
            // Every registered adapter is TypeScript-backed today, so one shared backend
            // state must serve all of them; a second backend is allowed to change this.
            assert_eq!(session.states.len(), 2, "{suffix}");
        }
    }
}

/// Checks legacy diagnostic classification follows message prefixes and responds to message mutation.
#[test]
fn legacy_diagnostics_preserve_message_classification_and_mutation() {
    for (message, incomplete, unparseable) in [
        ("ordinary warning", false, false),
        (UNRESOLVED_REGISTRATION_DIAGNOSTIC_PREFIX, true, false),
        (UNPARSEABLE_SOURCE_DIAGNOSTIC_PREFIX, true, true),
    ] {
        let mut diagnostic = ExtractionDiagnostic::new(
            ExtractionDiagnosticLevel::Warning,
            SourceLocation::new("steps.ts", 1, 1, 1, 1),
            message,
        );
        assert_eq!(is_completeness_diagnostic(&diagnostic), incomplete);
        assert_eq!(is_unparseable_diagnostic(&diagnostic), unparseable);
        diagnostic.message = "ordinary warning".into();
        assert!(!is_completeness_diagnostic(&diagnostic));
        assert!(!is_unparseable_diagnostic(&diagnostic));
    }
}

/// Checks uncertainty scope and cause selectively remove registrations or handler trust while preserving independent findings.
#[test]
fn finalization_scopes_preserve_independent_final_findings() {
    use crate::model::Rule;
    use UncertaintyCause::{Handler, Registration, Source};
    let project = session_project();
    let root = project.path();
    let mut session = SourceExtractionSession::new(root);
    let mut definitions = session_extract(
        &mut session,
        root,
        "ts",
        "Given('unrelated control', () => check()); Given('unrelated control', () => check());",
    )
    .definitions;
    definitions.extend(session_extract(&mut session, root, "js",
        "Given('opens red panel', () => work()); Given('opens red panel', () => work()); Given('opens blue panel', () => work());").definitions);
    let config = crate::config::Config::load(root, Default::default()).unwrap();
    let definition = UncertaintyScope::Definition(definitions[4].location.clone());
    let file = UncertaintyScope::File(root.join("steps.js"));
    let registry = UncertaintyScope::Registry(SourceLanguage::JavaScript);
    for (scope, cause, retained, matchers, handlers) in [
        (definition.clone(), Registration, 4, 2, 0),
        (file.clone(), Registration, 2, 1, 0),
        (definition, Handler, 5, 2, 0),
        (file, Handler, 5, 2, 0),
        (registry, Source, 5, 2, 2),
    ] {
        let mut actual = definitions.clone();
        let mut finalization = SourceFinalization::default();
        finalization
            .uncertainties
            .push(SourceUncertainty::new(scope, cause, "test evidence"));
        finalization.apply(&mut actual);
        assert_eq!(actual.len(), retained, "{finalization:?}");
        let result = crate::analysis::analyze(actual, vec![], &config).unwrap();
        for (rule, expected) in [
            (Rule::DuplicateMatcher, matchers),
            (Rule::DuplicateHandler, handlers),
        ] {
            let actual = result.findings.iter().filter(|f| f.rule == rule).count();
            assert_eq!(actual, expected, "{finalization:?}");
        }
    }
}

/// Checks registry uncertainty groups ECMAScript variants together while keeping Ruby findings independent.
#[test]
fn registry_uncertainty_isolates_languages_but_groups_ecmascript_variants() {
    use crate::model::Rule;
    let project = session_project();
    let root = project.path();
    let mut session = SourceExtractionSession::new(root);
    let mut definitions = Vec::new();
    for suffix in ["js", "ts", "tsx", "rb"] {
        let source = if suffix == "rb" {
            "Given('shared step') { work() }; Given('shared step') { work() }"
        } else {
            IMPORTED_STEP
        };
        definitions.extend(session_extract(&mut session, root, suffix, source).definitions);
    }
    let config = crate::config::Config::load(root, Default::default()).unwrap();
    for (language, retained, matcher_findings) in [
        (SourceLanguage::JavaScript, 2, 1),
        (SourceLanguage::TypeScript, 2, 1),
        (SourceLanguage::Tsx, 2, 1),
        (SourceLanguage::Ruby, 3, 2),
    ] {
        let mut actual = definitions.clone();
        let mut finalization = SourceFinalization::default();
        finalization.uncertainties.push(SourceUncertainty::new(
            UncertaintyScope::Registry(language),
            UncertaintyCause::Registration,
            "registry effect",
        ));
        finalization.apply(&mut actual);
        assert_eq!(actual.len(), retained, "{language:?}");
        let result = crate::analysis::analyze_with_diagnostics(actual, vec![], &config)
            .unwrap()
            .result;
        let rule = Rule::DuplicateMatcher;
        let actual = result.findings.iter().filter(|f| f.rule == rule).count();
        assert_eq!(actual, matcher_findings, "{language:?}");
    }
}

/// Checks session finalization applies late Ruby registration mutation in either order and remains idempotent.
#[test]
fn public_session_finalization_applies_late_ruby_effects_in_either_order() {
    for mutation_first in [false, true] {
        let project = session_project();
        let root = project.path();
        let mut session = SourceExtractionSession::new(root);
        let files: Vec<_> = ["registrations.rb", "mutation.rb"]
            .into_iter()
            .map(|name| SourceFile {
                path: root.join(name),
                language: SourceLanguage::Ruby,
            })
            .collect();
        let sources = [
            "Given('shared') { work() }; Then('shared') { work() }",
            "def Given(text); :replaced; end",
        ];
        let mut definitions = session_extract(&mut session, root, "ts", IMPORTED_STEP).definitions;
        for index in if mutation_first { [1, 0] } else { [0, 1] } {
            let extracted = adapter_for_language(SourceLanguage::Ruby)
                .extract_with_session(sources[index], &files[index], &mut session)
                .unwrap();
            definitions.extend(extracted.definitions);
        }
        if !mutation_first {
            assert_eq!(definitions.len(), 3);
        }
        let first = session.finalize(&files, &mut definitions).unwrap();
        assert!(!first.uncertainties.is_empty());
        assert_eq!(definitions.len(), 1);
        assert_eq!(definitions[0].location.path, root.join("steps.ts"));
        let once = definitions.clone();
        assert_eq!(session.finalize(&files, &mut definitions).unwrap(), first);
        assert_eq!(definitions, once);
    }
}