shuck-semantic 0.0.40

Semantic analysis model for shell scripts with scopes, bindings, and dataflow
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use crate::{OptionValue, ShellBehaviorAt, ShellDialect};

/// Whether unquoted expansion results are subject to field splitting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldSplittingBehavior {
    /// Field splitting does not apply.
    Never,
    /// Field splitting applies only when the expansion is unquoted.
    UnquotedOnly,
    /// Runtime option state may select either behavior.
    Ambiguous,
}

impl FieldSplittingBehavior {
    /// Returns whether an unquoted expansion result may be split into fields.
    pub fn unquoted_results_can_split(self) -> bool {
        matches!(self, Self::UnquotedOnly | Self::Ambiguous)
    }
}

/// Whether pathname expansion applies to literal globs and substitution results.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathnameExpansionBehavior {
    /// Pathname expansion does not apply.
    Disabled,
    /// Literal glob characters are active, but substitution results are not globbed.
    LiteralGlobsOnly,
    /// Runtime option state may enable literal glob expansion, but substitution results stay plain.
    LiteralGlobsOnlyOrDisabled,
    /// Unquoted substitution results can also trigger pathname expansion.
    SubstitutionResultsWhenUnquoted,
    /// Runtime option state may select either behavior family.
    Ambiguous,
}

impl PathnameExpansionBehavior {
    /// Returns whether literal glob characters may trigger pathname expansion.
    pub fn literal_globs_can_expand(self) -> bool {
        !matches!(self, Self::Disabled)
    }

    /// Returns whether unquoted substitution results may be interpreted as globs.
    pub fn unquoted_substitution_results_can_glob(self) -> bool {
        matches!(
            self,
            Self::SubstitutionResultsWhenUnquoted | Self::Ambiguous
        )
    }
}

/// Whether zsh filename expansion runs before or after parameter expansion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileExpansionOrderBehavior {
    /// Filename expansion runs before parameter expansion.
    BeforeParameterExpansion,
    /// Filename expansion runs after parameter expansion.
    AfterParameterExpansion,
    /// Runtime option state may select either order.
    Ambiguous,
}

/// How unmatched glob patterns behave.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlobFailureBehavior {
    /// An unmatched glob produces an error.
    ErrorOnNoMatch,
    /// An unmatched glob stays literal in argv.
    KeepLiteralOnNoMatch,
    /// An unmatched glob is removed from argv.
    DropUnmatchedPattern,
    /// Unmatched globs are removed unless every glob in the command misses.
    CshNullGlob,
    /// Runtime option state may select either behavior.
    Ambiguous,
}

/// Whether glob patterns match dot-prefixed path segments without an explicit dot.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlobDotBehavior {
    /// Dot-prefixed names require an explicit dot in the pattern.
    ExplicitDotRequired,
    /// Dot-prefixed names may be matched by ordinary glob metacharacters.
    DotfilesIncluded,
    /// Runtime option state may select either behavior.
    Ambiguous,
}

/// Whether a zsh pattern-operator family is active.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatternOperatorBehavior {
    /// The operator family is disabled.
    Disabled,
    /// The operator family is enabled.
    Enabled,
    /// Runtime option state may select either behavior.
    Ambiguous,
}

/// Whether zsh brace character-class expansion is active.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BraceCharacterClassBehavior {
    /// Brace character classes are not expanded.
    Disabled,
    /// Brace character classes are expanded.
    Enabled,
    /// Runtime option state may select either behavior.
    Ambiguous,
}

impl BraceCharacterClassBehavior {
    /// Returns whether a brace character class may expand at runtime.
    pub fn can_expand(self) -> bool {
        matches!(self, Self::Enabled | Self::Ambiguous)
    }
}

/// Option-sensitive pattern operators available to glob and shell-pattern facts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlobPatternBehavior {
    extended_glob: PatternOperatorBehavior,
    ksh_glob: PatternOperatorBehavior,
    sh_glob: PatternOperatorBehavior,
}

impl GlobPatternBehavior {
    /// Creates a pattern behavior from its operator-family states.
    pub const fn from_parts(
        extended_glob: PatternOperatorBehavior,
        ksh_glob: PatternOperatorBehavior,
        sh_glob: PatternOperatorBehavior,
    ) -> Self {
        Self {
            extended_glob,
            ksh_glob,
            sh_glob,
        }
    }

    /// Returns whether zsh extended glob operators such as `#`, `~`, and `^` are active.
    pub fn extended_glob(self) -> PatternOperatorBehavior {
        self.extended_glob
    }

