Normalize HCL config paths in TestComplianceBinary/* tests on Windows (#3222)

Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
This commit is contained in:
Diógenes Fernandes
2025-09-04 13:59:50 -03:00
committed by GitHub
parent 13d8671db8
commit a88a1f004e
4 changed files with 30 additions and 7 deletions

View File

@@ -47,6 +47,8 @@ go 1.22`)
targetBinary := path.Join(dir, "testprovider")
if runtime.GOOS == "windows" {
targetBinary += ".exe"
// If we do not use raw backslashes below, the binary won't be found by HCL after parsing.
targetBinary = strings.ReplaceAll(targetBinary, "\\", `\\`)
}
t.Logf("\033[32mCompiling test provider binary...\033[0m")
cmd := exec.Command("go", "build", "-o", targetBinary)
@@ -72,6 +74,10 @@ func Python(t *testing.T) []string {
t.Errorf("Failed to create temporary directory (%v)", err)
}
target := path.Join(dir, "testprovider.py")
if runtime.GOOS == "windows" {
// If we do not use raw backslashes below, the binary won't be found by HCL after parsing.
target = strings.ReplaceAll(target, "\\", `\\`)
}
if err := ejectFile("testprovider.py", target); err != nil {
t.Errorf("%v", err)
}

View File

@@ -71,7 +71,6 @@ func (cfg *TestConfiguration[TDescriptor, TConfig, TMethod]) testHCL(t *testing.
hasValidHCLInvalidBuild := false
hasValidBuild := false
for name, tc := range cfg.HCLParseTestCases {
tc := tc
if !tc.ValidHCL {
hasInvalidHCL = true
} else {
@@ -109,7 +108,6 @@ func (cfg *TestConfiguration[TDescriptor, TConfig, TMethod]) testConfigStruct(t
}
for name, tc := range cfg.ConfigStructTestCases {
tc := tc
t.Run(name, func(t *testing.T) {
tc.execute(t)
})

View File

@@ -7,10 +7,12 @@ package external
import (
"fmt"
"runtime"
"slices"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/opentofu/opentofu/internal/encryption/keyprovider"
"github.com/opentofu/opentofu/internal/encryption/method/compliancetest"
"github.com/opentofu/opentofu/internal/encryption/method/external/testmethod"
@@ -53,11 +55,20 @@ func runTest(t *testing.T, cmd []string) {
ValidHCL: true,
ValidBuild: true,
Validate: func(config *Config, method *command) error {
if !slices.Equal(config.EncryptCommand, encryptCommand) {
return fmt.Errorf("incorrect encrypt command after HCL parsing")
// We need to normalize in order to match with the decoded config
if runtime.GOOS == "windows" {
for i, v := range encryptCommand {
encryptCommand[i] = strings.ReplaceAll(v, `\\`, "\\")
}
for i, v := range decryptCommand {
decryptCommand[i] = strings.ReplaceAll(v, `\\`, "\\")
}
}
if !slices.Equal(config.DecryptCommand, decryptCommand) {
return fmt.Errorf("incorrect decrypt command after HCL parsing")
if diff := cmp.Diff(config.EncryptCommand, encryptCommand); diff != "" {
return fmt.Errorf("incorrect encrypt command after HCL parsing: %s", diff)
}
if diff := cmp.Diff(config.DecryptCommand, decryptCommand); diff != "" {
return fmt.Errorf("incorrect decrypt command after HCL parsing: %s", diff)
}
return nil
},

View File

@@ -12,6 +12,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
@@ -42,9 +43,11 @@ go 1.22`)
if err := ejectFile("testmethod.go", path.Join(dir, "testmethod.go")); err != nil {
t.Errorf("%v", err)
}
targetBinary := path.Join(dir, "testmethod")
targetBinary := filepath.FromSlash(path.Join(dir, "testmethod"))
if runtime.GOOS == "windows" {
targetBinary += ".exe"
// If we do not use raw backslashes below, the binary won't be found by HCL after parsing.
targetBinary = strings.ReplaceAll(targetBinary, "\\", `\\`)
}
t.Logf("\033[32mCompiling test method binary...\033[0m")
cmd := exec.Command("go", "build", "-o", targetBinary)
@@ -69,10 +72,15 @@ func Python(t *testing.T) []string {
t.Errorf("Failed to create temporary directory (%v)", err)
}
target := path.Join(dir, "testmethod.py")
if runtime.GOOS == "windows" {
// If we do not use raw backslashes below, the binary won't be found by HCL after parsing.
target = strings.ReplaceAll(target, "\\", `\\`)
}
if err := ejectFile("testmethod.py", target); err != nil {
t.Errorf("%v", err)
}
python := findExecutable(t, []string{"python", "python3"}, []string{"--version"})
return []string{python, target}
}