Skip to content

Commit

Permalink
reduce func complexity; handle err; testmain handles missing log entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Jun 3, 2022
1 parent 319e1ba commit 6c61a0f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
79 changes: 45 additions & 34 deletions config/configload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func parseFile(filePath string, srcBytes *[][]byte) (*hclsyntax.Body, error) {
}

body := parsed.Body.(*hclsyntax.Body)
absolutizePaths(body)
return body, nil
err = absolutizePaths(body)
return body, err
}

func LoadFiles(filesList []string) (*config.Couper, error) {
Expand Down Expand Up @@ -475,45 +475,56 @@ func checkPermissionMixedConfig(apiConfig *config.API) error {
return nil
}

func absolutizePaths(fileBody *hclsyntax.Body) {
func absolutizePaths(fileBody *hclsyntax.Body) error {
visitor := func(node hclsyntax.Node) hcl.Diagnostics {
if attribute, ok := node.(*hclsyntax.Attribute); ok {
if _, exists := pathBearingAttributesMap[attribute.Name]; exists {
value, diags := attribute.Expr.Value(envContext)
if diags.HasErrors() {
return diags
}
attribute, ok := node.(*hclsyntax.Attribute)
if !ok {
return nil
}

filePath := value.AsString()
basePath := attribute.SrcRange.Filename
var absolutePath string
if attribute.Name == "jwks_url" {
if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") {
return nil
}
if strings.HasPrefix(filePath, "file:") {
filePath = filePath[5:]
}
if path.IsAbs(filePath) {
return nil
}
_, exists := pathBearingAttributesMap[attribute.Name]
if !exists {
return nil
}

absolutePath = "file:" + filepath.ToSlash(path.Join(filepath.Dir(basePath), filePath))
} else {
if filepath.IsAbs(filePath) {
return nil
}
absolutePath = filepath.Join(filepath.Dir(basePath), filePath)
}
value, diags := attribute.Expr.Value(envContext)
if diags.HasErrors() {
return diags
}

attribute.Expr = &hclsyntax.LiteralValueExpr{
Val: cty.StringVal(absolutePath),
SrcRange: attribute.SrcRange,
}
filePath := value.AsString()
basePath := attribute.SrcRange.Filename
var absolutePath string
if attribute.Name == "jwks_url" {
if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") {
return nil
}
if strings.HasPrefix(filePath, "file:") {
filePath = filePath[5:]
}
if path.IsAbs(filePath) {
return nil
}

absolutePath = "file:" + filepath.ToSlash(path.Join(filepath.Dir(basePath), filePath))
} else {
if filepath.IsAbs(filePath) {
return nil
}
absolutePath = filepath.Join(filepath.Dir(basePath), filePath)
}

attribute.Expr = &hclsyntax.LiteralValueExpr{
Val: cty.StringVal(absolutePath),
SrcRange: attribute.SrcRange,
}

return nil
}

hclsyntax.VisitAll(fileBody, visitor)
diags := hclsyntax.VisitAll(fileBody, visitor)
if diags.HasErrors() {
return diags
}
return nil
}
17 changes: 11 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ func Test_realmain(t *testing.T) {

currWD, _ := os.Getwd()

entry, _ := localHook.LastEntry().String()
//println(entry)
entry := localHook.LastEntry()
if entry == nil {
subT.Error("missing log entry")
return
}
entryStr, _ := entry.String()
//println(entryStr)

wantLog := tt.wantLog
if strings.Contains(wantLog, `msg="%s/`) {
Expand All @@ -73,15 +78,15 @@ func Test_realmain(t *testing.T) {
wantLog = fmt.Sprintf(wantLog, currWD)
}

if wantLog != "" && !strings.Contains(entry, wantLog) {
if wantLog != "" && !strings.Contains(entryStr, wantLog) {
if strings.Contains(wantLog, `failed to load configuration:`) {
re := regexp.MustCompile(wantLog)

if !re.MatchString(entry) {
subT.Errorf("\nwant:\t%s\ngot:\t%s\n", wantLog, entry)
if !re.MatchString(entryStr) {
subT.Errorf("\nwant:\t%s\ngot:\t%s\n", wantLog, entryStr)
}
} else {
subT.Errorf("\nwant:\t%s\ngot:\t%s\n", wantLog, entry)
subT.Errorf("\nwant:\t%s\ngot:\t%s\n", wantLog, entryStr)
}
}
})
Expand Down

0 comments on commit 6c61a0f

Please sign in to comment.