    /// Returns whether ksh-style glob operators such as `@(...)` are active.
    pub fn ksh_glob(self) -> PatternOperatorBehavior {
        self.ksh_glob
    }

    /// Returns whether sh-compatible glob parsing is active.
    pub fn sh_glob(self) -> PatternOperatorBehavior {
        self.sh_glob
    }
}

impl ShellBehaviorAt<'_> {
    /// Returns the field-splitting behavior implied by the shell and runtime option state.
    pub fn field_splitting(&self) -> FieldSplittingBehavior {
        if self.shell != ShellDialect::Zsh {
            return FieldSplittingBehavior::UnquotedOnly;
        }

        match self
            .effective_zsh_options()
            .map(|options| options.sh_word_split)
        {
            Some(OptionValue::Off) => FieldSplittingBehavior::Never,
            Some(OptionValue::Unknown) => FieldSplittingBehavior::Ambiguous,
            Some(OptionValue::On) | None => FieldSplittingBehavior::UnquotedOnly,
        }
    }

    /// Returns the pathname-expansion behavior implied by the shell and runtime option state.
    pub fn pathname_expansion(&self) -> PathnameExpansionBehavior {
        if self.shell != ShellDialect::Zsh {
            return PathnameExpansionBehavior::SubstitutionResultsWhenUnquoted;
        }

        let Some(options) = self.effective_zsh_options() else {
            return PathnameExpansionBehavior::LiteralGlobsOnly;
        };

        match options.glob {
            OptionValue::Off => PathnameExpansionBehavior::Disabled,
            OptionValue::Unknown => match options.glob_subst {
                OptionValue::Off => PathnameExpansionBehavior::LiteralGlobsOnlyOrDisabled,
                OptionValue::On | OptionValue::Unknown => PathnameExpansionBehavior::Ambiguous,
            },
            OptionValue::On => match options.glob_subst {
                OptionValue::Off => PathnameExpansionBehavior::LiteralGlobsOnly,
                OptionValue::On => PathnameExpansionBehavior::SubstitutionResultsWhenUnquoted,
                OptionValue::Unknown => PathnameExpansionBehavior::Ambiguous,
            },
        }
    }

    /// Returns the filename-expansion order implied by the shell and runtime option state.
    pub fn file_expansion_order(&self) -> FileExpansionOrderBehavior {
        if self.shell != ShellDialect::Zsh {
            return FileExpansionOrderBehavior::BeforeParameterExpansion;
        }

        match self
            .effective_zsh_options()
            .map(|options| options.sh_file_expansion)
        {
            Some(OptionValue::On) => FileExpansionOrderBehavior::BeforeParameterExpansion,
            Some(OptionValue::Unknown) => FileExpansionOrderBehavior::Ambiguous,
            Some(OptionValue::Off) | None => FileExpansionOrderBehavior::AfterParameterExpansion,
        }
    }

    /// Returns the unmatched-glob behavior implied by the shell and runtime option state.
    pub fn glob_failure(&self) -> GlobFailureBehavior {
        if self.shell != ShellDialect::Zsh {
            return GlobFailureBehavior::KeepLiteralOnNoMatch;
        }

        let Some(options) = self.effective_zsh_options() else {
            return GlobFailureBehavior::ErrorOnNoMatch;
        };

        match options.glob {
            OptionValue::Off => GlobFailureBehavior::KeepLiteralOnNoMatch,
            OptionValue::Unknown => GlobFailureBehavior::Ambiguous,
            OptionValue::On => match options.csh_null_glob {
                OptionValue::On => GlobFailureBehavior::CshNullGlob,
                OptionValue::Unknown => GlobFailureBehavior::Ambiguous,
                OptionValue::Off => match options.null_glob {
                    OptionValue::On => GlobFailureBehavior::DropUnmatchedPattern,
                    OptionValue::Unknown => GlobFailureBehavior::Ambiguous,
                    OptionValue::Off => match options.nomatch {
                        OptionValue::On => GlobFailureBehavior::ErrorOnNoMatch,
                        OptionValue::Off => GlobFailureBehavior::KeepLiteralOnNoMatch,
                        OptionValue::Unknown => GlobFailureBehavior::Ambiguous,
                    },
                },
            },
        }
    }

    /// Returns how glob patterns treat dot-prefixed path segments.
    pub fn glob_dot_matching(&self) -> GlobDotBehavior {
        if self.shell != ShellDialect::Zsh {
            return GlobDotBehavior::ExplicitDotRequired;
        }

        match self
            .effective_zsh_options()
            .map(|options| options.glob_dots)
        {
            Some(OptionValue::On) => GlobDotBehavior::DotfilesIncluded,
            Some(OptionValue::Unknown) => GlobDotBehavior::Ambiguous,
            Some(OptionValue::Off) | None => GlobDotBehavior::ExplicitDotRequired,
        }
    }

    /// Returns the option-sensitive glob/pattern operator families available at this offset.
    pub fn glob_pattern(&self) -> GlobPatternBehavior {
        if self.shell != ShellDialect::Zsh {
            return GlobPatternBehavior::from_parts(
                PatternOperatorBehavior::Disabled,
                PatternOperatorBehavior::Disabled,
                PatternOperatorBehavior::Disabled,
            );
        }

        let Some(options) = self.effective_zsh_options() else {
            return GlobPatternBehavior::from_parts(
                PatternOperatorBehavior::Disabled,
                PatternOperatorBehavior::Disabled,
                PatternOperatorBehavior::Disabled,
            );
        };

        GlobPatternBehavior::from_parts(
            pattern_operator_behavior(options.extended_glob),
            pattern_operator_behavior(options.ksh_glob),
            pattern_operator_behavior(options.sh_glob),
        )
    }

    /// Returns whether zsh brace character classes such as `{abc}` are active.
    pub fn brace_character_classes(&self) -> BraceCharacterClassBehavior {
        if self.shell != ShellDialect::Zsh {
            return BraceCharacterClassBehavior::Disabled;
        }

        match self
            .effective_zsh_options()
            .map(|options| options.brace_ccl)
        {
            Some(OptionValue::On) => BraceCharacterClassBehavior::Enabled,
            Some(OptionValue::Unknown) => BraceCharacterClassBehavior::Ambiguous,
            Some(OptionValue::Off) | None => BraceCharacterClassBehavior::Disabled,
        }
    }
}

