From 99a89098443a38f6e95e7dafc9005267d1a59627 Mon Sep 17 00:00:00 2001 From: Robert Cowham Date: Wed, 5 Jul 2023 13:26:06 +0100 Subject: [PATCH] Fix typemap processing in config loading --- config/config.go | 7 ++++++- config/config_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index be220f5..bff5da3 100644 --- a/config/config.go +++ b/config/config.go @@ -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) diff --git a/config/config_test.go b/config/config_test.go index c9cbbe3..53f5dba 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) { @@ -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: