-
Notifications
You must be signed in to change notification settings - Fork 0
fix: provide a warning when paths provided to save don't exist #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package zstash | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFilterExistingPaths(t *testing.T) { | ||
| t.Run("returns all paths when all exist", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| file1 := filepath.Join(dir, "file1.txt") | ||
| file2 := filepath.Join(dir, "file2.txt") | ||
| require.NoError(t, os.WriteFile(file1, []byte("test1"), 0o644)) | ||
| require.NoError(t, os.WriteFile(file2, []byte("test2"), 0o644)) | ||
|
|
||
| validPaths, warnings, err := filterExistingPaths([]string{file1, file2}) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, []string{file1, file2}, validPaths) | ||
| assert.Empty(t, warnings) | ||
| }) | ||
|
|
||
| t.Run("returns warning for missing path", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| existingFile := filepath.Join(dir, "exists.txt") | ||
| missingFile := filepath.Join(dir, "missing.txt") | ||
| require.NoError(t, os.WriteFile(existingFile, []byte("test"), 0o644)) | ||
|
|
||
| validPaths, warnings, err := filterExistingPaths([]string{existingFile, missingFile}) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, []string{existingFile}, validPaths) | ||
| assert.Len(t, warnings, 1) | ||
| assert.Contains(t, warnings[0], "path does not exist") | ||
| assert.Contains(t, warnings[0], missingFile) | ||
| }) | ||
|
|
||
| t.Run("returns error when all paths missing", func(t *testing.T) { | ||
| validPaths, warnings, err := filterExistingPaths([]string{"/nonexistent/path1", "/nonexistent/path2"}) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "no valid paths found") | ||
| assert.Nil(t, validPaths) | ||
| assert.Len(t, warnings, 2) | ||
| }) | ||
|
|
||
| t.Run("returns error for empty paths", func(t *testing.T) { | ||
| validPaths, warnings, err := filterExistingPaths([]string{}) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "no paths provided") | ||
| assert.Nil(t, validPaths) | ||
| assert.Nil(t, warnings) | ||
| }) | ||
|
|
||
| t.Run("handles directories", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| subdir := filepath.Join(dir, "subdir") | ||
| require.NoError(t, os.MkdirAll(subdir, 0o755)) | ||
|
|
||
| validPaths, warnings, err := filterExistingPaths([]string{subdir}) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, []string{subdir}, validPaths) | ||
| assert.Empty(t, warnings) | ||
| }) | ||
|
|
||
| t.Run("handles tilde expansion", func(t *testing.T) { | ||
| homeDir, err := os.UserHomeDir() | ||
| require.NoError(t, err) | ||
|
|
||
| // Create a temp file in home directory for this test | ||
| tempFile := filepath.Join(homeDir, ".zstash_test_temp") | ||
| require.NoError(t, os.WriteFile(tempFile, []byte("test"), 0o644)) | ||
| defer os.Remove(tempFile) | ||
|
|
||
| validPaths, warnings, err := filterExistingPaths([]string{"~/.zstash_test_temp"}) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, []string{"~/.zstash_test_temp"}, validPaths) | ||
| assert.Empty(t, warnings) | ||
| }) | ||
|
|
||
| t.Run("warns for missing tilde path", func(t *testing.T) { | ||
| validPaths, warnings, err := filterExistingPaths([]string{"~/.nonexistent_zstash_test_path", "/tmp"}) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, []string{"/tmp"}, validPaths) | ||
| assert.Len(t, warnings, 1) | ||
| assert.Contains(t, warnings[0], "~/.nonexistent_zstash_test_path") | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,6 +202,15 @@ type SaveResult struct { | |
| // TotalDuration is the end-to-end duration of the save operation, | ||
| // from validation through commit (if created) or early exit (if exists). | ||
| TotalDuration time.Duration | ||
|
|
||
| // Warnings contains non-fatal issues encountered during the save operation, | ||
| // such as paths that were configured but did not exist. | ||
| Warnings []string | ||
| } | ||
|
|
||
| // HasWarnings one or more warnings are present in the result | ||
| func (sr *SaveResult) HasWarnings() bool { | ||
| return len(sr.Warnings) > 0 | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a blocker but I don't anticipate caller side can do anything beyond logging for these warnings. Do you want to consider allowing zstash to take a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zhming0 You make a good point, I was tossing up what to do here. I am really just putting off solving the logger problem, I need to either pass in the slog logger in https://pkg.go.dev/github.com/buildkite/zstash#NewCache or propegate it via the context. |
||
|
|
||
| // RestoreResult contains detailed information about a cache restore operation. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an implementation detail. I wonder if we should start by adding an test case for the Save func instead of this internal function?