github.com/opentofu/opentofu@v1.7.1/internal/command/cliconfig/config_unix_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 //go:build !windows 7 // +build !windows 8 9 package cliconfig 10 11 import ( 12 "os" 13 "path/filepath" 14 "slices" 15 "testing" 16 ) 17 18 func TestConfigFileConfigDir(t *testing.T) { 19 homeDir := filepath.Join(t.TempDir(), "home") 20 21 tests := []struct { 22 name string 23 xdgConfigHome string 24 files []string 25 testFunc func() (string, error) 26 expect string 27 }{ 28 { 29 name: "configFile: use home tofurc", 30 testFunc: configFile, 31 files: []string{filepath.Join(homeDir, ".tofurc")}, 32 expect: filepath.Join(homeDir, ".tofurc"), 33 }, 34 { 35 name: "configFile: use home terraformrc", 36 testFunc: configFile, 37 files: []string{filepath.Join(homeDir, ".terraformrc")}, 38 expect: filepath.Join(homeDir, ".terraformrc"), 39 }, 40 { 41 name: "configFile: use default fallback", 42 testFunc: configFile, 43 expect: filepath.Join(homeDir, ".tofurc"), 44 }, 45 { 46 name: "configFile: use XDG tofurc", 47 testFunc: configFile, 48 xdgConfigHome: filepath.Join(homeDir, "xdg"), 49 expect: filepath.Join(homeDir, "xdg", "opentofu", "tofurc"), 50 }, 51 { 52 name: "configFile: prefer home tofurc", 53 testFunc: configFile, 54 xdgConfigHome: filepath.Join(homeDir, "xdg"), 55 files: []string{filepath.Join(homeDir, ".tofurc")}, 56 expect: filepath.Join(homeDir, ".tofurc"), 57 }, 58 { 59 name: "configFile: prefer home terraformrc", 60 testFunc: configFile, 61 xdgConfigHome: filepath.Join(homeDir, "xdg"), 62 files: []string{filepath.Join(homeDir, ".terraformrc")}, 63 expect: filepath.Join(homeDir, ".terraformrc"), 64 }, 65 { 66 name: "configDir: use .terraform.d default", 67 testFunc: configDir, 68 expect: filepath.Join(homeDir, ".terraform.d"), 69 }, 70 { 71 name: "configDir: prefer .terraform.d", 72 testFunc: configDir, 73 xdgConfigHome: filepath.Join(homeDir, "xdg"), 74 files: []string{filepath.Join(homeDir, ".terraform.d", "placeholder")}, 75 expect: filepath.Join(homeDir, ".terraform.d"), 76 }, 77 { 78 name: "configDir: use XDG value", 79 testFunc: configDir, 80 xdgConfigHome: filepath.Join(homeDir, "xdg"), 81 expect: filepath.Join(homeDir, "xdg", "opentofu"), 82 }, 83 } 84 85 for _, test := range tests { 86 t.Run(test.name, func(t *testing.T) { 87 t.Setenv("HOME", homeDir) 88 t.Setenv("XDG_CONFIG_HOME", test.xdgConfigHome) 89 for _, f := range test.files { 90 createFile(t, f) 91 } 92 93 file, err := test.testFunc() 94 if err != nil { 95 t.Fatalf("unexpected error: %v", err) 96 } 97 if test.expect != file { 98 t.Fatalf("expected %q, but got %q", test.expect, file) 99 } 100 }) 101 } 102 } 103 104 func TestDataDirs(t *testing.T) { 105 homeDir := filepath.Join(t.TempDir(), "home") 106 107 tests := []struct { 108 name string 109 xdgDataHome string 110 expect []string 111 }{ 112 { 113 name: "use XDG data dir", 114 xdgDataHome: filepath.Join(homeDir, "xdg"), 115 expect: []string{ 116 filepath.Join(homeDir, ".terraform.d"), 117 filepath.Join(homeDir, "xdg", "opentofu"), 118 }, 119 }, 120 { 121 name: "use default", 122 expect: []string{ 123 filepath.Join(homeDir, ".terraform.d"), 124 }, 125 }, 126 } 127 128 for _, test := range tests { 129 t.Run(test.name, func(t *testing.T) { 130 t.Setenv("HOME", homeDir) 131 t.Setenv("XDG_DATA_HOME", test.xdgDataHome) 132 133 dirs, err := dataDirs() 134 if err != nil { 135 t.Fatalf("unexpected error: %v", err) 136 } 137 if !slices.Equal(test.expect, dirs) { 138 t.Fatalf("expected %+v, but got %+v", test.expect, dirs) 139 } 140 }) 141 } 142 } 143 144 func createFile(t *testing.T, path string) { 145 t.Helper() 146 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { 147 t.Fatal(err) 148 } 149 if err := os.WriteFile(path, nil, 0o600); err != nil { 150 t.Fatal(err) 151 } 152 t.Cleanup(func() { _ = os.RemoveAll(filepath.Dir(path)) }) 153 }