fn pattern_operator_behavior(value: OptionValue) -> PatternOperatorBehavior {
    match value {
        OptionValue::Off => PatternOperatorBehavior::Disabled,
        OptionValue::On => PatternOperatorBehavior::Enabled,
        OptionValue::Unknown => PatternOperatorBehavior::Ambiguous,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{SemanticBuildOptions, SemanticModel, ShellProfile};
    use shuck_indexer::Indexer;
    use shuck_parser::parser::Parser;

    fn model_with_profile(source: &str, profile: ShellProfile) -> SemanticModel {
        let output = Parser::with_profile(source, profile.clone())
            .parse()
            .unwrap();
        let indexer = Indexer::new(source, &output);
        SemanticModel::build_with_options(
            &output.file,
            source,
            &indexer,
            SemanticBuildOptions {
                shell_profile: Some(profile),
                ..SemanticBuildOptions::default()
            },
        )
    }

    #[test]
    fn tracks_field_splitting_by_offset() {
        for (source, expected) in [
            ("print $name\n", FieldSplittingBehavior::Never),
            (
                "setopt sh_word_split\nprint $name\n",
                FieldSplittingBehavior::UnquotedOnly,
            ),
            (
                "opt=sh_word_split\nsetopt \"$opt\"\nprint $name\n",
                FieldSplittingBehavior::Ambiguous,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).field_splitting(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn uses_non_zsh_default_behaviors() {
        let source = "printf '%s\\n' $name *.txt\n";
        let model = model_with_profile(source, ShellProfile::native(ShellDialect::Bash));
        let offset = source.find("printf").expect("expected printf offset");
        let behavior = model.shell_behavior_at(offset);

        assert_eq!(
            behavior.field_splitting(),
            FieldSplittingBehavior::UnquotedOnly
        );
        assert_eq!(
            behavior.pathname_expansion(),
            PathnameExpansionBehavior::SubstitutionResultsWhenUnquoted
        );
        assert_eq!(
            behavior.glob_failure(),
            GlobFailureBehavior::KeepLiteralOnNoMatch
        );
        assert_eq!(
            behavior.glob_dot_matching(),
            GlobDotBehavior::ExplicitDotRequired
        );
        let pattern = behavior.glob_pattern();
        assert_eq!(pattern.extended_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(pattern.ksh_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(pattern.sh_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(
            behavior.brace_character_classes(),
            BraceCharacterClassBehavior::Disabled
        );
    }

    #[test]
    fn tracks_brace_character_classes_by_offset() {
        for (source, expected) in [
            ("print {app}\n", BraceCharacterClassBehavior::Disabled),
            (
                "setopt brace_ccl\nprint {app}\n",
                BraceCharacterClassBehavior::Enabled,
            ),
            (
                "setopt brace_ccl\nunsetopt brace_ccl\nprint {app}\n",
                BraceCharacterClassBehavior::Disabled,
            ),
            (
                "opt=brace_ccl\nsetopt \"$opt\"\nprint {app}\n",
                BraceCharacterClassBehavior::Ambiguous,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).brace_character_classes(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn tracks_pathname_expansion_by_offset() {
        for (source, expected) in [
            ("print $name\n", PathnameExpansionBehavior::LiteralGlobsOnly),
            (
                "setopt glob_subst\nprint $name\n",
                PathnameExpansionBehavior::SubstitutionResultsWhenUnquoted,
            ),
            (
                "setopt no_glob\nprint $name\n",
                PathnameExpansionBehavior::Disabled,
            ),
            (
                "opt=glob_subst\nsetopt \"$opt\"\nprint $name\n",
                PathnameExpansionBehavior::Ambiguous,
            ),
            (
                "if cond; then setopt no_glob; fi\nprint $name\n",
                PathnameExpansionBehavior::LiteralGlobsOnlyOrDisabled,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).pathname_expansion(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn pathname_expansion_respects_no_glob_precedence() {
        for (source, expected) in [
            (
                "setopt no_glob glob_subst\nprint $name\n",
                PathnameExpansionBehavior::Disabled,
            ),
            (
                "setopt glob_subst\nprint $name\n",
                PathnameExpansionBehavior::SubstitutionResultsWhenUnquoted,
            ),
            (
                "unsetopt glob_subst\nprint $name\n",
                PathnameExpansionBehavior::LiteralGlobsOnly,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).pathname_expansion(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn file_expansion_order_tracks_sh_file_expansion_by_offset() {
        let source = "\
print ~$USER
setopt sh_file_expansion
print ~$USER
unsetopt sh_file_expansion
print ~$USER
opt=sh_file_expansion
setopt \"$opt\"
print ~$USER
";
        let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));

        let first = source.find("print ~$USER").unwrap();
        let enabled = source.find("print ~$USER\nunsetopt").unwrap();
        let disabled = source.find("print ~$USER\nopt=").unwrap();
        let ambiguous = source.rfind("print ~$USER").unwrap();

        assert_eq!(
            model.shell_behavior_at(first).file_expansion_order(),
            FileExpansionOrderBehavior::AfterParameterExpansion,
        );
        assert_eq!(
            model.shell_behavior_at(enabled).file_expansion_order(),
            FileExpansionOrderBehavior::BeforeParameterExpansion,
        );
        assert_eq!(
            model.shell_behavior_at(disabled).file_expansion_order(),
            FileExpansionOrderBehavior::AfterParameterExpansion,
        );
        assert_eq!(
            model.shell_behavior_at(ambiguous).file_expansion_order(),
            FileExpansionOrderBehavior::Ambiguous,
        );
    }

    #[test]
    fn pathname_expansion_keeps_glob_subst_off_when_glob_is_flow_merged() {
        let source = "\
if cond; then
  setopt no_glob
fi
print $name
";
        let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
        let offset = source.find("print").expect("expected print offset");
        let behavior = model.shell_behavior_at(offset).pathname_expansion();

        assert_eq!(
            behavior,
            PathnameExpansionBehavior::LiteralGlobsOnlyOrDisabled
        );
        assert!(behavior.literal_globs_can_expand());
        assert!(!behavior.unquoted_substitution_results_can_glob());
    }

    #[test]
    fn tracks_function_leaked_option_effects_by_offset() {
        let source = "\
fn() {
  setopt sh_word_split glob_subst
}
fn
print $name
";
        let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
        let offset = source.rfind("print").expect("expected print offset");

        assert_eq!(
            model.shell_behavior_at(offset).field_splitting(),
            FieldSplittingBehavior::UnquotedOnly
        );
        assert_eq!(
            model.shell_behavior_at(offset).pathname_expansion(),
            PathnameExpansionBehavior::SubstitutionResultsWhenUnquoted
        );
        assert_eq!(
            model.shell_behavior_at(offset).glob_failure(),
            GlobFailureBehavior::ErrorOnNoMatch
        );
    }

    #[test]
    fn tracks_glob_failure_modes_by_offset() {
        for (source, expected) in [
            ("print *\n", GlobFailureBehavior::ErrorOnNoMatch),
            (
                "setopt no_nomatch\nprint *\n",
                GlobFailureBehavior::KeepLiteralOnNoMatch,
            ),
            (
                "setopt null_glob\nprint *\n",
                GlobFailureBehavior::DropUnmatchedPattern,
            ),
            (
                "setopt csh_null_glob\nprint *\n",
                GlobFailureBehavior::CshNullGlob,
            ),
            (
                "opt=no_nomatch\nsetopt \"$opt\"\nprint *\n",
                GlobFailureBehavior::Ambiguous,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).glob_failure(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn glob_failure_respects_option_precedence() {
        for (source, expected) in [
            (
                "setopt no_glob null_glob csh_null_glob\nprint *\n",
                GlobFailureBehavior::KeepLiteralOnNoMatch,
            ),
            (
                "setopt null_glob csh_null_glob\nprint *\n",
                GlobFailureBehavior::CshNullGlob,
            ),
            (
                "setopt null_glob\nunsetopt csh_null_glob\nprint *\n",
                GlobFailureBehavior::DropUnmatchedPattern,
            ),
            (
                "setopt no_nomatch null_glob csh_null_glob\nprint *\n",
                GlobFailureBehavior::CshNullGlob,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).glob_failure(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn tracks_glob_dot_matching_by_offset() {
        for (source, expected) in [
            ("print *\n", GlobDotBehavior::ExplicitDotRequired),
            (
                "setopt glob_dots\nprint *\n",
                GlobDotBehavior::DotfilesIncluded,
            ),
            (
                "setopt glob_dots\nunsetopt glob_dots\nprint *\n",
                GlobDotBehavior::ExplicitDotRequired,
            ),
            (
                "opt=glob_dots\nsetopt \"$opt\"\nprint *\n",
                GlobDotBehavior::Ambiguous,
            ),
        ] {
            let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
            let offset = source.find("print").expect("expected print offset");

            assert_eq!(
                model.shell_behavior_at(offset).glob_dot_matching(),
                expected,
                "{source}"
            );
        }
    }

    #[test]
    fn tracks_glob_pattern_operator_families_by_offset() {
        let source = "\
print *
setopt extended_glob ksh_glob sh_glob
print *
unsetopt extended_glob
print *
unsetopt ksh_glob
print *
unsetopt sh_glob
print *
opt=extended_glob
unsetopt \"$opt\"
print *
";
        let model = model_with_profile(source, ShellProfile::native(ShellDialect::Zsh));
        let mut print_offsets = source.match_indices("print").map(|(offset, _)| offset);

        let native = model
            .shell_behavior_at(print_offsets.next().expect("expected native print"))
            .glob_pattern();
        assert_eq!(native.extended_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(native.ksh_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(native.sh_glob(), PatternOperatorBehavior::Disabled);

        let enabled = model
            .shell_behavior_at(print_offsets.next().expect("expected enabled print"))
            .glob_pattern();
        assert_eq!(enabled.extended_glob(), PatternOperatorBehavior::Enabled);
        assert_eq!(enabled.ksh_glob(), PatternOperatorBehavior::Enabled);
        assert_eq!(enabled.sh_glob(), PatternOperatorBehavior::Enabled);

        let only_ksh_and_sh = model
            .shell_behavior_at(print_offsets.next().expect("expected partial print"))
            .glob_pattern();
        assert_eq!(
            only_ksh_and_sh.extended_glob(),
            PatternOperatorBehavior::Disabled
        );
        assert_eq!(only_ksh_and_sh.ksh_glob(), PatternOperatorBehavior::Enabled);
        assert_eq!(only_ksh_and_sh.sh_glob(), PatternOperatorBehavior::Enabled);

        let only_sh = model
            .shell_behavior_at(print_offsets.next().expect("expected sh print"))
            .glob_pattern();
        assert_eq!(only_sh.extended_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(only_sh.ksh_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(only_sh.sh_glob(), PatternOperatorBehavior::Enabled);

        let disabled_again = model
            .shell_behavior_at(print_offsets.next().expect("expected disabled print"))
            .glob_pattern();
        assert_eq!(
            disabled_again.extended_glob(),
            PatternOperatorBehavior::Disabled
        );
        assert_eq!(disabled_again.ksh_glob(), PatternOperatorBehavior::Disabled);
        assert_eq!(disabled_again.sh_glob(), PatternOperatorBehavior::Disabled);

        let ambiguous = model
            .shell_behavior_at(print_offsets.next().expect("expected ambiguous print"))
            .glob_pattern();
        assert_eq!(
            ambiguous.extended_glob(),
            PatternOperatorBehavior::Ambiguous
        );
        assert_eq!(ambiguous.ksh_glob(), PatternOperatorBehavior::Ambiguous);
        assert_eq!(ambiguous.sh_glob(), PatternOperatorBehavior::Ambiguous);
    }
}