Commit 1d07cf4d authored by Aaron Goldenthal's avatar Aaron Goldenthal
Browse files

Fix test release file and update create-from-file test to check values

parent 164b5e27
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -115,6 +115,8 @@ OPTIONS:
   --help, -h                 Show help (default: false)
```

An example file showing all valid properties can be found [here](internal/testdata/release.yml).

### Get an existing release by tag name

The `get` command uses the [Get a Release by tag name](https://docs.gitlab.com/ee/api/releases/#get-a-release-by-a-tag-name) endpoint
+52 −1
Original line number Diff line number Diff line
package commands

import (
	"encoding/json"
	"flag"
	"io"
	http "net/http"
	"os"
	"testing"
	"time"

	"github.com/sirupsen/logrus"
	testlog "github.com/sirupsen/logrus/hooks/test"
@@ -39,6 +42,29 @@ func TestCreate(t *testing.T) {
}

func TestCreateFromFile(t *testing.T) {
	releasedAt, _ := time.Parse(time.RFC3339, "2019-01-03T01:55:18.203Z")
	expectedRelease := gitlab.CreateReleaseRequest{
		Name:        "Release for test",
		Description: "This is a release for testing only.",
		TagName:     "v1.2.3",
		ReleasedAt:  &releasedAt,
		Ref:         "abcdefgh12345678",
		Assets: &gitlab.Assets{
			Links: []*gitlab.Link{
				{
					Name:            "asset1",
					URL:             "https://gitlab.com/gitlab-org/release-cli",
					DirectAssetPath: "/bin/binary_file",
				},
				{
					Name: "asset2",
					URL:  "https://gitlab.com/",
				},
			},
		},
		Milestones: []string{"v2.0", "v3.0"},
	}

	logger, hook := testlog.NewNullLogger()

	mhc := &gitlab.MockHTTPClient{}
@@ -53,8 +79,12 @@ func TestCreateFromFile(t *testing.T) {
	err := createFromFile.Run(ctx)
	require.NoError(t, err)

	// test the release passed to the HTTPClient since the response is mocked
	release := getReleaseFromRequest(t, mhc)
	require.Equal(t, expectedRelease, release)

	for _, entry := range hook.AllEntries() {
		require.Contains(t, []string{"Creating Release...", "file does not exist, using string value for --description", "release created successfully!"}, entry.Message)
		require.Contains(t, []string{"Creating Release...", "release created successfully!"}, entry.Message)
	}
}

@@ -425,3 +455,24 @@ func newTestCreateFromFile(t *testing.T, logger logrus.FieldLogger, mhc gitlab.H

	return create, ctx
}

// getReleaseFromRequest reads the request body from the mock server call and
// unmarshals it into a CreateReleaseRequest
func getReleaseFromRequest(t *testing.T, mhc *gitlab.MockHTTPClient) gitlab.CreateReleaseRequest {
	// check that mock was called once with an http request
	require.Len(t, mhc.Mock.Calls, 1)
	require.Len(t, mhc.Mock.Calls[0].Arguments, 1)
	request := mhc.Mock.Calls[0].Arguments[0].(*http.Request)
	require.NotNil(t, request)

	body, err := request.GetBody()
	require.NoError(t, err)

	data, err := io.ReadAll(body)
	require.NoError(t, err)

	release := gitlab.CreateReleaseRequest{}
	err = json.Unmarshal(data, &release)
	require.NoError(t, err)
	return release
}
+2 −2
Original line number Diff line number Diff line
@@ -2,9 +2,9 @@
name: Release for test
description: This is a release for testing only.
tag-name: v1.2.3
released_at: 2019-01-03T01:55:18.203Z
released-at: 2019-01-03T01:55:18.203Z
ref: abcdefgh12345678
milestones:
milestone:
  - 'v2.0'
  - 'v3.0'
assets-link: