Skip to content

Commit

Permalink
Fix typemap processing in config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rcowham committed Jul 5, 2023
1 parent c2bdc2f commit 99a8909
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func (c *Config) validate() error {
if !strings.Contains(ftype, "binary") && !strings.Contains(ftype, "text") {
return fmt.Errorf("typemaps must contain either 'binary' or 'text' in first part: %s", m)
}
reStr = strings.ReplaceAll(reStr, "...", ".*")
// Swap out "..." and then replace back with go equivalent
// Note we want to be sure we don't leave "." as will match any char
reStr = strings.ReplaceAll(reStr, "...", "\t")
reStr = strings.ReplaceAll(reStr, ".", "\\.")
reStr = strings.ReplaceAll(reStr, "*", "[^/]*")
reStr = strings.ReplaceAll(reStr, "\t", ".*")
reStr += "$"
if rePath, err := regexp.Compile(reStr); err != nil {
return fmt.Errorf("failed to parse '%s' as a regex", reStr)
Expand Down
27 changes: 26 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ typemaps:
assert.Equal(t, "text //....txt", cfg.TypeMaps[0])
assert.Equal(t, "binary //....bin", cfg.TypeMaps[1])
assert.True(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/file.txt"))
assert.True(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/fredtxt"))
assert.False(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/fredtxt")) // not .txt
assert.False(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/fred.txt1"))
assert.False(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/fred.bin"))
assert.True(t, cfg.ReTypeMaps[1].RePath.MatchString("//file.bin"))
assert.True(t, cfg.ReTypeMaps[1].RePath.MatchString("//some/file.bin"))
assert.False(t, cfg.ReTypeMaps[1].RePath.MatchString("//some/file_bin"))
}

func TestTypeMap2(t *testing.T) {
Expand All @@ -121,6 +122,30 @@ typemaps:
assert.Equal(t, "binary \"//....bin\"", cfg.TypeMaps[1])
}

func TestTypeMap3(t *testing.T) {
const config = `
typemaps:
- text //....c
- binary //some/*.bin
- binary //some/*/*.dat
`
cfg := loadOrFail(t, config)
checkValue(t, "ImportDepot", cfg.ImportDepot, "import")
checkValue(t, "ImportPath", cfg.ImportPath, "")
checkValue(t, "DefaultBranch", cfg.DefaultBranch, "main")
assert.Equal(t, 0, len(cfg.BranchMappings))
assert.Equal(t, 3, len(cfg.TypeMaps))
assert.Equal(t, "text //....c", cfg.TypeMaps[0])
assert.True(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/file.c"))
assert.True(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/path/file.c"))
assert.False(t, cfg.ReTypeMaps[0].RePath.MatchString("//some/file_c"))
assert.True(t, cfg.ReTypeMaps[1].RePath.MatchString("//some/file.bin"))
assert.False(t, cfg.ReTypeMaps[1].RePath.MatchString("//some/path/file.bin"))
assert.True(t, cfg.ReTypeMaps[2].RePath.MatchString("//some/path/file.dat"))
assert.False(t, cfg.ReTypeMaps[2].RePath.MatchString("//some/path/file_dat"))
assert.False(t, cfg.ReTypeMaps[2].RePath.MatchString("//some/file.dat"))
}

func TestRegex(t *testing.T) {
const config = `
branch_mappings:
Expand Down

0 comments on commit 99a8909

Please sign in to comment.