mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-20 02:09:26 -05:00
Go 1.17 and earlier used a different syntax for build constraint comments, starting with "+build". Go 1.18 changed this to the modern "go:build" form as part of standardizing the structure of toolchain directive comments, and so for a while it was convention to include comments in both styles to allow building with both old and new Go compilers. However, Go 1.17 is no longer supported, and regardless of that we only expect OpenTofu to be built with the specific version we have selected in "go.mod" and ".go-version" anyway, so we no longer need the legacy form of these comments: the all supported Go toolchains now support the new form, which this commit retains. golangci-lint v2.6.0 includes a check for this legacy form, so removing this will also allow us to upgrade to a newer version of that linter aggregator in a future commit. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
154 lines
3.9 KiB
Go
154 lines
3.9 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
//go:build !windows
|
|
|
|
package cliconfig
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestConfigFileConfigDir(t *testing.T) {
|
|
homeDir := filepath.Join(t.TempDir(), "home")
|
|
cl := standardConfigLoader()
|
|
|
|
tests := []struct {
|
|
name string
|
|
xdgConfigHome string
|
|
files []string
|
|
testFunc func() (string, error)
|
|
expect string
|
|
}{
|
|
{
|
|
name: "configFile: use home tofurc",
|
|
testFunc: cl.configFile,
|
|
files: []string{filepath.Join(homeDir, ".tofurc")},
|
|
expect: filepath.Join(homeDir, ".tofurc"),
|
|
},
|
|
{
|
|
name: "configFile: use home terraformrc",
|
|
testFunc: cl.configFile,
|
|
files: []string{filepath.Join(homeDir, ".terraformrc")},
|
|
expect: filepath.Join(homeDir, ".terraformrc"),
|
|
},
|
|
{
|
|
name: "configFile: use default fallback",
|
|
testFunc: cl.configFile,
|
|
expect: filepath.Join(homeDir, ".tofurc"),
|
|
},
|
|
{
|
|
name: "configFile: use XDG tofurc",
|
|
testFunc: cl.configFile,
|
|
xdgConfigHome: filepath.Join(homeDir, "xdg"),
|
|
expect: filepath.Join(homeDir, "xdg", "opentofu", "tofurc"),
|
|
},
|
|
{
|
|
name: "configFile: prefer home tofurc",
|
|
testFunc: cl.configFile,
|
|
xdgConfigHome: filepath.Join(homeDir, "xdg"),
|
|
files: []string{filepath.Join(homeDir, ".tofurc")},
|
|
expect: filepath.Join(homeDir, ".tofurc"),
|
|
},
|
|
{
|
|
name: "configFile: prefer home terraformrc",
|
|
testFunc: cl.configFile,
|
|
xdgConfigHome: filepath.Join(homeDir, "xdg"),
|
|
files: []string{filepath.Join(homeDir, ".terraformrc")},
|
|
expect: filepath.Join(homeDir, ".terraformrc"),
|
|
},
|
|
{
|
|
name: "configDir: use .terraform.d default",
|
|
testFunc: cl.configDir,
|
|
expect: filepath.Join(homeDir, ".terraform.d"),
|
|
},
|
|
{
|
|
name: "configDir: prefer .terraform.d",
|
|
testFunc: cl.configDir,
|
|
xdgConfigHome: filepath.Join(homeDir, "xdg"),
|
|
files: []string{filepath.Join(homeDir, ".terraform.d", "placeholder")},
|
|
expect: filepath.Join(homeDir, ".terraform.d"),
|
|
},
|
|
{
|
|
name: "configDir: use XDG value",
|
|
testFunc: cl.configDir,
|
|
xdgConfigHome: filepath.Join(homeDir, "xdg"),
|
|
expect: filepath.Join(homeDir, "xdg", "opentofu"),
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Setenv("HOME", homeDir)
|
|
t.Setenv("XDG_CONFIG_HOME", test.xdgConfigHome)
|
|
for _, f := range test.files {
|
|
createFile(t, f)
|
|
}
|
|
|
|
file, err := test.testFunc()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if test.expect != file {
|
|
t.Fatalf("expected %q, but got %q", test.expect, file)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDataDirs(t *testing.T) {
|
|
homeDir := filepath.Join(t.TempDir(), "home")
|
|
|
|
tests := []struct {
|
|
name string
|
|
xdgDataHome string
|
|
expect []string
|
|
}{
|
|
{
|
|
name: "use XDG data dir",
|
|
xdgDataHome: filepath.Join(homeDir, "xdg"),
|
|
expect: []string{
|
|
filepath.Join(homeDir, ".terraform.d"),
|
|
filepath.Join(homeDir, "xdg", "opentofu"),
|
|
},
|
|
},
|
|
{
|
|
name: "use default",
|
|
expect: []string{
|
|
filepath.Join(homeDir, ".terraform.d"),
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Setenv("HOME", homeDir)
|
|
t.Setenv("XDG_DATA_HOME", test.xdgDataHome)
|
|
|
|
dirs, err := standardConfigLoader().dataDirs()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !slices.Equal(test.expect, dirs) {
|
|
t.Fatalf("expected %+v, but got %+v", test.expect, dirs)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func createFile(t *testing.T, path string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = os.RemoveAll(filepath.Dir(path)) })
|
|
}
|