1
+ # Copyright Kani Contributors
2
+ # SPDX-License-Identifier: Apache-2.0 OR MIT
3
+
4
+ # Test that --include-pattern and --exclude-pattern work as expected when provided together.
5
+
6
+ set -e
7
+
8
+ # Define all function paths
9
+ FUNCTIONS=(
10
+ " foo::foo_function"
11
+ " foo::bar::bar_function"
12
+ " foo::bar::foo_bar_function"
13
+ " foo::baz::foo_baz_function"
14
+ " other::regular_function"
15
+ " other::with_bar_name"
16
+ " foo_top_level"
17
+ " bar_top_level"
18
+ )
19
+
20
+ # Check if a function appears in the "Selected Function" table
21
+ check_selected () {
22
+ local output=" $1 "
23
+ local function_name=" $2 "
24
+ if echo " $output " | grep -q " | $function_name *|" ; then
25
+ return 0
26
+ else
27
+ return 1
28
+ fi
29
+ }
30
+
31
+ # Check if a function appears in the "Skipped Function" table
32
+ check_skipped () {
33
+ local output=" $1 "
34
+ local function_name=" $2 "
35
+ if echo " $output " | grep -q " | $function_name *|.*Did not match provided filters" ; then
36
+ return 0
37
+ else
38
+ return 1
39
+ fi
40
+ }
41
+
42
+ # Check that the warning message gets printed for the patterns that are mutually exclusive (no functions get selected)
43
+ check_warning () {
44
+ local output=" $1 "
45
+ local should_warn=" $2 "
46
+ local warning_present=$( echo " $output " | grep -c " warning: Include pattern" || true)
47
+
48
+ if [ " $should_warn " = true ] && [ " $warning_present " -eq 0 ]; then
49
+ echo " ERROR: expected printed warning about conflicting --include-pattern and --exclude-pattern flags"
50
+ return 1
51
+ elif [ " $should_warn " = false ] && [ " $warning_present " -gt 0 ]; then
52
+ echo " ERROR: Got unexpected warning message"
53
+ return 1
54
+ fi
55
+ return 0
56
+ }
57
+
58
+
59
+ # Helper function to verify functions against include/exclude patterns
60
+ verify_functions () {
61
+ local output=" $1 "
62
+ local include_pattern=" $2 "
63
+ local exclude_pattern=" $3 "
64
+
65
+ for func in " ${FUNCTIONS[@]} " ; do
66
+ # If the function name matches the include pattern and not the exclude pattern, it should be selected
67
+ if echo " $func " | grep -q " $include_pattern " && ! echo " $func " | grep -q " $exclude_pattern " ; then
68
+ if ! check_selected " $output " " $func " ; then
69
+ echo " ERROR: Expected $func to be selected"
70
+ exit 1
71
+ fi
72
+ # Otherwise, it should be skipped
73
+ else
74
+ if ! check_skipped " $output " " $func " ; then
75
+ echo " ERROR: Expected $func to be skipped"
76
+ exit 1
77
+ fi
78
+ fi
79
+ done
80
+ }
81
+
82
+ # Test cases
83
+ test_cases=(
84
+ " include 'foo' exclude 'foo::bar'"
85
+ " include 'foo' exclude 'bar'"
86
+ " include 'foo::bar' exclude 'bar'"
87
+ " include 'foo' exclude 'foo'"
88
+ )
89
+
90
+ include_patterns=(
91
+ " foo"
92
+ " foo"
93
+ " foo::bar"
94
+ " foo"
95
+ )
96
+
97
+ exclude_patterns=(
98
+ " foo::bar"
99
+ " bar"
100
+ " bar"
101
+ " foo"
102
+ )
103
+
104
+ # Whether each test case should produce a warning about no functions being selected
105
+ should_warn=(
106
+ false
107
+ false
108
+ true
109
+ true
110
+ )
111
+
112
+ for i in " ${! test_cases[@]} " ; do
113
+ echo " Testing: ${test_cases[$i]} "
114
+ output=$( kani autoharness -Z autoharness src/lib.rs --include-pattern " ${include_patterns[$i]} " --exclude-pattern " ${exclude_patterns[$i]} " --only-codegen)
115
+ echo " $output "
116
+
117
+ if ! check_warning " $output " " ${should_warn[$i]} " ; then
118
+ exit 1
119
+ fi
120
+
121
+ verify_functions " $output " " ${include_patterns[$i]} " " ${exclude_patterns[$i]} "
122
+ done
123
+
124
+ echo " All tests passed!"
0 commit comments