From 3cdad8a54e137da20107e2a58646298f11830ce2 Mon Sep 17 00:00:00 2001 From: dongjiang1989 <dongjiang1989@126.com> Date: Fri, 25 Oct 2024 10:23:37 +0800 Subject: [PATCH] add unittest case Signed-off-by: dongjiang1989 <dongjiang1989@126.com> --- .github/workflows/publish.yaml | 2 +- Makefile | 1 + pkg/version/version_test.go | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 pkg/version/version_test.go diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 07dceeb0..32d3c2d5 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -34,7 +34,7 @@ jobs: uses: actions/checkout@v4 - id: git-x run: | - echo "git-version=$(git describe --tags --always)" >> "$GITHUB_OUTPUT" + echo "git-version=$(echo \"${GITHUB_SHA:-$(git rev-parse HEAD)}\" | cut -c1-8)" >> "$GITHUB_OUTPUT" - id: git-branch run: | echo "git-branch=$(echo ${GITHUB_REF##*/} | tr '[A-Z]' '[a-z]')" >> "$GITHUB_OUTPUT" diff --git a/Makefile b/Makefile index be35d62b..e2d86023 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ TOOLING=$(GOLANGCILINTER_BINARY) $(GOSEC_BINARY) $(GOSYCLO_BINARY) $(SWAGGO_BINA GO_PKG=github.com/kubeservice-stack/$(SERVER_NAME) COMMON_PKG ?= $(GO_PKG)/pkg COMMON_CMD ?= $(GO_PKG)/cmd +COMMON_INTERNAL ?= $(GO_PKG)/internal # The ldflags for the go build process to set the version related data. GO_BUILD_LDFLAGS=\ -s \ diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go new file mode 100644 index 00000000..90bf9e45 --- /dev/null +++ b/pkg/version/version_test.go @@ -0,0 +1,41 @@ +/* +Copyright 2024 The KubeService-Stack Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package version + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestVersionInfo(t *testing.T) { + assert := assert.New(t) + info := Info() + assert.Equal("(version=, branch=, revision=unknown)", info) +} + +func TestVersionBuildContext(t *testing.T) { + assert := assert.New(t) + info := BuildContext() + assert.Equal("(go=go1.22.0, platform=darwin/amd64, user=, date=, tags=unknown)", info) +} + +func TestVersionPrint(t *testing.T) { + assert := assert.New(t) + info := Print("echo") + assert.NotEmpty(info) +}