2024-05-02 06:43:46 +00:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
2024-07-17 23:26:35 +00:00
|
|
|
"context"
|
2024-05-02 06:43:46 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2024-07-17 23:26:35 +00:00
|
|
|
"github.com/urfave/cli/v3"
|
2024-05-02 06:43:46 +00:00
|
|
|
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker/mocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPipelineList(t *testing.T) {
|
|
|
|
testtases := []struct {
|
|
|
|
name string
|
|
|
|
repoID int64
|
|
|
|
repoErr error
|
|
|
|
pipelines []*woodpecker.Pipeline
|
|
|
|
pipelineErr error
|
|
|
|
args []string
|
2024-12-01 19:39:47 +00:00
|
|
|
expected []*woodpecker.Pipeline
|
2024-05-02 06:43:46 +00:00
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
repoID: 1,
|
|
|
|
pipelines: []*woodpecker.Pipeline{
|
|
|
|
{ID: 1, Branch: "main", Event: "push", Status: "success"},
|
|
|
|
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
|
|
|
|
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
|
|
|
|
},
|
|
|
|
args: []string{"ls", "repo/name"},
|
2024-12-01 19:39:47 +00:00
|
|
|
expected: []*woodpecker.Pipeline{
|
2024-05-02 06:43:46 +00:00
|
|
|
{ID: 1, Branch: "main", Event: "push", Status: "success"},
|
|
|
|
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
|
|
|
|
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "limit results",
|
|
|
|
repoID: 1,
|
|
|
|
pipelines: []*woodpecker.Pipeline{
|
|
|
|
{ID: 1, Branch: "main", Event: "push", Status: "success"},
|
|
|
|
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
|
|
|
|
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
|
|
|
|
},
|
|
|
|
args: []string{"ls", "--limit", "2", "repo/name"},
|
2024-12-01 19:39:47 +00:00
|
|
|
expected: []*woodpecker.Pipeline{
|
2024-05-02 06:43:46 +00:00
|
|
|
{ID: 1, Branch: "main", Event: "push", Status: "success"},
|
|
|
|
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pipeline list error",
|
|
|
|
repoID: 1,
|
|
|
|
pipelineErr: errors.New("pipeline error"),
|
|
|
|
args: []string{"ls", "repo/name"},
|
|
|
|
wantErr: errors.New("pipeline error"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testtases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
mockClient := mocks.NewClient(t)
|
2024-12-01 19:39:47 +00:00
|
|
|
mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(func(_ int64, opt woodpecker.PipelineListOptions) ([]*woodpecker.Pipeline, error) {
|
|
|
|
if tt.pipelineErr != nil {
|
|
|
|
return nil, tt.pipelineErr
|
|
|
|
}
|
|
|
|
if opt.Page == 1 {
|
|
|
|
return tt.pipelines, nil
|
|
|
|
}
|
|
|
|
return []*woodpecker.Pipeline{}, nil
|
|
|
|
}).Maybe()
|
2024-05-02 06:43:46 +00:00
|
|
|
mockClient.On("RepoLookup", mock.Anything).Return(&woodpecker.Repo{ID: tt.repoID}, nil)
|
|
|
|
|
2024-11-18 18:40:45 +00:00
|
|
|
command := buildPipelineListCmd()
|
2024-07-17 23:26:35 +00:00
|
|
|
command.Writer = io.Discard
|
2024-11-30 12:57:59 +00:00
|
|
|
command.Action = func(_ context.Context, c *cli.Command) error {
|
|
|
|
pipelines, err := pipelineList(c, mockClient)
|
2024-05-02 06:43:46 +00:00
|
|
|
if tt.wantErr != nil {
|
|
|
|
assert.EqualError(t, err, tt.wantErr.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, tt.expected, pipelines)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
_ = command.Run(context.Background(), tt.args)
|
2024-05-02 06:43:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|