Bugfix include and exclude regexes
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
14576cde0e
commit
6f002f007d
2 changed files with 103 additions and 10 deletions
27
parser.go
27
parser.go
|
@ -23,17 +23,23 @@ type Parser struct {
|
||||||
|
|
||||||
// NewParser to create a new Parser instance
|
// NewParser to create a new Parser instance
|
||||||
func NewParser(includeRegex string, excludeRegex string) (*Parser, error) {
|
func NewParser(includeRegex string, excludeRegex string) (*Parser, error) {
|
||||||
|
var err error
|
||||||
|
var includeRegexCompiled, excludeRegexCompiled *regexp.Regexp
|
||||||
|
|
||||||
log.Debugf("compiling include name regex")
|
log.Debugf("compiling include name regex")
|
||||||
includeRegexCompiled, err := regexp.Compile(includeRegex)
|
if includeRegex != "" {
|
||||||
if err != nil {
|
includeRegexCompiled, err = regexp.Compile(includeRegex)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("compiling exclude name regex")
|
log.Debugf("compiling exclude name regex")
|
||||||
excludeRegexCompiled, err := regexp.Compile(excludeRegex)
|
if excludeRegex != "" {
|
||||||
if err != nil {
|
excludeRegexCompiled, err = regexp.Compile(excludeRegex)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("creating context with headless browser drivers")
|
log.Debugf("creating context with headless browser drivers")
|
||||||
|
@ -105,13 +111,14 @@ func (p *Parser) filterExclusive(products []*Product) []*Product {
|
||||||
var filtered []*Product
|
var filtered []*Product
|
||||||
if p.excludeRegex != nil {
|
if p.excludeRegex != nil {
|
||||||
for _, product := range products {
|
for _, product := range products {
|
||||||
if !p.excludeRegex.MatchString(product.Name) {
|
if p.excludeRegex.MatchString(product.Name) {
|
||||||
log.Debugf("product %s included because it matches does not match the exclude regex", product.Name)
|
|
||||||
filtered = append(filtered, product)
|
|
||||||
} else {
|
|
||||||
log.Debugf("product %s excluded because it matches the exclude regex", product.Name)
|
log.Debugf("product %s excluded because it matches the exclude regex", product.Name)
|
||||||
|
} else {
|
||||||
|
log.Debugf("product %s included because it does not match the exclude regex", product.Name)
|
||||||
|
filtered = append(filtered, product)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return filtered
|
||||||
}
|
}
|
||||||
return products
|
return products
|
||||||
}
|
}
|
||||||
|
|
86
parser_test.go
Normal file
86
parser_test.go
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFilterInclusive(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
regex string // inclusive regex
|
||||||
|
name string // product name
|
||||||
|
included bool // should be included or not
|
||||||
|
}{
|
||||||
|
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "MSI GeForce RTX 3060 GAMING X", true}, // 3060 in the include regex
|
||||||
|
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "ASUS AMD Radeon RX 5600 XT TUF Gaming X3", false}, // 5600 not in the include regex
|
||||||
|
{"", "MSI GeForce RTX 3060 GAMING X", true}, // do nothing when the include regex is empty
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range tests {
|
||||||
|
t.Run(fmt.Sprintf("TestFilterInclusive#%d", i), func(t *testing.T) {
|
||||||
|
p, err := NewParser(tc.regex, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to initialize parser: %s", err)
|
||||||
|
} else {
|
||||||
|
products := []*Product{{Name: tc.name}}
|
||||||
|
filtered := p.filterInclusive(products)
|
||||||
|
included := false
|
||||||
|
for _, product := range filtered {
|
||||||
|
if product.Name == tc.name && !included {
|
||||||
|
included = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tc.included != included {
|
||||||
|
t.Errorf("regex '%s' for product '%s': got included=%t, want included=%t", tc.regex, tc.name, included, tc.included)
|
||||||
|
} else {
|
||||||
|
if included {
|
||||||
|
t.Logf("regex '%s' includes product '%s'", tc.regex, tc.name)
|
||||||
|
} else {
|
||||||
|
t.Logf("regex '%s' excludes product '%s'", tc.regex, tc.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterExclusive(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
regex string // exclusive regex
|
||||||
|
name string // product name
|
||||||
|
included bool // should be included or not
|
||||||
|
}{
|
||||||
|
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "MSI GeForce RTX 3060 GAMING X", false}, // 3060 in the exclude regex
|
||||||
|
{"(?i)(rtx|rx)(.*)(3060|3070|3080|3090|5700|6800|6900)( )?(xt|ti)?", "ASUS AMD Radeon RX 5600 XT TUF Gaming X3", true}, // 5600 not in the exclude regex
|
||||||
|
{"", "MSI GeForce RTX 3060 GAMING X", true}, // do nothing when the exclude regex is empty
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range tests {
|
||||||
|
t.Run(fmt.Sprintf("TestFilterExclusive#%d", i), func(t *testing.T) {
|
||||||
|
p, err := NewParser("", tc.regex)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to initialize parser: %s", err)
|
||||||
|
} else {
|
||||||
|
products := []*Product{{Name: tc.name}}
|
||||||
|
filtered := p.filterExclusive(products)
|
||||||
|
included := false
|
||||||
|
for _, product := range filtered {
|
||||||
|
if product.Name == tc.name && !included {
|
||||||
|
included = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tc.included != included {
|
||||||
|
t.Errorf("regex '%s' for product '%s': got included=%t, want included=%t", tc.regex, tc.name, included, tc.included)
|
||||||
|
} else {
|
||||||
|
if included {
|
||||||
|
t.Logf("regex '%s' includes product '%s'", tc.regex, tc.name)
|
||||||
|
} else {
|
||||||
|
t.Logf("regex '%s' excludes product '%s'", tc.regex, tc.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue