diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 2134bc4..5e221a2 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -18,6 +18,9 @@ jobs: with: go-version-file: go.mod + - name: Generate Mocks + run: make generate-mock + - name: Static Code Analysis uses: golangci/golangci-lint-action@v6 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 174e25f..282e272 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -33,11 +33,8 @@ jobs: restore-keys: go- - name: Run Tests - env: - XUNIT_OUTFILE: test-reports/unit.xml run: | - test -d "$(dirname $XUNIT_OUTFILE)" || mkdir -p "$(dirname $XUNIT_OUTFILE)" - .github/scripts/gotest.sh ./... + make test-ci - name: Publish Results uses: EnricoMi/publish-unit-test-result-action@v2 @@ -45,4 +42,4 @@ jobs: with: check_name: Unit Tests Results files: | - test-reports/unit.xml + utests-report.xml diff --git a/.golangci.yml b/.golangci.yml index e91ccd7..3e58db1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,48 +1,48 @@ # Options for analysis running # More info could be found at https://golangci-lint.run/usage/configuration/ run: - # timeout for analysis, e.g. 30s, 5m, default is 1m - timeout: 5m - modules-download-mode: readonly + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 5m + modules-download-mode: readonly # List of useful linters could be found at https://github.com/golangci/awesome-go-linters linters: - disable-all: true - enable: - - errcheck - - copyloopvar - - gofumpt - - goimports - - gosimple - - govet - - ineffassign - - makezero - - misspell - - noctx - - nolintlint - - rowserrcheck - - sqlclosecheck - - staticcheck - - unconvert - - unused - - wastedassign - - gosec + disable-all: true + enable: + - errcheck + - copyloopvar + - gofumpt + - goimports + - gosimple + - govet + - ineffassign + - makezero + - misspell + - noctx + - nolintlint + - rowserrcheck + - sqlclosecheck + - staticcheck + - unconvert + - unused + - wastedassign + - gosec linters-settings: - staticcheck: - # https://staticcheck.io/docs/options#checks - checks: [ "all","-SA1019","-SA1029" ] - gosec: - excludes: ["G204", "G301", "G302", "G304", "G306", "G601", "G101", "G407"] - exclude-generated: true - exclude-test-files: true - config: - global: - nosec: true + staticcheck: + # https://staticcheck.io/docs/options#checks + checks: [ "all","-SA1019","-SA1029" ] + gosec: + excludes: [ "G204", "G301", "G302", "G304", "G306", "G601", "G101", "G407" ] + exclude-generated: true + exclude-test-files: true + config: + global: + nosec: true issues: - exclude-use-default: false - # Maximum issues count per one linter. Set to 0 to disable. Default is 50. - max-issues-per-linter: 0 - # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. - max-same-issues: 0 + exclude-use-default: false + # Maximum issues count per one linter. Set to 0 to disable. Default is 50. + max-issues-per-linter: 0 + # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. + max-same-issues: 0 diff --git a/Makefile b/Makefile index 0c1677e..78d8e2f 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ export PROJECT_DIR ?= $(CURDIR) BINARY_CLI = bin WORKSPACE_ROOT = $(shell cd "${PROJECT_DIR}" && pwd) TOOLS_DIR := $(CURDIR)/.tools -SCRIPTS_DIR = ${PROJECT_DIR}/.github/scripts +SCRIPTS_DIR = ${PROJECT_DIR}/scripts TARGET_DIR = ${PROJECT_DIR}/target LINKERFLAGS = -s -w COMPILERFLAGS = all=-trimpath=$(WORKSPACE_ROOT) @@ -34,7 +34,7 @@ GOIMPORTS: ########## ANALYSE ########## GOLANGCI_LINT = ${TOOLS_DIR}/golangci-lint -GOLANGCI_LINT_VERSION = 1.62.2 +GOLANGCI_LINT_VERSION = 1.63.4 verify: GOLANGCI_LINT echo $(GO_SOURCES) @@ -45,8 +45,9 @@ GOLANGCI_LINT: ########## BUILD ########## prereq:: - $(GOCMD) install github.com/jstemmer/go-junit-report@v1.0.0 + $(GOCMD) install github.com/jstemmer/go-junit-report/v2@latest GOBIN=${TOOLS_DIR} $(GOCMD) install go.uber.org/mock/mockgen@v0.5.0 + ${TOOLS_DIR}/mockgen --version build:: $(GOCMD) env GOOS GOARCH @@ -63,26 +64,23 @@ build-install:: build .PHONY: clean-mock clean-mock: @echo Cleaning generated mock files - find . -path "*/mocks/*.go" -delete + @find . -name "*_mock.go" -delete + +.PHONY: clean +clean:: clean-mock + @echo Cleaning generated files + @rm -rf ${BINARY_CLI} .PHONY: generate-mock -generate-mock: clean-mock +generate-mock: prereq clean-mock @echo Generating test mocks TOOLS_DIR=$(TOOLS_DIR) go generate ./... -test-prereq: prereq generate-mock - mkdir -p target/reports +test-prereq: generate-mock test: PACKAGES=./... -test: TEST_ARGS=-short -test: test-prereq do-run-tests - -itest: PACKAGES=./test/... -itest: TAGS=-tags=itest -itest: TEST_ARGS=-count=1 -p=1 -itest:: test-prereq do-run-tests - -do-run-tests:: - $(SCRIPTS_DIR)/gotest.sh $$(go list $(TAGS) $(PACKAGES) | grep -v "^.*/mocks$$") -timeout 30m -coverpkg=github.com/jfrog/jfrog-cli-application/... -coverprofile=$(TARGET_DIR)/reports/coverage.out $(TEST_ARGS) $(TAGS) +test: test-prereq + go test ./... +test-ci: test-prereq + go test -v 2>&1 ./... | go-junit-report -set-exit-code -iocopy -out utests-report.xml -.PHONY: $(MAKECMDGOALS) diff --git a/application/app/auth/app_details.go b/application/app/auth/app_details.go deleted file mode 100644 index faa47b8..0000000 --- a/application/app/auth/app_details.go +++ /dev/null @@ -1,15 +0,0 @@ -package auth - -import "github.com/jfrog/jfrog-client-go/auth" - -type appDetails struct { - auth.CommonConfigFields -} - -func NewAppDetails() auth.ServiceDetails { - return &appDetails{} -} - -func (rt *appDetails) GetVersion() (string, error) { - panic("Failed: Method is not implemented") -} diff --git a/application/app/context.go b/application/app/context.go index 7109c5a..130e991 100644 --- a/application/app/context.go +++ b/application/app/context.go @@ -1,32 +1,33 @@ package app import ( - "github.com/jfrog/jfrog-cli-application/application/service" + "github.com/jfrog/jfrog-cli-application/application/service/systems" + "github.com/jfrog/jfrog-cli-application/application/service/versions" ) type Context interface { - GetVersionService() service.VersionService - GetSystemService() service.SystemService + GetVersionService() versions.VersionService + GetSystemService() systems.SystemService GetConfig() interface{} } type context struct { - versionService service.VersionService - systemService service.SystemService + versionService versions.VersionService + systemService systems.SystemService } func NewAppContext() Context { return &context{ - versionService: service.NewVersionService(), - systemService: service.NewSystemService(), + versionService: versions.NewVersionService(), + systemService: systems.NewSystemService(), } } -func (c *context) GetVersionService() service.VersionService { +func (c *context) GetVersionService() versions.VersionService { return c.versionService } -func (c *context) GetSystemService() service.SystemService { +func (c *context) GetSystemService() systems.SystemService { return c.systemService } diff --git a/application/app/context_test.go b/application/app/context_test.go new file mode 100644 index 0000000..8e03a08 --- /dev/null +++ b/application/app/context_test.go @@ -0,0 +1,38 @@ +package app + +import ( + "testing" + + mocksystems "github.com/jfrog/jfrog-cli-application/application/service/systems/mocks" + mockversions "github.com/jfrog/jfrog-cli-application/application/service/versions/mocks" + + "github.com/stretchr/testify/assert" +) + +func TestNewAppContext(t *testing.T) { + ctx := NewAppContext() + assert.NotNil(t, ctx) + assert.NotNil(t, ctx.GetVersionService()) + assert.NotNil(t, ctx.GetSystemService()) +} + +func TestGetVersionService(t *testing.T) { + mockVersionService := &mockversions.MockVersionService{} + ctx := &context{ + versionService: mockVersionService, + } + assert.Equal(t, mockVersionService, ctx.GetVersionService()) +} + +func TestGetSystemService(t *testing.T) { + mockSystemService := &mocksystems.MockSystemService{} + ctx := &context{ + systemService: mockSystemService, + } + assert.Equal(t, mockSystemService, ctx.GetSystemService()) +} + +func TestGetConfig(t *testing.T) { + ctx := &context{} + assert.Nil(t, ctx.GetConfig()) +} diff --git a/application/commands/system/ping_cmd.go b/application/commands/system/ping_cmd.go index 8b3e7c8..54ff91b 100644 --- a/application/commands/system/ping_cmd.go +++ b/application/commands/system/ping_cmd.go @@ -1,23 +1,30 @@ package system +//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE} + import ( "github.com/jfrog/jfrog-cli-application/application/app" "github.com/jfrog/jfrog-cli-application/application/commands" "github.com/jfrog/jfrog-cli-application/application/commands/utils" "github.com/jfrog/jfrog-cli-application/application/common" "github.com/jfrog/jfrog-cli-application/application/service" + "github.com/jfrog/jfrog-cli-application/application/service/systems" commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" "github.com/jfrog/jfrog-cli-core/v2/plugins/components" coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" ) type pingCommand struct { - systemService service.SystemService + systemService systems.SystemService serverDetails *coreConfig.ServerDetails } func (pc *pingCommand) Run() error { - ctx := &service.Context{ServerDetails: pc.serverDetails} + ctx, err := service.NewContext(*pc.serverDetails) + if err != nil { + return err + } + return pc.systemService.Ping(ctx) } diff --git a/application/commands/version/create_app_version_cmd.go b/application/commands/version/create_app_version_cmd.go index 21d6443..b937206 100644 --- a/application/commands/version/create_app_version_cmd.go +++ b/application/commands/version/create_app_version_cmd.go @@ -3,6 +3,8 @@ package version import ( "encoding/json" + "github.com/jfrog/jfrog-cli-application/application/service/versions" + "github.com/jfrog/jfrog-cli-application/application/app" "github.com/jfrog/jfrog-cli-application/application/commands" "github.com/jfrog/jfrog-cli-application/application/commands/utils" @@ -19,7 +21,7 @@ import ( ) type createAppVersionCommand struct { - versionService service.VersionService + versionService versions.VersionService serverDetails *coreConfig.ServerDetails requestPayload *model.CreateAppVersionRequest } @@ -29,7 +31,11 @@ type createVersionSpec struct { } func (cv *createAppVersionCommand) Run() error { - ctx := &service.Context{ServerDetails: cv.serverDetails} + ctx, err := service.NewContext(*cv.serverDetails) + if err != nil { + return err + } + return cv.versionService.CreateAppVersion(ctx, cv.requestPayload) } diff --git a/application/commands/version/create_app_version_cmd_test.go b/application/commands/version/create_app_version_cmd_test.go new file mode 100644 index 0000000..a7f95ee --- /dev/null +++ b/application/commands/version/create_app_version_cmd_test.go @@ -0,0 +1,78 @@ +package version + +import ( + "errors" + "testing" + + mockversions "github.com/jfrog/jfrog-cli-application/application/service/versions/mocks" + "go.uber.org/mock/gomock" + + "github.com/jfrog/jfrog-cli-application/application/model" + "github.com/jfrog/jfrog-cli-core/v2/utils/config" + "github.com/stretchr/testify/assert" +) + +func TestCreateAppVersionCommand_Run(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + serverDetails := &config.ServerDetails{Url: "https://example.com"} + requestPayload := &model.CreateAppVersionRequest{ + ApplicationKey: "app-key", + Version: "1.0.0", + Packages: []model.CreateVersionPackage{ + { + Type: "type", + Name: "name", + Version: "1.0.0", + Repository: "repo", + }, + }, + } + + mockVersionService := mockversions.NewMockVersionService(ctrl) + mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), requestPayload). + Return(nil).Times(1) + + cmd := &createAppVersionCommand{ + versionService: mockVersionService, + serverDetails: serverDetails, + requestPayload: requestPayload, + } + + err := cmd.Run() + assert.NoError(t, err) +} + +func TestCreateAppVersionCommand_Run_ContextError(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + serverDetails := &config.ServerDetails{Url: "https://example.com"} + requestPayload := &model.CreateAppVersionRequest{ + ApplicationKey: "app-key", + Version: "1.0.0", + Packages: []model.CreateVersionPackage{ + { + Type: "type", + Name: "name", + Version: "1.0.0", + Repository: "repo", + }, + }, + } + + mockVersionService := mockversions.NewMockVersionService(ctrl) + mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), requestPayload). + Return(errors.New("context error")).Times(1) + + cmd := &createAppVersionCommand{ + versionService: mockVersionService, + serverDetails: serverDetails, + requestPayload: requestPayload, + } + + err := cmd.Run() + assert.Error(t, err) + assert.Equal(t, "context error", err.Error()) +} diff --git a/application/commands/version/promote_app_version_cmd.go b/application/commands/version/promote_app_version_cmd.go index d8274a2..d5de57b 100644 --- a/application/commands/version/promote_app_version_cmd.go +++ b/application/commands/version/promote_app_version_cmd.go @@ -1,5 +1,7 @@ package version +//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE} + import ( "github.com/jfrog/jfrog-cli-application/application/app" "github.com/jfrog/jfrog-cli-application/application/commands" @@ -7,6 +9,7 @@ import ( "github.com/jfrog/jfrog-cli-application/application/common" "github.com/jfrog/jfrog-cli-application/application/model" "github.com/jfrog/jfrog-cli-application/application/service" + "github.com/jfrog/jfrog-cli-application/application/service/versions" commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" "github.com/jfrog/jfrog-cli-core/v2/plugins/components" @@ -15,13 +18,17 @@ import ( ) type promoteAppVersionCommand struct { - versionService service.VersionService + versionService versions.VersionService serverDetails *coreConfig.ServerDetails requestPayload *model.PromoteAppVersionRequest } func (pv *promoteAppVersionCommand) Run() error { - ctx := &service.Context{ServerDetails: pv.serverDetails} + ctx, err := service.NewContext(*pv.serverDetails) + if err != nil { + return err + } + return pv.versionService.PromoteAppVersion(ctx, pv.requestPayload) } diff --git a/application/commands/version/promote_app_version_cmd_test.go b/application/commands/version/promote_app_version_cmd_test.go new file mode 100644 index 0000000..8af3f5f --- /dev/null +++ b/application/commands/version/promote_app_version_cmd_test.go @@ -0,0 +1,60 @@ +package version + +import ( + "testing" + + "github.com/jfrog/jfrog-cli-application/application/commands" + mockversions "github.com/jfrog/jfrog-cli-application/application/service/versions/mocks" + "go.uber.org/mock/gomock" + + "github.com/jfrog/jfrog-cli-application/application/model" + coreconfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" + "github.com/stretchr/testify/assert" +) + +func TestRun(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + serverDetails := &coreconfig.ServerDetails{} + requestPayload := &model.PromoteAppVersionRequest{ + ApplicationKey: "app", + Version: "1.0.0", + Environment: "env", + } + + mockVersionService := mockversions.NewMockVersionService(ctrl) + mockVersionService.EXPECT().PromoteAppVersion(gomock.Any(), requestPayload). + Return(nil).Times(1) + + cmd := &promoteAppVersionCommand{ + versionService: mockVersionService, + serverDetails: serverDetails, + requestPayload: requestPayload, + } + + err := cmd.Run() + assert.NoError(t, err) +} + +func TestServerDetails(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + serverDetails := &coreconfig.ServerDetails{} + cmd := &promoteAppVersionCommand{ + serverDetails: serverDetails, + } + + details, err := cmd.ServerDetails() + assert.NoError(t, err) + assert.Equal(t, serverDetails, details) +} + +func TestCommandName(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + cmd := &promoteAppVersionCommand{} + assert.Equal(t, commands.PromoteAppVersion, cmd.CommandName()) +} diff --git a/application/http/http_client.go b/application/http/http_client.go index 896167e..817b758 100644 --- a/application/http/http_client.go +++ b/application/http/http_client.go @@ -1,5 +1,7 @@ package http +//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE} + import ( "encoding/json" "fmt" diff --git a/application/service/context.go b/application/service/context.go index 8b59970..12f6b67 100644 --- a/application/service/context.go +++ b/application/service/context.go @@ -1,7 +1,35 @@ package service -import coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" +//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE} -type Context struct { - ServerDetails *coreConfig.ServerDetails +import ( + "github.com/jfrog/jfrog-cli-application/application/http" + coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" +) + +type Context interface { + GetServerDetails() coreConfig.ServerDetails + GetHttpClient() http.AppHttpClient +} + +type context struct { + ServerDetails coreConfig.ServerDetails + HttpClient http.AppHttpClient +} + +func (c *context) GetServerDetails() coreConfig.ServerDetails { + return c.ServerDetails +} + +func (c *context) GetHttpClient() http.AppHttpClient { + return c.HttpClient +} + +func NewContext(serverDetails coreConfig.ServerDetails) (Context, error) { + httpClient, err := http.NewAppHttpClient(&serverDetails) + if err != nil { + return nil, err + } + + return &context{ServerDetails: serverDetails, HttpClient: httpClient}, nil } diff --git a/application/service/context_test.go b/application/service/context_test.go new file mode 100644 index 0000000..5d7eb54 --- /dev/null +++ b/application/service/context_test.go @@ -0,0 +1,42 @@ +package service + +import ( + "testing" + + "github.com/jfrog/jfrog-cli-application/application/http" + coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" + "github.com/stretchr/testify/assert" +) + +func TestGetServerDetails(t *testing.T) { + serverDetails := coreConfig.ServerDetails{ + Url: "https://example.com", + } + ctx := &context{ServerDetails: serverDetails} + + assert.Equal(t, serverDetails, ctx.GetServerDetails()) +} + +func TestGetHttpClient(t *testing.T) { + serverDetails := coreConfig.ServerDetails{ + Url: "https://example.com", + } + httpClient, err := http.NewAppHttpClient(&serverDetails) + assert.NoError(t, err) + + ctx := &context{ServerDetails: serverDetails, HttpClient: httpClient} + + assert.Equal(t, httpClient, ctx.GetHttpClient()) +} + +func TestNewContext(t *testing.T) { + serverDetails := coreConfig.ServerDetails{ + Url: "https://example.com", + } + ctx, err := NewContext(serverDetails) + assert.NoError(t, err) + assert.NotNil(t, ctx) + + assert.Equal(t, serverDetails, ctx.GetServerDetails()) + assert.NotNil(t, ctx.GetHttpClient()) +} diff --git a/application/service/system_service.go b/application/service/system_service.go deleted file mode 100644 index 08c8335..0000000 --- a/application/service/system_service.go +++ /dev/null @@ -1,36 +0,0 @@ -package service - -import ( - "fmt" - - "github.com/jfrog/jfrog-cli-application/application/http" -) - -type SystemService interface { - Ping(ctx *Context) error -} - -type systemService struct{} - -func NewSystemService() SystemService { - return &systemService{} -} - -func (ss *systemService) Ping(ctx *Context) error { - httpClient, err := http.NewAppHttpClient(ctx.ServerDetails) - if err != nil { - return err - } - - response, body, err := httpClient.Get("/v1/system/ping") - if err != nil { - return err - } - - if response.StatusCode != 200 { - return fmt.Errorf("failed to create app version. Status code: %d", response.StatusCode) - } - - fmt.Println(string(body)) - return nil -} diff --git a/application/service/systems/system_service.go b/application/service/systems/system_service.go new file mode 100644 index 0000000..7c48885 --- /dev/null +++ b/application/service/systems/system_service.go @@ -0,0 +1,33 @@ +package systems + +//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE} + +import ( + "fmt" + + "github.com/jfrog/jfrog-cli-application/application/service" +) + +type SystemService interface { + Ping(ctx service.Context) error +} + +type systemService struct{} + +func NewSystemService() SystemService { + return &systemService{} +} + +func (ss *systemService) Ping(ctx service.Context) error { + response, body, err := ctx.GetHttpClient().Get("/v1/system/ping") + if err != nil { + return err + } + + if response.StatusCode != 200 { + return fmt.Errorf("failed pinging application service. Status code: %d", response.StatusCode) + } + + fmt.Println(string(body)) + return nil +} diff --git a/application/service/systems/system_service_test.go b/application/service/systems/system_service_test.go new file mode 100644 index 0000000..7a0e3f3 --- /dev/null +++ b/application/service/systems/system_service_test.go @@ -0,0 +1,72 @@ +package systems + +import ( + "errors" + "net/http" + "testing" + + mockservice "github.com/jfrog/jfrog-cli-application/application/service/mocks" + "go.uber.org/mock/gomock" + + mockhttp "github.com/jfrog/jfrog-cli-application/application/http/mocks" + "github.com/stretchr/testify/assert" +) + +func TestSystemService_Ping(t *testing.T) { + tests := []struct { + name string + mockResponse *http.Response + mockBody []byte + mockError error + expectedError error + }{ + { + name: "Ping successful", + mockResponse: &http.Response{ + StatusCode: 200, + }, + mockBody: []byte("pong"), + mockError: nil, + expectedError: nil, + }, + { + name: "Ping failed with non-200 status code", + mockResponse: &http.Response{ + StatusCode: 500, + }, + mockBody: []byte(""), + mockError: nil, + expectedError: errors.New("failed pinging application service. Status code: 500"), + }, + { + name: "Ping failed with error", + mockResponse: nil, + mockBody: nil, + mockError: errors.New("http error"), + expectedError: errors.New("http error"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockHttpClient := mockhttp.NewMockAppHttpClient(ctrl) + mockHttpClient.EXPECT().Get("/v1/system/ping"). + Return(tt.mockResponse, tt.mockBody, tt.mockError) + + mockCtx := mockservice.NewMockContext(ctrl) + mockCtx.EXPECT().GetHttpClient().Return(mockHttpClient).Times(1) + + ss := NewSystemService() + err := ss.Ping(mockCtx) + + if tt.expectedError != nil { + assert.EqualError(t, err, tt.expectedError.Error()) + } else { + assert.NoError(t, err) + } + }) + } +} diff --git a/application/service/version_service.go b/application/service/version_service.go deleted file mode 100644 index 193670d..0000000 --- a/application/service/version_service.go +++ /dev/null @@ -1,57 +0,0 @@ -package service - -import ( - "fmt" - - "github.com/jfrog/jfrog-cli-application/application/http" - "github.com/jfrog/jfrog-cli-application/application/model" -) - -type VersionService interface { - CreateAppVersion(ctx *Context, request *model.CreateAppVersionRequest) error - PromoteAppVersion(ctx *Context, payload *model.PromoteAppVersionRequest) error -} - -type versionService struct{} - -func NewVersionService() VersionService { - return &versionService{} -} - -func (vs *versionService) CreateAppVersion(ctx *Context, request *model.CreateAppVersionRequest) error { - httpClient, err := http.NewAppHttpClient(ctx.ServerDetails) - if err != nil { - return err - } - - response, responseBody, err := httpClient.Post("/v1/version", request) - if err != nil { - return err - } - - if response.StatusCode != 201 { - return fmt.Errorf("failed to create app version. Status code: %d. \n%s", - response.StatusCode, responseBody) - } - - return nil -} - -func (vs *versionService) PromoteAppVersion(ctx *Context, payload *model.PromoteAppVersionRequest) error { - httpClient, err := http.NewAppHttpClient(ctx.ServerDetails) - if err != nil { - return err - } - - response, responseBody, err := httpClient.Post("/v1/version/promote", payload) - if err != nil { - return err - } - - if response.StatusCode >= 400 { - return fmt.Errorf("failed to promote app version. Status code: %d. \n%s", - response.StatusCode, responseBody) - } - - return nil -} diff --git a/application/service/versions/version_service.go b/application/service/versions/version_service.go new file mode 100644 index 0000000..a5f68af --- /dev/null +++ b/application/service/versions/version_service.go @@ -0,0 +1,50 @@ +package versions + +//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE} + +import ( + "fmt" + + "github.com/jfrog/jfrog-cli-application/application/service" + + "github.com/jfrog/jfrog-cli-application/application/model" +) + +type VersionService interface { + CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest) error + PromoteAppVersion(ctx service.Context, payload *model.PromoteAppVersionRequest) error +} + +type versionService struct{} + +func NewVersionService() VersionService { + return &versionService{} +} + +func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest) error { + response, responseBody, err := ctx.GetHttpClient().Post("/v1/version", request) + if err != nil { + return err + } + + if response.StatusCode != 201 { + return fmt.Errorf("failed to create app version. Status code: %d. \n%s", + response.StatusCode, responseBody) + } + + return nil +} + +func (vs *versionService) PromoteAppVersion(ctx service.Context, payload *model.PromoteAppVersionRequest) error { + response, responseBody, err := ctx.GetHttpClient().Post("/v1/version/promote", payload) + if err != nil { + return err + } + + if response.StatusCode >= 400 { + return fmt.Errorf("failed to promote app version. Status code: %d. \n%s", + response.StatusCode, responseBody) + } + + return nil +} diff --git a/application/service/versions/version_service_test.go b/application/service/versions/version_service_test.go new file mode 100644 index 0000000..2ca4eab --- /dev/null +++ b/application/service/versions/version_service_test.go @@ -0,0 +1,134 @@ +package versions + +import ( + "errors" + "net/http" + "testing" + + mockhttp "github.com/jfrog/jfrog-cli-application/application/http/mocks" + mockservice "github.com/jfrog/jfrog-cli-application/application/service/mocks" + "go.uber.org/mock/gomock" + + "github.com/jfrog/jfrog-cli-application/application/model" + "github.com/stretchr/testify/assert" +) + +func TestCreateAppVersion(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + service := NewVersionService() + + tests := []struct { + name string + request *model.CreateAppVersionRequest + mockResponse *http.Response + mockResponseBody string + mockError error + expectedError string + }{ + { + name: "success", + request: &model.CreateAppVersionRequest{}, + mockResponse: &http.Response{StatusCode: 201}, + mockResponseBody: "{}", + mockError: nil, + expectedError: "", + }, + { + name: "failure", + request: &model.CreateAppVersionRequest{}, + mockResponse: &http.Response{StatusCode: 400}, + mockResponseBody: "error", + mockError: nil, + expectedError: "failed to create app version", + }, + { + name: "http client error", + request: &model.CreateAppVersionRequest{}, + mockResponse: nil, + mockResponseBody: "", + mockError: errors.New("http client error"), + expectedError: "http client error", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockHttpClient := mockhttp.NewMockAppHttpClient(ctrl) + mockHttpClient.EXPECT().Post("/v1/version", tt.request). + Return(tt.mockResponse, []byte(tt.mockResponseBody), tt.mockError).Times(1) + + mockCtx := mockservice.NewMockContext(ctrl) + mockCtx.EXPECT().GetHttpClient().Return(mockHttpClient).Times(1) + + err := service.CreateAppVersion(mockCtx, tt.request) + if tt.expectedError == "" { + assert.NoError(t, err) + } else { + assert.Error(t, err) + assert.Contains(t, err.Error(), tt.expectedError) + } + }) + } +} + +func TestPromoteAppVersion(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + service := NewVersionService() + + tests := []struct { + name string + payload *model.PromoteAppVersionRequest + mockResponse *http.Response + mockResponseBody string + mockError error + expectedError string + }{ + { + name: "success", + payload: &model.PromoteAppVersionRequest{}, + mockResponse: &http.Response{StatusCode: 200}, + mockResponseBody: "{}", + mockError: nil, + expectedError: "", + }, + { + name: "failure", + payload: &model.PromoteAppVersionRequest{}, + mockResponse: &http.Response{StatusCode: 400}, + mockResponseBody: "error", + mockError: nil, + expectedError: "failed to promote app version", + }, + { + name: "http client error", + payload: &model.PromoteAppVersionRequest{}, + mockResponse: nil, + mockResponseBody: "", + mockError: errors.New("http client error"), + expectedError: "http client error", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockHttpClient := mockhttp.NewMockAppHttpClient(ctrl) + mockHttpClient.EXPECT().Post("/v1/version/promote", tt.payload). + Return(tt.mockResponse, []byte(tt.mockResponseBody), tt.mockError).Times(1) + + mockCtx := mockservice.NewMockContext(ctrl) + mockCtx.EXPECT().GetHttpClient().Return(mockHttpClient).Times(1) + + err := service.PromoteAppVersion(mockCtx, tt.payload) + if tt.expectedError == "" { + assert.NoError(t, err) + } else { + assert.Error(t, err) + assert.Contains(t, err.Error(), tt.expectedError) + } + }) + } +} diff --git a/go.mod b/go.mod index 48156ae..7e93e0b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.23.4 require ( github.com/jfrog/jfrog-cli-core/v2 v2.57.6 github.com/jfrog/jfrog-client-go v1.49.0 + github.com/stretchr/testify v1.10.0 + go.uber.org/mock v0.5.0 ) require ( @@ -19,13 +21,14 @@ require ( github.com/chzyer/readline v1.5.1 // indirect github.com/cloudflare/circl v1.5.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect - github.com/cyphar/filepath-securejoin v0.3.6 // indirect + github.com/cyphar/filepath-securejoin v0.4.0 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dsnet/compress v0.0.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/forPelevin/gomoji v1.2.0 // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect - github.com/go-git/go-billy/v5 v5.6.1 // indirect + github.com/go-git/go-billy/v5 v5.6.2 // indirect github.com/go-git/go-git/v5 v5.13.1 // indirect github.com/golang-jwt/jwt/v4 v4.5.1 // indirect github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect @@ -44,7 +47,7 @@ require ( github.com/klauspost/pgzip v1.2.6 // indirect github.com/magiconair/properties v1.8.9 // indirect github.com/manifoldco/promptui v0.9.0 // indirect - github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/mattn/go-tty v0.0.7 // indirect @@ -57,14 +60,15 @@ require ( github.com/pjbgf/sha1cd v0.3.1 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect github.com/pkg/term v1.2.0-beta.2 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/sagikazarmark/locafero v0.6.0 // indirect + github.com/sagikazarmark/locafero v0.7.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.3.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect - github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/afero v1.12.0 // indirect github.com/spf13/cast v1.7.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.19.0 // indirect diff --git a/go.sum b/go.sum index b8a7c87..6301574 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/cloudflare/circl v1.5.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZ github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= -github.com/cyphar/filepath-securejoin v0.3.6 h1:4d9N5ykBnSp5Xn2JkhocYDkOpURL/18CYMpo6xB9uWM= -github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI= +github.com/cyphar/filepath-securejoin v0.4.0 h1:PioTG9TBRSApBpYGnDU8HC+miIsX8vitBH9LGNNMoLQ= +github.com/cyphar/filepath-securejoin v0.4.0/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -62,8 +62,8 @@ github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c= github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= -github.com/go-git/go-billy/v5 v5.6.1 h1:u+dcrgaguSSkbjzHwelEjc0Yj300NUevrrPphk/SoRA= -github.com/go-git/go-billy/v5 v5.6.1/go.mod h1:0AsLr1z2+Uksi4NlElmMblP5rPcDZNRCD8ujZCRR2BE= +github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM= +github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.13.1 h1:DAQ9APonnlvSWpvolXWIuV6Q6zXy2wHbN4cVlNR5Q+M= @@ -119,12 +119,11 @@ github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYt github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= -github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= @@ -166,8 +165,8 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= -github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= +github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo= +github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= @@ -177,8 +176,8 @@ github.com/skeema/knownhosts v1.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0 github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= -github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= -github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs= +github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4= github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -222,6 +221,8 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavM github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= +go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= +go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= @@ -252,7 +253,6 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= diff --git a/.github/scripts/gotest.sh b/scripts/gotest.sh similarity index 87% rename from .github/scripts/gotest.sh rename to scripts/gotest.sh index 507fa9f..c2d9900 100755 --- a/.github/scripts/gotest.sh +++ b/scripts/gotest.sh @@ -69,18 +69,16 @@ fi echoDebug "Running ${GOCMD} test ${modargs[*]}" # Disable log coloring (ANSI codes are invalid xml characters) -(2>&1 DEV_DISABLE_LOG_COLORS=true ${GOCMD} test ${modargs[*]} || echo "$?" > "${exitCodeFile}") | tee "${OUTFILE}" +(2>&1 DEV_DISABLE_LOG_COLORS=true ${GOCMD} test "${modargs[*]}" || echo "$?" > "${exitCodeFile}") | tee "${OUTFILE}" exitCode="$(cat "${exitCodeFile}")" echoDebug "Tests Exit Code: $exitCode" if [[ -n "${JSON_OUTFILE}" ]]; then - echoDebug "Gernerating JSON test report at: ${JSON_OUTFILE}" + echoDebug "Generating JSON test report at: ${JSON_OUTFILE}" go tool test2json < "${OUTFILE}" > "${JSON_OUTFILE}" fi if [[ -n "${XUNIT_OUTFILE}" ]]; then - echoDebug "Ensuring jstemmer/go-junit-report is installed" - ${GOCMD} install github.com/jstemmer/go-junit-report@v1.0.0 echoDebug "Generating xUnit test report at: ${XUNIT_OUTFILE}" go-junit-report < "${OUTFILE}" > "${XUNIT_OUTFILE}" fi diff --git a/scripts/mockgen.sh b/scripts/mockgen.sh new file mode 100755 index 0000000..a621f11 --- /dev/null +++ b/scripts/mockgen.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +FILENAME=${1%.*} +DEBUG=${DEBUG:-false} +SUB_PACKAGE=mocks/ +MOCK_PACKAGE="" + +function echoDebug() { + if [[ "${DEBUG}" == "true" ]]; then + echo "[mockgen.sh] [DEBUG] $@" + fi +} + +while [ -n "$1" ]; do + case $1 in + "--no-sub-package") + SUB_PACKAGE="" + ;; + "--package") + shift + if [ -n "$1" ]; then + MOCK_PACKAGE="-package=$1" + fi + ;; + esac + shift +done + +test -z "${SUB_PACKAGE}" || mkdir -p "${SUB_PACKAGE}" + +PROJECT_DIR="${PROJECT_DIR:-}" +echoDebug "Generating mocks for file: ${PWD#${PROJECT_DIR}/}/$FILE" +"${TOOLS_DIR}"/mockgen -source="${FILENAME}.go" -destination="${SUB_PACKAGE}${FILENAME}_mock.go" "${MOCK_PACKAGE}"