Skip to content

Commit

Permalink
feat: add terramate.root.path.fs.basename/absolute (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
katcipis committed Jun 28, 2022
1 parent 7cc2426 commit 2dfccb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions generate/generate_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestGenerateFileRemoveFilesWhenConditionIsFalse(t *testing.T) {
assertFileDontExist(filename)
}

func TestGenerateFileTerramateMetadata(t *testing.T) {
func TestGenerateFileTerramateRootMetadata(t *testing.T) {
// We need to know the sandbox abspath to test terramate.root properly
const generatedFile = "file.hcl"

Expand All @@ -312,7 +312,7 @@ func TestGenerateFileTerramateMetadata(t *testing.T) {
hcldoc(
generateFile(
labels(generatedFile),
expr("content", "terramate.root.path.absolute"),
expr("content", `"${terramate.root.path.fs.absolute}-${terramate.root.path.fs.basename}"`),
),
).String(),
)
Expand All @@ -327,7 +327,7 @@ func TestGenerateFileTerramateMetadata(t *testing.T) {
},
})

want := s.RootDir()
want := s.RootDir() + "-" + filepath.Base(s.RootDir())
got := stackEntry.ReadFile(generatedFile)

assert.EqualStrings(t, want, got)
Expand Down
8 changes: 5 additions & 3 deletions generate/generate_hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func TestGenerateHCLCleanupOldFiles(t *testing.T) {
assertEqualStringList(t, got, []string{})
}

func TestGenerateHCLTerramateMetadata(t *testing.T) {
func TestGenerateHCLTerramateRootMetadata(t *testing.T) {
// We need to know the sandbox abspath to test terramate.root properly
const generatedFile = "file.hcl"

Expand All @@ -885,7 +885,8 @@ func TestGenerateHCLTerramateMetadata(t *testing.T) {
generateHCL(
labels(generatedFile),
content(
expr("terramate_root", "terramate.root.path.absolute"),
expr("terramate_root_path_abs", "terramate.root.path.fs.absolute"),
expr("terramate_root_path_basename", "terramate.root.path.fs.basename"),
),
),
).String(),
Expand All @@ -902,7 +903,8 @@ func TestGenerateHCLTerramateMetadata(t *testing.T) {
})

want := hcldoc(
str("terramate_root", s.RootDir()),
str("terramate_root_path_abs", s.RootDir()),
str("terramate_root_path_basename", filepath.Base(s.RootDir())),
).String()
got := stackEntry.ReadFile(generatedFile)

Expand Down
7 changes: 6 additions & 1 deletion stack/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package stack

import (
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2/hclsyntax"
Expand Down Expand Up @@ -84,8 +85,12 @@ func metaToCtyMap(rootdir string, m Metadata) map[string]cty.Value {
"description": cty.StringVal(m.Desc()),
"path": stackpath,
})
rootpath := eval.FromMapToObject(map[string]cty.Value{
rootfs := eval.FromMapToObject(map[string]cty.Value{
"absolute": cty.StringVal(rootdir),
"basename": cty.StringVal(filepath.Base(rootdir)),
})
rootpath := eval.FromMapToObject(map[string]cty.Value{
"fs": rootfs,
})
root := eval.FromMapToObject(map[string]cty.Value{
"path": rootpath,
Expand Down

0 comments on commit 2dfccb3

Please sign in to comment.