Skip to content

Commit

Permalink
Fix absolute path replacement (#463)
Browse files Browse the repository at this point in the history
* Fix absolute path replacement

* Fixup comment

* Add an additional test-case

Co-authored-by: Johannes Koch <53434855+johakoch@users.noreply.github.com>
  • Loading branch information
Marcel Ludwig and johakoch authored Apr 4, 2022
1 parent adb6162 commit 4c6ecad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
16 changes: 9 additions & 7 deletions config/configload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ func LoadFiles(filePath, dirPath string) (*config.Couper, error) {

parsedBodies = append([]*hclsyntax.Body{parsed.Body.(*hclsyntax.Body)}, parsedBodies...)
}

if filePath != "" {
filePath, err := filepath.Abs(filePath)
var err error
filePath, err = filepath.Abs(filePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,22 +174,22 @@ func LoadFiles(filePath, dirPath string) (*config.Couper, error) {
return nil, diags
}

settings := mergeAttributes("settings", parsedBodies)
settingsBlock := mergeAttributes(settings, parsedBodies)

definitions, err := mergeDefinitions(parsedBodies)
definitionsBlock, err := mergeDefinitions(parsedBodies)
if err != nil {
return nil, err
}

servers, err := mergeServers(parsedBodies)
serverBlocks, err := mergeServers(parsedBodies)
if err != nil {
return nil, err
}

configBlocks := servers
configBlocks = append(configBlocks, definitions)
configBlocks := serverBlocks
configBlocks = append(configBlocks, definitionsBlock)
configBlocks = append(configBlocks, defaults)
configBlocks = append(configBlocks, settings)
configBlocks = append(configBlocks, settingsBlock)

configBody := &hclsyntax.Body{
Blocks: configBlocks,
Expand Down
7 changes: 5 additions & 2 deletions config/configload/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path"
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand All @@ -22,10 +23,12 @@ func errorUniqueLabels(block *hclsyntax.Block) error {
}
}

// absPath replaces the given attribute path expression with an absolute path
// related to its filename if not already an absolute one.
func absPath(attr *hclsyntax.Attribute) hclsyntax.Expression {
value, diags := attr.Expr.Value(envContext)
if diags.HasErrors() {
return attr.Expr // Return unchanged in error cases.
if diags.HasErrors() || strings.Index(value.AsString(), "/") == 0 {
return attr.Expr // Return unchanged in error cases and for absolute path values.
}

return &hclsyntax.LiteralValueExpr{
Expand Down
47 changes: 47 additions & 0 deletions config/configload/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package configload

import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"testing"
)

func Test_absPath(t *testing.T) {
tests := []struct {
name string
attr *hclsyntax.Attribute
want cty.Value
}{
{"absolute path",
&hclsyntax.Attribute{Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("/www")}, SrcRange: hcl.Range{Filename: "/dir1/case1.hcl"}},
cty.StringVal("/www"),
},
{"relative path",
&hclsyntax.Attribute{Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("./www")}, SrcRange: hcl.Range{Filename: "/dir2/case2.hcl"}},
cty.StringVal("/dir2/www"),
},
{"relative parent dir path",
&hclsyntax.Attribute{Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("./../subDir1/www")}, SrcRange: hcl.Range{Filename: "/dir3/case3.hcl"}},
cty.StringVal("/subDir1/www"),
},
{"relative parent dir path 2",
&hclsyntax.Attribute{Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("./../../../../../subDir2/www")}, SrcRange: hcl.Range{Filename: "/dir4/case4.hcl"}},
cty.StringVal("/subDir2/www"),
},
{"relative path w/o dot",
&hclsyntax.Attribute{Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("www")}, SrcRange: hcl.Range{Filename: "/dir5/case5.hcl"}},
cty.StringVal("/dir5/www"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := absPath(tt.attr)
gotExpr := got.(*hclsyntax.LiteralValueExpr)
gotValue, _ := gotExpr.Value(envContext)
if gotValue.Equals(tt.want).False() {
t.Errorf("absPath() = %q, want %q", gotValue.AsString(), tt.want.AsString())
}
})
}
}

0 comments on commit 4c6ecad

Please sign in to comment.