fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

query: fix expandFileContent (#483)

This fixes a bug where if s.FileName and s.Content were TRUE, we didn't
expand and the resulting matchTree was implicitly matching filenames
only.

Now we match content and filenames if s.Filename == s.Content.

+59 -2
+6 -2
query/query.go
··· 653 653 func ExpandFileContent(q Q) Q { 654 654 switch s := q.(type) { 655 655 case *Substring: 656 - if !s.FileName && !s.Content { 656 + if s.FileName == s.Content { 657 657 f := *s 658 658 f.FileName = true 659 + f.Content = false 659 660 c := *s 661 + c.FileName = false 660 662 c.Content = true 661 663 return NewOr(&f, &c) 662 664 } 663 665 case *Regexp: 664 - if !s.FileName && !s.Content { 666 + if s.FileName == s.Content { 665 667 f := *s 666 668 f.FileName = true 669 + f.Content = false 667 670 c := *s 671 + c.FileName = false 668 672 c.Content = true 669 673 return NewOr(&f, &c) 670 674 }
+53
query/query_test.go
··· 17 17 import ( 18 18 "log" 19 19 "reflect" 20 + "regexp/syntax" 20 21 "testing" 21 22 22 23 "github.com/grafana/regexp" ··· 111 112 t.Errorf("got %d, want 3", count) 112 113 } 113 114 } 115 + 116 + func TestExpandFileContent(t *testing.T) { 117 + re, _ := syntax.Parse("foo", syntax.Perl) 118 + 119 + cases := []struct { 120 + q Q 121 + want string 122 + }{ 123 + { 124 + q: &Substring{FileName: true, Content: true}, 125 + want: "(or file_substr:\"\" content_substr:\"\")", 126 + }, 127 + { 128 + q: &Substring{FileName: false, Content: false}, 129 + want: "(or file_substr:\"\" content_substr:\"\")", 130 + }, 131 + 132 + { 133 + q: &Substring{FileName: true, Content: false}, 134 + want: "file_substr:\"\"", 135 + }, 136 + { 137 + q: &Substring{FileName: false, Content: true}, 138 + want: "content_substr:\"\"", 139 + }, 140 + { 141 + q: &Regexp{Regexp: re, FileName: true, Content: true}, 142 + want: "(or file_regex:\"foo\" regex:\"foo\")", 143 + }, 144 + { 145 + q: &Regexp{Regexp: re, FileName: false, Content: false}, 146 + want: "(or file_regex:\"foo\" regex:\"foo\")", 147 + }, 148 + 149 + { 150 + q: &Regexp{Regexp: re, FileName: true, Content: false}, 151 + want: "file_regex:\"foo\"", 152 + }, 153 + { 154 + q: &Regexp{Regexp: re, FileName: false, Content: true}, 155 + want: "regex:\"foo\"", 156 + }, 157 + } 158 + 159 + for _, tt := range cases { 160 + t.Run("", func(t *testing.T) { 161 + if got := ExpandFileContent(tt.q); got.String() != tt.want { 162 + t.Fatalf("got %s, want %s\n", got.String(), tt.want) 163 + } 164 + }) 165 + } 166 + }