1
0
mirror of https://codeberg.org/crowci/crow.git synced 2025-08-06 09:22:46 +03:00
Files
crow/cli/pipeline/purge_test.go
pat-s cecfb22099 refactor: mocking (#38)
- `mockery` can now be run in one call instead of multiple ones
- Use the recommended "packages" approach which will be the new default moving forward
- Enforce consistent naming of mock files
- Remove warnings when invoking `mockery`
- Get rid of *many* linter warnings 🎉️

Reviewed-on: https://codeberg.org/crowci/crow/pulls/38
Co-authored-by: pat-s <patrick.schratz@gmail.com>
Co-committed-by: pat-s <patrick.schratz@gmail.com>
2025-02-15 10:23:47 +00:00

127 lines
3.0 KiB
Go

package pipeline
import (
"context"
"errors"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/urfave/cli/v3"
crow "codeberg.org/crowci/crow/v3/crow-go/crow"
crow_mocks "codeberg.org/crowci/crow/v3/crow-go/crow/mocks"
)
func TestPipelinePurge(t *testing.T) {
tests := []struct {
name string
repoID int64
args []string
pipelinesKeep []*crow.Pipeline
pipelines []*crow.Pipeline
mockDeleteError error
wantDelete int
wantErr error
}{
{
name: "success with no pipelines to purge",
repoID: 1,
args: []string{"purge", "--older-than", "1h", "repo/name"},
pipelinesKeep: []*crow.Pipeline{
{Number: 1},
},
pipelines: []*crow.Pipeline{},
},
{
name: "success with pipelines to purge",
repoID: 1,
args: []string{"purge", "--older-than", "1h", "repo/name"},
pipelinesKeep: []*crow.Pipeline{
{Number: 1},
},
pipelines: []*crow.Pipeline{
{Number: 1},
{Number: 2},
{Number: 3},
},
wantDelete: 2,
},
{
name: "error on invalid duration",
repoID: 1,
args: []string{"purge", "--older-than", "invalid", "repo/name"},
wantErr: errors.New("time: invalid duration \"invalid\""),
},
{
name: "continue on 422 error",
repoID: 1,
args: []string{"purge", "--older-than", "1h", "repo/name"},
pipelinesKeep: []*crow.Pipeline{
{Number: 1},
},
pipelines: []*crow.Pipeline{
{Number: 1},
{Number: 2},
{Number: 3},
},
wantDelete: 2,
mockDeleteError: &crow.ClientError{
StatusCode: 422,
Message: "test error",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := new(crow_mocks.MockClient)
mockClient.On("RepoLookup", mock.Anything).Maybe().Return(&crow.Repo{ID: tt.repoID}, nil)
mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(func(_ int64, opt crow.PipelineListOptions) ([]*crow.Pipeline, error) {
// Return keep pipelines for first call
if opt.Before.IsZero() {
if opt.Page == 1 {
return tt.pipelinesKeep, nil
}
return []*crow.Pipeline{}, nil
}
// Return pipelines to purge for calls with Before filter
if !opt.Before.IsZero() {
if opt.Page == 1 {
return tt.pipelines, nil
}
return []*crow.Pipeline{}, nil
}
return []*crow.Pipeline{}, nil
}).Maybe()
if tt.mockDeleteError != nil {
mockClient.On("PipelineDelete", tt.repoID, mock.Anything).Return(tt.mockDeleteError)
} else if tt.wantDelete > 0 {
mockClient.On("PipelineDelete", tt.repoID, mock.Anything).Return(nil).Times(tt.wantDelete)
}
command := pipelinePurgeCmd
command.Writer = io.Discard
command.Action = func(_ context.Context, c *cli.Command) error {
err := pipelinePurge(c, mockClient)
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
return nil
}
assert.NoError(t, err)
return nil
}
_ = command.Run(context.Background(), tt.args)
})
}
}