mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-11 00:22:32 -06:00
Fix more tests on Windows (#1824)
Signed-off-by: eduzgun <emreduz00@gmail.com>
This commit is contained in:
parent
b6c31dfb87
commit
b93acf96a9
@ -145,7 +145,9 @@ func TestResourceProvider_CollectScripts_script(t *testing.T) {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if out.String() != expectedScriptOut {
|
||||
expectedOutput := normaliseNewlines(expectedScriptOut)
|
||||
actualOutput := normaliseNewlines(out.String())
|
||||
if actualOutput != expectedOutput {
|
||||
t.Fatalf("bad: %v", out.String())
|
||||
}
|
||||
}
|
||||
@ -181,7 +183,9 @@ func TestResourceProvider_CollectScripts_scripts(t *testing.T) {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if out.String() != expectedScriptOut {
|
||||
expectedOutput := normaliseNewlines(expectedScriptOut)
|
||||
actualOutput := normaliseNewlines(out.String())
|
||||
if actualOutput != expectedOutput {
|
||||
t.Fatalf("bad: %v", out.String())
|
||||
}
|
||||
}
|
||||
@ -323,3 +327,7 @@ func TestResourceProvisioner_nullsInOptionals(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func normaliseNewlines(input string) string {
|
||||
return strings.ReplaceAll(input, "\r\n", "\n")
|
||||
}
|
@ -11,6 +11,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -197,7 +198,8 @@ func setupBinary() func() {
|
||||
os.Exit(1)
|
||||
}
|
||||
// Getting top level dir
|
||||
dirPaths := strings.Split(currentDir, "/")
|
||||
newDir := filepath.ToSlash(currentDir)
|
||||
dirPaths := strings.Split(newDir, "/")
|
||||
log.Println(currentDir)
|
||||
topLevel := len(dirPaths) - 3
|
||||
topDir := strings.Join(dirPaths[0:topLevel], "/")
|
||||
|
@ -918,8 +918,8 @@ func TestNewDiagnostic(t *testing.T) {
|
||||
}
|
||||
|
||||
// Don't care about leading or trailing whitespace
|
||||
gotString := strings.TrimSpace(string(gotBytes))
|
||||
wantString := strings.TrimSpace(string(wantBytes))
|
||||
gotString := normaliseNewlines(strings.TrimSpace(string(gotBytes)))
|
||||
wantString := normaliseNewlines(strings.TrimSpace(string(wantBytes)))
|
||||
|
||||
if !cmp.Equal(wantString, gotString) {
|
||||
t.Fatalf("wrong result\n:%s", cmp.Diff(wantString, gotString))
|
||||
@ -928,6 +928,11 @@ func TestNewDiagnostic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Function to normalise newlines in a string for Windows
|
||||
func normaliseNewlines(s string) string {
|
||||
return strings.ReplaceAll(s, "\r\n", "\n")
|
||||
}
|
||||
|
||||
// Helper function to make constructing literal Diagnostics easier. There
|
||||
// are fields which are pointer-to-string to ensure that the rendered JSON
|
||||
// results in `null` for an empty value, rather than `""`.
|
||||
|
@ -8,6 +8,7 @@ package views
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -3565,6 +3566,11 @@ func runTestSaveErroredStateFile(t *testing.T, tc map[string]struct {
|
||||
// File does not exist
|
||||
t.Errorf("Expected state file 'errored_test.tfstate' to exist in: %s, but it does not.", tempDir)
|
||||
}
|
||||
// Trigger garbage collection to ensure that all open file handles are closed.
|
||||
// This prevents TempDir RemoveAll cleanup errors on Windows.
|
||||
if runtime.GOOS == "windows" {
|
||||
runtime.GC()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
@ -46,6 +47,9 @@ func TestLoadConfigWithSnapshot(t *testing.T) {
|
||||
"child_b.child_d": "testdata/already-installed/.terraform/modules/child_b.child_d",
|
||||
}
|
||||
|
||||
for key, module := range wantModuleDirs {
|
||||
wantModuleDirs[key] = filepath.Clean(module)
|
||||
}
|
||||
problems := deep.Equal(wantModuleDirs, gotModuleDirs)
|
||||
for _, problem := range problems {
|
||||
t.Errorf(problem)
|
||||
@ -57,7 +61,7 @@ func TestLoadConfigWithSnapshot(t *testing.T) {
|
||||
|
||||
gotRoot := got.Modules[""]
|
||||
wantRoot := &SnapshotModule{
|
||||
Dir: "testdata/already-installed",
|
||||
Dir: filepath.Join("testdata", "already-installed"),
|
||||
Files: map[string][]byte{
|
||||
"root.tf": []byte(`
|
||||
module "child_a" {
|
||||
@ -72,6 +76,10 @@ module "child_b" {
|
||||
`),
|
||||
},
|
||||
}
|
||||
// Normalise line endings and file paths for Windows
|
||||
for k, v := range gotRoot.Files {
|
||||
gotRoot.Files[k] = []byte(strings.ReplaceAll(string(v), "\r\n", "\n"))
|
||||
}
|
||||
if !reflect.DeepEqual(gotRoot, wantRoot) {
|
||||
t.Errorf("wrong root module snapshot\ngot: %swant: %s", spew.Sdump(gotRoot), spew.Sdump(wantRoot))
|
||||
}
|
||||
@ -126,7 +134,7 @@ func TestSnapshotRoundtrip(t *testing.T) {
|
||||
if config.Module == nil {
|
||||
t.Fatalf("config has no root module")
|
||||
}
|
||||
if got, want := config.Module.SourceDir, "testdata/already-installed"; got != want {
|
||||
if got, want := config.Module.SourceDir, filepath.Clean("testdata/already-installed"); got != want {
|
||||
t.Errorf("wrong root module sourcedir %q; want %q", got, want)
|
||||
}
|
||||
if got, want := len(config.Module.ModuleCalls), 2; got != want {
|
||||
@ -139,7 +147,7 @@ func TestSnapshotRoundtrip(t *testing.T) {
|
||||
if childA.Module == nil {
|
||||
t.Fatalf("child_a config has no module")
|
||||
}
|
||||
if got, want := childA.Module.SourceDir, "testdata/already-installed/.terraform/modules/child_a"; got != want {
|
||||
if got, want := childA.Module.SourceDir, filepath.Clean("testdata/already-installed/.terraform/modules/child_a"); got != want {
|
||||
t.Errorf("wrong child_a sourcedir %q; want %q", got, want)
|
||||
}
|
||||
if got, want := len(childA.Module.ModuleCalls), 1; got != want {
|
||||
|
@ -7,6 +7,7 @@ package planfile
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -189,10 +190,15 @@ func TestWrappedError(t *testing.T) {
|
||||
}
|
||||
|
||||
// Open something that doesn't exist: should error
|
||||
missingFile := "no such file or directory"
|
||||
var missingFileError string
|
||||
if runtime.GOOS == "windows" {
|
||||
missingFileError = "The system cannot find the file specified"
|
||||
} else {
|
||||
missingFileError = "no such file or directory"
|
||||
}
|
||||
_, err = OpenWrapped(filepath.Join("testdata", "absent.tfplan"), encryption.PlanEncryptionDisabled())
|
||||
if !strings.Contains(err.Error(), missingFile) {
|
||||
t.Fatalf("expected %q, got %q", missingFile, err)
|
||||
if !strings.Contains(err.Error(), missingFileError) {
|
||||
t.Fatalf("expected %q, got %q", missingFileError, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,11 @@
|
||||
package refactoring
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
@ -16,7 +21,54 @@ import (
|
||||
"github.com/opentofu/opentofu/internal/tfdiags"
|
||||
)
|
||||
|
||||
// Changes line endings from Windows-style ('\r\n') to Unix-style ('\n').
|
||||
func normaliseLineEndings(filename string) ([]byte, error) {
|
||||
originalContent, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading file %s: %w", filename, err)
|
||||
}
|
||||
|
||||
// Replace all occurrences of '\r\n' with '\n'
|
||||
normalisedContent := bytes.ReplaceAll(originalContent, []byte("\r\n"), []byte("\n"))
|
||||
|
||||
if !bytes.Equal(originalContent, normalisedContent) {
|
||||
err = os.WriteFile(filename, normalisedContent, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error writing file %s: %w", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
return originalContent, nil
|
||||
}
|
||||
|
||||
func TestImpliedMoveStatements(t *testing.T) {
|
||||
// Normalise file content for cross-platform compatibility
|
||||
if runtime.GOOS == "windows" {
|
||||
file1 := "testdata/move-statement-implied/move-statement-implied.tf"
|
||||
file2 := "testdata/move-statement-implied/child/move-statement-implied.tf"
|
||||
originalContent, err := normaliseLineEndings(file1)
|
||||
if err != nil {
|
||||
t.Errorf("Error normalising line endings %v", err)
|
||||
}
|
||||
originalContentChild, err := normaliseLineEndings(file2)
|
||||
if err != nil {
|
||||
t.Errorf("Error normalising line endings %v", err)
|
||||
}
|
||||
|
||||
// Restore original file content after test completion
|
||||
t.Cleanup(func() {
|
||||
err1 := os.WriteFile(file1, originalContent, 0600)
|
||||
if err1 != nil {
|
||||
t.Error()
|
||||
}
|
||||
err = os.WriteFile(file2, originalContentChild, 0600)
|
||||
if err != nil {
|
||||
t.Error()
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
resourceAddr := func(name string) addrs.AbsResource {
|
||||
return addrs.Resource{
|
||||
Mode: addrs.ManagedResourceMode,
|
||||
@ -124,7 +176,7 @@ func TestImpliedMoveStatements(t *testing.T) {
|
||||
To: addrs.ImpliedMoveStatementEndpoint(resourceAddr("formerly_count").Instance(addrs.NoKey), tfdiags.SourceRange{}),
|
||||
Implied: true,
|
||||
DeclRange: tfdiags.SourceRange{
|
||||
Filename: "testdata/move-statement-implied/move-statement-implied.tf",
|
||||
Filename: filepath.Join("testdata", "move-statement-implied", "move-statement-implied.tf"),
|
||||
Start: tfdiags.SourcePos{Line: 5, Column: 1, Byte: 180},
|
||||
End: tfdiags.SourcePos{Line: 5, Column: 32, Byte: 211},
|
||||
},
|
||||
@ -136,7 +188,7 @@ func TestImpliedMoveStatements(t *testing.T) {
|
||||
To: addrs.ImpliedMoveStatementEndpoint(nestedResourceAddr("child", "formerly_count").Instance(addrs.NoKey), tfdiags.SourceRange{}),
|
||||
Implied: true,
|
||||
DeclRange: tfdiags.SourceRange{
|
||||
Filename: "testdata/move-statement-implied/child/move-statement-implied.tf",
|
||||
Filename: filepath.Join("testdata", "move-statement-implied", "child", "move-statement-implied.tf"),
|
||||
Start: tfdiags.SourcePos{Line: 5, Column: 1, Byte: 180},
|
||||
End: tfdiags.SourcePos{Line: 5, Column: 32, Byte: 211},
|
||||
},
|
||||
@ -147,7 +199,7 @@ func TestImpliedMoveStatements(t *testing.T) {
|
||||
To: addrs.ImpliedMoveStatementEndpoint(resourceAddr("now_count").Instance(addrs.IntKey(0)), tfdiags.SourceRange{}),
|
||||
Implied: true,
|
||||
DeclRange: tfdiags.SourceRange{
|
||||
Filename: "testdata/move-statement-implied/move-statement-implied.tf",
|
||||
Filename: filepath.Join("testdata", "move-statement-implied", "move-statement-implied.tf"),
|
||||
Start: tfdiags.SourcePos{Line: 10, Column: 11, Byte: 282},
|
||||
End: tfdiags.SourcePos{Line: 10, Column: 12, Byte: 283},
|
||||
},
|
||||
@ -159,7 +211,7 @@ func TestImpliedMoveStatements(t *testing.T) {
|
||||
To: addrs.ImpliedMoveStatementEndpoint(nestedResourceAddr("child", "now_count").Instance(addrs.IntKey(0)), tfdiags.SourceRange{}),
|
||||
Implied: true,
|
||||
DeclRange: tfdiags.SourceRange{
|
||||
Filename: "testdata/move-statement-implied/child/move-statement-implied.tf",
|
||||
Filename: filepath.Join("testdata", "move-statement-implied", "child", "move-statement-implied.tf"),
|
||||
Start: tfdiags.SourcePos{Line: 10, Column: 11, Byte: 282},
|
||||
End: tfdiags.SourcePos{Line: 10, Column: 12, Byte: 283},
|
||||
},
|
||||
@ -175,7 +227,7 @@ func TestImpliedMoveStatements(t *testing.T) {
|
||||
To: addrs.ImpliedMoveStatementEndpoint(resourceAddr("ambiguous").Instance(addrs.NoKey), tfdiags.SourceRange{}),
|
||||
Implied: true,
|
||||
DeclRange: tfdiags.SourceRange{
|
||||
Filename: "testdata/move-statement-implied/move-statement-implied.tf",
|
||||
Filename: filepath.Join("testdata", "move-statement-implied", "move-statement-implied.tf"),
|
||||
Start: tfdiags.SourcePos{Line: 46, Column: 1, Byte: 806},
|
||||
End: tfdiags.SourcePos{Line: 46, Column: 27, Byte: 832},
|
||||
},
|
||||
|
@ -7,6 +7,7 @@ package refactoring
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -517,7 +518,8 @@ A chain of move statements must end with an address that doesn't appear in any o
|
||||
if !gotDiags.HasErrors() {
|
||||
t.Fatalf("unexpected success\nwant error: %s", test.WantError)
|
||||
}
|
||||
if got, want := gotDiags.Err().Error(), test.WantError; got != want {
|
||||
normalisedErr := filepath.ToSlash(gotDiags.Err().Error())
|
||||
if got, want := normalisedErr, test.WantError; got != want {
|
||||
t.Fatalf("wrong error\ngot error: %s\nwant error: %s", got, want)
|
||||
}
|
||||
default:
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -3417,9 +3418,9 @@ func TestContext2Plan_pathVar(t *testing.T) {
|
||||
t.Fatalf("resource %s should be created", i)
|
||||
}
|
||||
checkVals(t, objectVal(t, schema, map[string]cty.Value{
|
||||
"cwd": cty.StringVal(cwd + "/barpath"),
|
||||
"module": cty.StringVal(m.Module.SourceDir + "/foopath"),
|
||||
"root": cty.StringVal(m.Module.SourceDir + "/barpath"),
|
||||
"cwd": cty.StringVal(filepath.ToSlash(cwd + "/barpath")),
|
||||
"module": cty.StringVal(filepath.ToSlash(m.Module.SourceDir + "/foopath")),
|
||||
"root": cty.StringVal(filepath.ToSlash(m.Module.SourceDir + "/barpath")),
|
||||
}), ric.After)
|
||||
default:
|
||||
t.Fatal("unknown instance:", i)
|
||||
|
Loading…
Reference in New Issue
Block a user