(t *testing.T)
| 247 | } |
| 248 | |
| 249 | func TestConfigFileSave(t *testing.T) { |
| 250 | testDir := t.TempDir() |
| 251 | configPath := filepath.Join(testDir, "a", "b", "c", "configfile") |
| 252 | |
| 253 | assert.NoError(t, config.SetConfigPath(configPath)) |
| 254 | data := &Storage{} |
| 255 | require.Error(t, data.Load(), config.ErrorConfigFileNotFound) |
| 256 | |
| 257 | t.Run("CreatesDirsAndFile", func(t *testing.T) { |
| 258 | err := data.Save() |
| 259 | require.NoError(t, err) |
| 260 | info, err := os.Stat(configPath) |
| 261 | require.NoError(t, err) |
| 262 | assert.False(t, info.IsDir()) |
| 263 | }) |
| 264 | t.Run("KeepsFileMode", func(t *testing.T) { |
| 265 | if runtime.GOOS != "linux" { |
| 266 | t.Skip("this is a Linux only test") |
| 267 | } |
| 268 | assert.NoError(t, os.Chmod(configPath, 0400)) // -r-------- |
| 269 | defer func() { |
| 270 | _ = os.Chmod(configPath, 0644) // -rw-r--r-- |
| 271 | }() |
| 272 | err := data.Save() |
| 273 | require.NoError(t, err) |
| 274 | info, err := os.Stat(configPath) |
| 275 | require.NoError(t, err) |
| 276 | assert.Equal(t, os.FileMode(0400), info.Mode().Perm()) |
| 277 | }) |
| 278 | t.Run("SucceedsEvenIfReadOnlyFile", func(t *testing.T) { |
| 279 | // Save succeeds even if file is read-only since it does not write directly to the file. |
| 280 | assert.NoError(t, os.Chmod(configPath, 0400)) // -r-------- |
| 281 | defer func() { |
| 282 | _ = os.Chmod(configPath, 0644) // -rw-r--r-- |
| 283 | }() |
| 284 | err := data.Save() |
| 285 | assert.NoError(t, err) |
| 286 | }) |
| 287 | t.Run("FailsIfNotAccessToDir", func(t *testing.T) { |
| 288 | // Save fails if no access to the directory. |
| 289 | if runtime.GOOS != "linux" { |
| 290 | // On Windows the os.Chmod only affects the read-only attribute of files) |
| 291 | t.Skip("this is a Linux only test") |
| 292 | } |
| 293 | configDir := filepath.Dir(configPath) |
| 294 | assert.NoError(t, os.Chmod(configDir, 0400)) // -r-------- |
| 295 | defer func() { |
| 296 | _ = os.Chmod(configDir, 0755) // -rwxr-xr-x |
| 297 | }() |
| 298 | err := data.Save() |
| 299 | require.Error(t, err) |
| 300 | assert.True(t, strings.HasPrefix(err.Error(), "failed to resolve config file path")) |
| 301 | }) |
| 302 | t.Run("FailsIfNotAllowedToCreateNewFiles", func(t *testing.T) { |
| 303 | // Save fails if read-only access to the directory, since it needs to create temporary files in there. |
| 304 | if runtime.GOOS != "linux" { |
| 305 | // On Windows the os.Chmod only affects the read-only attribute of files) |
| 306 | t.Skip("this is a Linux only test") |
nothing calls this directly
no test coverage detected
searching dependent graphs…