-
Notifications
You must be signed in to change notification settings - Fork 35
Fix: DeleteCommand returns 0 for SuccessCount and FailCount when errors occur #342
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,307 @@ | ||
| package generic | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-core/v2/common/spec" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestNewDeleteCommand(t *testing.T) { | ||
| dc := NewDeleteCommand() | ||
| assert.NotNil(t, dc) | ||
| assert.Equal(t, "rt_delete", dc.CommandName()) | ||
| assert.NotNil(t, dc.Result()) | ||
| } | ||
|
|
||
| func TestDeleteCommand_SetThreads(t *testing.T) { | ||
| dc := NewDeleteCommand() | ||
| dc.SetThreads(5) | ||
| assert.Equal(t, 5, dc.Threads()) | ||
| } | ||
|
|
||
| func TestDeleteCommand_Threads(t *testing.T) { | ||
| dc := NewDeleteCommand() | ||
|
|
||
| // Default threads should be 0 | ||
| assert.Equal(t, 0, dc.Threads()) | ||
|
|
||
| // Set and verify threads | ||
| dc.SetThreads(10) | ||
| assert.Equal(t, 10, dc.Threads()) | ||
|
|
||
| // Chaining should work | ||
| result := dc.SetThreads(3) | ||
| assert.Equal(t, dc, result) | ||
| assert.Equal(t, 3, dc.Threads()) | ||
| } | ||
|
|
||
| func TestGetDeleteParams(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| file *spec.File | ||
| expectedRecursive bool | ||
| }{ | ||
| { | ||
| name: "Basic pattern with default recursive", | ||
| file: &spec.File{ | ||
| Pattern: "repo/path/*", | ||
| }, | ||
| expectedRecursive: true, | ||
| }, | ||
| { | ||
| name: "Pattern with recursive true", | ||
| file: &spec.File{ | ||
| Pattern: "repo/path/*", | ||
| Recursive: "true", | ||
| }, | ||
| expectedRecursive: true, | ||
| }, | ||
| { | ||
| name: "Pattern with recursive false", | ||
| file: &spec.File{ | ||
| Pattern: "repo/path/*", | ||
| Recursive: "false", | ||
| }, | ||
| expectedRecursive: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| params, err := getDeleteParams(tt.file) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, tt.file.Pattern, params.Pattern) | ||
| assert.Equal(t, tt.expectedRecursive, params.Recursive) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetDeleteParams_ExcludeArtifacts(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| excludeArtifacts string | ||
| expectedExcludeArtifacts bool | ||
| }{ | ||
| { | ||
| name: "Default exclude artifacts (false)", | ||
| excludeArtifacts: "", | ||
| expectedExcludeArtifacts: false, | ||
| }, | ||
| { | ||
| name: "Exclude artifacts true", | ||
| excludeArtifacts: "true", | ||
| expectedExcludeArtifacts: true, | ||
| }, | ||
| { | ||
| name: "Exclude artifacts false", | ||
| excludeArtifacts: "false", | ||
| expectedExcludeArtifacts: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| file := &spec.File{ | ||
| Pattern: "repo/*", | ||
| ExcludeArtifacts: tt.excludeArtifacts, | ||
| } | ||
| params, err := getDeleteParams(file) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, tt.expectedExcludeArtifacts, params.ExcludeArtifacts) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetDeleteParams_IncludeDeps(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| includeDeps string | ||
| expectedIncludeDeps bool | ||
| }{ | ||
| { | ||
| name: "Default include deps (false)", | ||
| includeDeps: "", | ||
| expectedIncludeDeps: false, | ||
| }, | ||
| { | ||
| name: "Include deps true", | ||
| includeDeps: "true", | ||
| expectedIncludeDeps: true, | ||
| }, | ||
| { | ||
| name: "Include deps false", | ||
| includeDeps: "false", | ||
| expectedIncludeDeps: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| file := &spec.File{ | ||
| Pattern: "repo/*", | ||
| IncludeDeps: tt.includeDeps, | ||
| } | ||
| params, err := getDeleteParams(file) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, tt.expectedIncludeDeps, params.IncludeDeps) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetDeleteParams_CombinedFlags(t *testing.T) { | ||
| file := &spec.File{ | ||
| Pattern: "repo/path/**/*.jar", | ||
| Recursive: "true", | ||
| ExcludeArtifacts: "true", | ||
| IncludeDeps: "true", | ||
| Exclusions: []string{"*-sources.jar", "*-javadoc.jar"}, | ||
| } | ||
|
|
||
| params, err := getDeleteParams(file) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, "repo/path/**/*.jar", params.Pattern) | ||
| assert.True(t, params.Recursive) | ||
| assert.True(t, params.ExcludeArtifacts) | ||
| assert.True(t, params.IncludeDeps) | ||
| assert.Len(t, params.Exclusions, 2) | ||
| } | ||
|
|
||
| func TestGetDeleteParams_WithExclusions(t *testing.T) { | ||
| file := &spec.File{ | ||
| Pattern: "repo/*.bin", | ||
| Exclusions: []string{ | ||
| "*-test.bin", | ||
| "*-debug.bin", | ||
| "temp/*", | ||
| }, | ||
| } | ||
|
|
||
| params, err := getDeleteParams(file) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Len(t, params.Exclusions, 3) | ||
| assert.Contains(t, params.Exclusions, "*-test.bin") | ||
| assert.Contains(t, params.Exclusions, "*-debug.bin") | ||
| assert.Contains(t, params.Exclusions, "temp/*") | ||
| } | ||
|
|
||
| func TestGetDeleteParams_EmptyPattern(t *testing.T) { | ||
| file := &spec.File{ | ||
| Pattern: "", | ||
| } | ||
|
|
||
| params, err := getDeleteParams(file) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, "", params.Pattern) | ||
| } | ||
|
|
||
| func TestDeleteCommand_ResultInitialized(t *testing.T) { | ||
| dc := NewDeleteCommand() | ||
|
|
||
| // Result should be initialized (not nil) | ||
| result := dc.Result() | ||
| assert.NotNil(t, result) | ||
|
|
||
| // Default counts should be 0 | ||
| assert.Equal(t, 0, result.SuccessCount()) | ||
| assert.Equal(t, 0, result.FailCount()) | ||
| } | ||
|
|
||
| func TestDeleteCommand_GenericCommandEmbedded(t *testing.T) { | ||
| dc := NewDeleteCommand() | ||
|
|
||
| // Test GenericCommand methods are accessible | ||
| dc.SetDryRun(true) | ||
| assert.True(t, dc.DryRun()) | ||
|
|
||
| dc.SetQuiet(true) | ||
| assert.True(t, dc.Quiet()) | ||
|
|
||
| dc.SetRetries(3) | ||
| assert.Equal(t, 3, dc.Retries()) | ||
| } | ||
|
|
||
| // TestErrorsJoin verifies that errors.Join works correctly for combining errors | ||
| // This tests the behavior used in DeleteFiles when both delete and length errors occur | ||
| func TestErrorsJoin(t *testing.T) { | ||
| err1 := errors.New("delete error") | ||
| err2 := errors.New("length error") | ||
|
|
||
| combined := errors.Join(err1, err2) | ||
| assert.Error(t, combined) | ||
| assert.Contains(t, combined.Error(), "delete error") | ||
| assert.Contains(t, combined.Error(), "length error") | ||
|
|
||
| // Test with nil errors | ||
| combinedWithNil := errors.Join(nil, err2) | ||
| assert.Error(t, combinedWithNil) | ||
| assert.Contains(t, combinedWithNil.Error(), "length error") | ||
|
|
||
| combinedBothNil := errors.Join(nil, nil) | ||
| assert.NoError(t, combinedBothNil) | ||
| } | ||
|
|
||
| // TestDeleteFilesCountBehavior documents the expected behavior of count calculations | ||
| // deletedCount represents successfully deleted items | ||
| // length - deletedCount represents failed items | ||
| func TestDeleteFilesCountBehavior(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| deletedCount int | ||
| totalLength int | ||
| expectedSuccessCount int | ||
| expectedFailedCount int | ||
| }{ | ||
| { | ||
| name: "All items deleted successfully", | ||
| deletedCount: 10, | ||
| totalLength: 10, | ||
| expectedSuccessCount: 10, | ||
| expectedFailedCount: 0, | ||
| }, | ||
| { | ||
| name: "Some items failed", | ||
| deletedCount: 7, | ||
| totalLength: 10, | ||
| expectedSuccessCount: 7, | ||
| expectedFailedCount: 3, | ||
| }, | ||
| { | ||
| name: "All items failed", | ||
| deletedCount: 0, | ||
| totalLength: 10, | ||
| expectedSuccessCount: 0, | ||
| expectedFailedCount: 10, | ||
| }, | ||
| { | ||
| name: "No items to delete", | ||
| deletedCount: 0, | ||
| totalLength: 0, | ||
| expectedSuccessCount: 0, | ||
| expectedFailedCount: 0, | ||
| }, | ||
| { | ||
| name: "Partial success (404 scenario)", | ||
| deletedCount: 5, | ||
| totalLength: 8, | ||
| expectedSuccessCount: 5, | ||
| expectedFailedCount: 3, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // This simulates the count calculation in DeleteFiles | ||
| successCount := tt.deletedCount | ||
| failedCount := tt.totalLength - tt.deletedCount | ||
|
|
||
| assert.Equal(t, tt.expectedSuccessCount, successCount) | ||
| assert.Equal(t, tt.expectedFailedCount, failedCount) | ||
| }) | ||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
@bhanurp I didn't understand the need of renaming err to deleteErr and then assigning deleteErr to err?
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.
to avoid variable shadowing of err at delete.go#L42
even below should work as well
var successCount, failedCount int successCount, failedCount, err = dc.DeleteFiles(reader) // Uses existing 'err' result := dc.Result() result.SetSuccessCount(successCount) result.SetFailCount(failedCount)moving successCount, failedCount as variable declaration instead of shorthand :=