From 3ce2c3fddc6f3926fd6389f90ff9e169a0517057 Mon Sep 17 00:00:00 2001 From: ysicing Date: Fri, 24 May 2024 15:50:03 +0800 Subject: [PATCH] feat: refactor GetVersion function and add test cases in common/func_test.go - Change the second parameter in the `GetVersion` function from `p` to `typ` - Remove the metadata line with `if len(v) != 2 {` and the closing bracket `}` at the end of the function - Remove the return statement `return v[1]` and add a new return statement `return version` - Add a new file `common/func_test.go` - Add two test cases for the `GetVersion` function in the new file Signed-off-by: ysicing --- common/func.go | 36 +++++++++++++++++-------------- common/func_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ pkg/quickon/quickon.go | 2 +- 3 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 common/func_test.go diff --git a/common/func.go b/common/func.go index b8ef4a44..b0b448b7 100644 --- a/common/func.go +++ b/common/func.go @@ -60,24 +60,28 @@ func GetChannel(p string) string { } // GetVersion 获取版本地址 -func GetVersion(devops bool, p, version string) string { +func GetVersion(devops bool, typ, version string) string { + if !devops { + // 渠成不支持版本 + return DefaultQuickonOSSVersion + } v := strings.Split(version, "-") - if len(v) != 2 { - switch p { - case string(ZenTaoIPDType): - return DefaultZentaoDevOPSIPDVersion - case string(ZenTaoBizType): - return DefaultZentaoDevOPSBizVersion - case string(ZenTaoMaxType): - return DefaultZentaoDevOPSMaxVersion - default: - if devops { - return DefaultZentaoDevOPSOSSVersion - } - return DefaultQuickonOSSVersion - } + if len(v) == 2 { + return v[1] + } + defaultVersion := DefaultZentaoDevOPSOSSVersion + switch typ { + case string(ZenTaoIPDType): + defaultVersion = DefaultZentaoDevOPSIPDVersion + case string(ZenTaoBizType): + defaultVersion = DefaultZentaoDevOPSBizVersion + case string(ZenTaoMaxType): + defaultVersion = DefaultZentaoDevOPSMaxVersion + } + if version == defaultVersion || len(version) == 0 { + return defaultVersion } - return v[1] + return version } func GetDefaultConfig() string { diff --git a/common/func_test.go b/common/func_test.go new file mode 100644 index 00000000..66bfb66b --- /dev/null +++ b/common/func_test.go @@ -0,0 +1,48 @@ +// Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. +// Use of this source code is covered by the following dual licenses: +// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) +// (2) Affero General Public License 3.0 (AGPL 3.0) +// license that can be found in the LICENSE file. + +package common + +import "testing" + +func TestGetVersion(t *testing.T) { + type args struct { + devops bool + typ string + version string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "test1", + args: args{ + devops: true, + typ: "oss", + version: "v1.0.0", + }, + want: "v1.0.0", + }, + { + name: "test2", + args: args{ + devops: true, + typ: "oss", + version: "", + }, + want: DefaultZentaoDevOPSOSSVersion, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetVersion(tt.args.devops, tt.args.typ, tt.args.version); got != tt.want { + t.Logf("GetVersion() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/quickon/quickon.go b/pkg/quickon/quickon.go index a95239e1..e6efb946 100644 --- a/pkg/quickon/quickon.go +++ b/pkg/quickon/quickon.go @@ -228,7 +228,7 @@ func (m *Meta) Init() error { } } installVersion := common.GetVersion(m.DevopsMode, m.Type, m.Version) - m.Log.Infof("devops: %v, type: %s, version: %s(%s), channel: %s", m.DevopsMode, m.Type, m.Version, installVersion, common.GetChannel(m.Version)) + m.Log.Infof("devops: %v, type: %s, version: %s, channel: %s", m.DevopsMode, m.Type, installVersion, common.GetChannel(m.Version)) m.Log.Debugf("start init %s", common.GetInstallType(m.DevopsMode)) cfg.Quickon.Type = common.QuickonType(m.Type) cfg.Quickon.DevOps = m.DevopsMode