|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestDiscoverPaths_usesXDGDefaults_whenOnlyHomeProvided(t *testing.T) { |
| 14 | + // Given: an isolated HOME with no XDG override variables. |
| 15 | + home := t.TempDir() |
| 16 | + |
| 17 | + // When: config paths are discovered from the injected environment. |
| 18 | + got, err := DiscoverPaths(PathOptions{Env: map[string]string{"HOME": home}}) |
| 19 | + |
| 20 | + // Then: lazynpm uses XDG defaults plus the legacy read-only migration input. |
| 21 | + assert.NoError(t, err) |
| 22 | + assert.Equal(t, filepath.Join(home, ".config", "lazynpm", "config.yaml"), got.WriteConfigFile.String()) |
| 23 | + assert.Equal(t, filepath.Join(home, ".local", "state", "lazynpm"), got.StateDir.String()) |
| 24 | + assert.Equal(t, filepath.Join(home, ".cache", "lazynpm"), got.CacheDir.String()) |
| 25 | + readPath, err := got.SelectConfigReadPath(ConfigReadOptions{}) |
| 26 | + assert.NoError(t, err) |
| 27 | + assert.Equal(t, ConfigSourceNone, readPath.Source) |
| 28 | + assert.Equal(t, filepath.Join(home, ".config", "jesseduffield", "lazynpm", "config.yml"), got.LegacyMigrationConfigFile.String()) |
| 29 | +} |
| 30 | + |
| 31 | +func TestDiscoverPaths_usesProcessEnvironment_whenXDGVariablesAreUnset(t *testing.T) { |
| 32 | + // Given: an isolated process environment with no XDG override variables. |
| 33 | + home := t.TempDir() |
| 34 | + t.Setenv("HOME", home) |
| 35 | + t.Setenv("XDG_CONFIG_HOME", "") |
| 36 | + t.Setenv("XDG_STATE_HOME", "") |
| 37 | + t.Setenv("XDG_CACHE_HOME", "") |
| 38 | + |
| 39 | + // When: config paths are discovered from the process environment. |
| 40 | + got, err := DiscoverPaths(PathOptions{}) |
| 41 | + |
| 42 | + // Then: discovery never consults the real user's config locations. |
| 43 | + assert.NoError(t, err) |
| 44 | + assert.Equal(t, filepath.Join(home, ".config", "lazynpm", "config.yaml"), got.WriteConfigFile.String()) |
| 45 | + assert.Equal(t, filepath.Join(home, ".local", "state", "lazynpm"), got.StateDir.String()) |
| 46 | + assert.Equal(t, filepath.Join(home, ".cache", "lazynpm"), got.CacheDir.String()) |
| 47 | +} |
| 48 | + |
| 49 | +func TestDiscoverPaths_usesExplicitXDGEnv_whenProvided(t *testing.T) { |
| 50 | + // Given: isolated XDG roots and HOME for the legacy migration path. |
| 51 | + home := t.TempDir() |
| 52 | + configRoot := filepath.Join(t.TempDir(), "config-root") |
| 53 | + stateRoot := filepath.Join(t.TempDir(), "state-root") |
| 54 | + cacheRoot := filepath.Join(t.TempDir(), "cache-root") |
| 55 | + |
| 56 | + // When: config paths are discovered from explicit XDG variables. |
| 57 | + got, err := DiscoverPaths(PathOptions{Env: map[string]string{ |
| 58 | + "HOME": home, |
| 59 | + "XDG_CONFIG_HOME": configRoot, |
| 60 | + "XDG_STATE_HOME": stateRoot, |
| 61 | + "XDG_CACHE_HOME": cacheRoot, |
| 62 | + }}) |
| 63 | + |
| 64 | + // Then: the XDG roots override HOME-derived defaults without changing legacy input. |
| 65 | + assert.NoError(t, err) |
| 66 | + assert.Equal(t, filepath.Join(configRoot, "lazynpm", "config.yaml"), got.WriteConfigFile.String()) |
| 67 | + assert.Equal(t, filepath.Join(stateRoot, "lazynpm"), got.StateDir.String()) |
| 68 | + assert.Equal(t, filepath.Join(cacheRoot, "lazynpm"), got.CacheDir.String()) |
| 69 | + assert.Equal(t, filepath.Join(home, ".config", "jesseduffield", "lazynpm", "config.yml"), got.LegacyMigrationConfigFile.String()) |
| 70 | +} |
| 71 | + |
| 72 | +func TestPathSet_SelectConfigReadPath_prefersXDG_whenBothConfigsExist(t *testing.T) { |
| 73 | + // Given: both the new XDG config and the legacy migration file exist. |
| 74 | + paths := mustDiscoverTempPaths(t) |
| 75 | + writeFile(t, paths.WriteConfigFile.String()) |
| 76 | + writeFile(t, paths.LegacyMigrationConfigFile.String()) |
| 77 | + |
| 78 | + // When: the read path is selected. |
| 79 | + got, err := paths.SelectConfigReadPath(ConfigReadOptions{}) |
| 80 | + |
| 81 | + // Then: the new XDG config wins over the legacy migration input. |
| 82 | + assert.NoError(t, err) |
| 83 | + assert.Equal(t, ConfigSourceXDG, got.Source) |
| 84 | + assert.Equal(t, paths.WriteConfigFile.String(), got.Path.String()) |
| 85 | +} |
| 86 | + |
| 87 | +func TestPathSet_SelectConfigReadPath_usesLegacy_whenOnlyLegacyExists(t *testing.T) { |
| 88 | + // Given: only the read-only legacy migration file exists. |
| 89 | + paths := mustDiscoverTempPaths(t) |
| 90 | + writeFile(t, paths.LegacyMigrationConfigFile.String()) |
| 91 | + |
| 92 | + // When: the read path is selected. |
| 93 | + got, err := paths.SelectConfigReadPath(ConfigReadOptions{}) |
| 94 | + |
| 95 | + // Then: the legacy file is available only as a migration source. |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.Equal(t, ConfigSourceLegacyMigration, got.Source) |
| 98 | + assert.Equal(t, paths.LegacyMigrationConfigFile.String(), got.Path.String()) |
| 99 | +} |
| 100 | + |
| 101 | +func TestPathSet_SelectConfigReadPath_characterizesExistingUserSourceFallback(t *testing.T) { |
| 102 | + // Given: an isolated user environment with only the legacy migration file. |
| 103 | + paths := mustDiscoverTempPaths(t) |
| 104 | + writeFile(t, paths.LegacyMigrationConfigFile.String()) |
| 105 | + |
| 106 | + // When: the existing user-source selector runs. |
| 107 | + got, err := paths.SelectConfigReadPath(ConfigReadOptions{}) |
| 108 | + |
| 109 | + // Then: legacy configuration remains the selected read-only fallback. |
| 110 | + assert.NoError(t, err) |
| 111 | + assert.Equal(t, ConfigSourceLegacyMigration, got.Source) |
| 112 | + assert.Equal(t, paths.LegacyMigrationConfigFile.String(), got.Path.String()) |
| 113 | +} |
| 114 | + |
| 115 | +func TestPathSet_SelectConfigReadPath_returnsNone_whenNoConfigExists(t *testing.T) { |
| 116 | + // Given: no config file exists in either the XDG or legacy location. |
| 117 | + paths := mustDiscoverTempPaths(t) |
| 118 | + |
| 119 | + // When: the read path is selected. |
| 120 | + got, err := paths.SelectConfigReadPath(ConfigReadOptions{}) |
| 121 | + |
| 122 | + // Then: discovery succeeds without requiring any user config file. |
| 123 | + assert.NoError(t, err) |
| 124 | + assert.Equal(t, ConfigSourceNone, got.Source) |
| 125 | + assert.Equal(t, "", got.Path.String()) |
| 126 | +} |
| 127 | + |
| 128 | +func TestPathSet_SelectConfigReadPath_returnsError_whenXDGConfigPathIsDirectory(t *testing.T) { |
| 129 | + // Given: the XDG config file location is occupied by a directory. |
| 130 | + paths := mustDiscoverTempPaths(t) |
| 131 | + assert.NoError(t, os.MkdirAll(paths.WriteConfigFile.String(), 0700)) |
| 132 | + |
| 133 | + // When: the read path is selected. |
| 134 | + _, err := paths.SelectConfigReadPath(ConfigReadOptions{}) |
| 135 | + |
| 136 | + // Then: malformed filesystem input is not treated as a usable config source. |
| 137 | + assert.True(t, errors.Is(err, ErrConfigPathNotRegularFile)) |
| 138 | +} |
| 139 | + |
| 140 | +func TestPathSet_EnsureDirectories_createsXDGDirs_whenMissing(t *testing.T) { |
| 141 | + // Given: discovered paths under isolated temp roots. |
| 142 | + paths := mustDiscoverTempPaths(t) |
| 143 | + |
| 144 | + // When: directory creation is requested. |
| 145 | + err := paths.EnsureDirectories() |
| 146 | + |
| 147 | + // Then: config, state, and cache directories exist with user-only permissions where supported. |
| 148 | + assert.NoError(t, err) |
| 149 | + assertUserOnlyDir(t, filepath.Dir(paths.WriteConfigFile.String())) |
| 150 | + assertUserOnlyDir(t, paths.StateDir.String()) |
| 151 | + assertUserOnlyDir(t, paths.CacheDir.String()) |
| 152 | + _, err = os.Stat(filepath.Dir(paths.LegacyMigrationConfigFile.String())) |
| 153 | + assert.True(t, os.IsNotExist(err)) |
| 154 | +} |
| 155 | + |
| 156 | +func mustDiscoverTempPaths(t *testing.T) PathSet { |
| 157 | + t.Helper() |
| 158 | + home := t.TempDir() |
| 159 | + paths, err := DiscoverPaths(PathOptions{Env: map[string]string{ |
| 160 | + "HOME": home, |
| 161 | + "XDG_CONFIG_HOME": filepath.Join(t.TempDir(), "config-root"), |
| 162 | + "XDG_STATE_HOME": filepath.Join(t.TempDir(), "state-root"), |
| 163 | + "XDG_CACHE_HOME": filepath.Join(t.TempDir(), "cache-root"), |
| 164 | + }}) |
| 165 | + assert.NoError(t, err) |
| 166 | + return paths |
| 167 | +} |
| 168 | + |
| 169 | +func writeFile(t *testing.T, path string) { |
| 170 | + t.Helper() |
| 171 | + assert.NoError(t, os.MkdirAll(filepath.Dir(path), 0700)) |
| 172 | + assert.NoError(t, os.WriteFile(path, []byte("config: true\n"), 0600)) |
| 173 | +} |
| 174 | + |
| 175 | +func assertUserOnlyDir(t *testing.T, path string) { |
| 176 | + t.Helper() |
| 177 | + info, err := os.Stat(path) |
| 178 | + if !assert.NoError(t, err) { |
| 179 | + return |
| 180 | + } |
| 181 | + assert.True(t, info.IsDir()) |
| 182 | + if runtime.GOOS == "windows" { |
| 183 | + return |
| 184 | + } |
| 185 | + assert.Equal(t, os.FileMode(0700), info.Mode().Perm()) |
| 186 | +} |
0 commit comments