diff --git a/CHANGELOG.md b/CHANGELOG.md index 621e3042..beafabcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,4 +15,7 @@ - Error Module brief design & using httplib module to validate this design. [4453](https://github.com/beego/beego/pull/4453) - Fix 4444: panic when 404 not found. [4446](https://github.com/beego/beego/pull/4446) - Fix 4435: fix panic when controller dir not found. [4452](https://github.com/beego/beego/pull/4452) -- Fix 4456: Fix router method expression [4456](https://github.com/beego/beego/pull/4456) \ No newline at end of file +- Fix 4456: Fix router method expression [4456](https://github.com/beego/beego/pull/4456) + +## Fix Sonar +- []() \ No newline at end of file diff --git a/adapter/cache/cache_test.go b/adapter/cache/cache_test.go index f6217e1a..382a3743 100644 --- a/adapter/cache/cache_test.go +++ b/adapter/cache/cache_test.go @@ -21,10 +21,18 @@ import ( "time" ) +const ( + initError = "init err" + setError = "set Error" + checkError = "check err" + getError = "get err" + getMultiError = "GetMulti Error" +) + func TestCacheIncr(t *testing.T) { bm, err := NewCache("memory", `{"interval":20}`) if err != nil { - t.Error("init err") + t.Error(initError) } // timeoutDuration := 10 * time.Second @@ -46,28 +54,28 @@ func TestCacheIncr(t *testing.T) { func TestCache(t *testing.T) { bm, err := NewCache("memory", `{"interval":20}`) if err != nil { - t.Error("init err") + t.Error(initError) } timeoutDuration := 10 * time.Second if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if v := bm.Get("astaxie"); v.(int) != 1 { - t.Error("get err") + t.Error(getError) } time.Sleep(30 * time.Second) if bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if err = bm.Incr("astaxie"); err != nil { @@ -75,7 +83,7 @@ func TestCache(t *testing.T) { } if v := bm.Get("astaxie"); v.(int) != 2 { - t.Error("get err") + t.Error(getError) } if err = bm.Decr("astaxie"); err != nil { @@ -83,7 +91,7 @@ func TestCache(t *testing.T) { } if v := bm.Get("astaxie"); v.(int) != 1 { - t.Error("get err") + t.Error(getError) } bm.Delete("astaxie") if bm.IsExist("astaxie") { @@ -92,49 +100,49 @@ func TestCache(t *testing.T) { // test GetMulti if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if v := bm.Get("astaxie"); v.(string) != "author" { - t.Error("get err") + t.Error(getError) } if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie1") { - t.Error("check err") + t.Error(checkError) } vv := bm.GetMulti([]string{"astaxie", "astaxie1"}) if len(vv) != 2 { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if vv[0].(string) != "author" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if vv[1].(string) != "author1" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } } func TestFileCache(t *testing.T) { bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`) if err != nil { - t.Error("init err") + t.Error(initError) } timeoutDuration := 10 * time.Second if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if v := bm.Get("astaxie"); v.(int) != 1 { - t.Error("get err") + t.Error(getError) } if err = bm.Incr("astaxie"); err != nil { @@ -142,7 +150,7 @@ func TestFileCache(t *testing.T) { } if v := bm.Get("astaxie"); v.(int) != 2 { - t.Error("get err") + t.Error(getError) } if err = bm.Decr("astaxie"); err != nil { @@ -150,7 +158,7 @@ func TestFileCache(t *testing.T) { } if v := bm.Get("astaxie"); v.(int) != 1 { - t.Error("get err") + t.Error(getError) } bm.Delete("astaxie") if bm.IsExist("astaxie") { @@ -159,32 +167,32 @@ func TestFileCache(t *testing.T) { // test string if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if v := bm.Get("astaxie"); v.(string) != "author" { - t.Error("get err") + t.Error(getError) } // test GetMulti if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie1") { - t.Error("check err") + t.Error(checkError) } vv := bm.GetMulti([]string{"astaxie", "astaxie1"}) if len(vv) != 2 { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if vv[0].(string) != "author" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if vv[1].(string) != "author1" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } os.RemoveAll("cache") diff --git a/adapter/cache/memcache/memcache_test.go b/adapter/cache/memcache/memcache_test.go index 6382543e..ae0c6df7 100644 --- a/adapter/cache/memcache/memcache_test.go +++ b/adapter/cache/memcache/memcache_test.go @@ -24,6 +24,14 @@ import ( "github.com/beego/beego/v2/adapter/cache" ) +const ( + initError = "init err" + setError = "set Error" + checkError = "check err" + getError = "get err" + getMultiError = "GetMulti Error" +) + func TestMemcacheCache(t *testing.T) { addr := os.Getenv("MEMCACHE_ADDR") @@ -33,27 +41,27 @@ func TestMemcacheCache(t *testing.T) { bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, addr)) if err != nil { - t.Error("init err") + t.Error(initError) } timeoutDuration := 10 * time.Second if err = bm.Put("astaxie", "1", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } time.Sleep(11 * time.Second) if bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if err = bm.Put("astaxie", "1", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 { - t.Error("get err") + t.Error(getError) } if err = bm.Incr("astaxie"); err != nil { @@ -61,7 +69,7 @@ func TestMemcacheCache(t *testing.T) { } if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 2 { - t.Error("get err") + t.Error(getError) } if err = bm.Decr("astaxie"); err != nil { @@ -69,7 +77,7 @@ func TestMemcacheCache(t *testing.T) { } if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 { - t.Error("get err") + t.Error(getError) } bm.Delete("astaxie") if bm.IsExist("astaxie") { @@ -78,33 +86,33 @@ func TestMemcacheCache(t *testing.T) { // test string if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if v := bm.Get("astaxie").([]byte); string(v) != "author" { - t.Error("get err") + t.Error(getError) } // test GetMulti if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie1") { - t.Error("check err") + t.Error(checkError) } vv := bm.GetMulti([]string{"astaxie", "astaxie1"}) if len(vv) != 2 { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if string(vv[0].([]byte)) != "author" && string(vv[0].([]byte)) != "author1" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if string(vv[1].([]byte)) != "author1" && string(vv[1].([]byte)) != "author" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } // test clear all diff --git a/adapter/cache/redis/redis_test.go b/adapter/cache/redis/redis_test.go index 39a30e8e..781489be 100644 --- a/adapter/cache/redis/redis_test.go +++ b/adapter/cache/redis/redis_test.go @@ -25,6 +25,14 @@ import ( "github.com/beego/beego/v2/adapter/cache" ) +const ( + initError = "init err" + setError = "set Error" + checkError = "check err" + getError = "get err" + getMultiError = "GetMulti Error" +) + func TestRedisCache(t *testing.T) { redisAddr := os.Getenv("REDIS_ADDR") if redisAddr == "" { @@ -33,27 +41,27 @@ func TestRedisCache(t *testing.T) { bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr)) if err != nil { - t.Error("init err") + t.Error(initError) } timeoutDuration := 10 * time.Second if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } time.Sleep(11 * time.Second) if bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 { - t.Error("get err") + t.Error(getError) } if err = bm.Incr("astaxie"); err != nil { @@ -61,7 +69,7 @@ func TestRedisCache(t *testing.T) { } if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 { - t.Error("get err") + t.Error(getError) } if err = bm.Decr("astaxie"); err != nil { @@ -69,7 +77,7 @@ func TestRedisCache(t *testing.T) { } if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 { - t.Error("get err") + t.Error(getError) } bm.Delete("astaxie") if bm.IsExist("astaxie") { @@ -78,33 +86,33 @@ func TestRedisCache(t *testing.T) { // test string if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie") { - t.Error("check err") + t.Error(checkError) } if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" { - t.Error("get err") + t.Error(getError) } // test GetMulti if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !bm.IsExist("astaxie1") { - t.Error("check err") + t.Error(checkError) } vv := bm.GetMulti([]string{"astaxie", "astaxie1"}) if len(vv) != 2 { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if v, _ := redis.String(vv[0], nil); v != "author" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } if v, _ := redis.String(vv[1], nil); v != "author1" { - t.Error("GetMulti ERROR") + t.Error(getMultiError) } // test clear all @@ -118,12 +126,12 @@ func TestCache_Scan(t *testing.T) { // init bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`) if err != nil { - t.Error("init err") + t.Error(initError) } // insert all for i := 0; i < 10000; i++ { if err = bm.Put(fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } } diff --git a/adapter/cache/ssdb/ssdb_test.go b/adapter/cache/ssdb/ssdb_test.go index 98e805d1..f2f72f81 100644 --- a/adapter/cache/ssdb/ssdb_test.go +++ b/adapter/cache/ssdb/ssdb_test.go @@ -10,6 +10,14 @@ import ( "github.com/beego/beego/v2/adapter/cache" ) +const ( + initError = "init err" + setError = "set Error" + checkError = "check err" + getError = "get err" + getMultiError = "GetMulti Error" +) + func TestSsdbcacheCache(t *testing.T) { ssdbAddr := os.Getenv("SSDB_ADDR") if ssdbAddr == "" { @@ -18,25 +26,25 @@ func TestSsdbcacheCache(t *testing.T) { ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr)) if err != nil { - t.Error("init err") + t.Error(initError) } // test put and exist if ssdb.IsExist("ssdb") { - t.Error("check err") + t.Error(checkError) } timeoutDuration := 10 * time.Second // timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !ssdb.IsExist("ssdb") { - t.Error("check err") + t.Error(checkError) } // Get test done if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if v := ssdb.Get("ssdb"); v != "ssdb" { @@ -45,14 +53,14 @@ func TestSsdbcacheCache(t *testing.T) { // inc/dec test done if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if err = ssdb.Incr("ssdb"); err != nil { t.Error("incr Error", err) } if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 { - t.Error("get err") + t.Error(getError) } if err = ssdb.Decr("ssdb"); err != nil { @@ -61,10 +69,10 @@ func TestSsdbcacheCache(t *testing.T) { // test del if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 { - t.Error("get err") + t.Error(getError) } if err := ssdb.Delete("ssdb"); err == nil { if ssdb.IsExist("ssdb") { @@ -74,31 +82,31 @@ func TestSsdbcacheCache(t *testing.T) { // test string if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !ssdb.IsExist("ssdb") { - t.Error("check err") + t.Error(checkError) } if v := ssdb.Get("ssdb").(string); v != "ssdb" { - t.Error("get err") + t.Error(getError) } // test GetMulti done if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil { - t.Error("set Error", err) + t.Error(setError, err) } if !ssdb.IsExist("ssdb1") { - t.Error("check err") + t.Error(checkError) } vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"}) if len(vv) != 2 { - t.Error("getmulti error") + t.Error(getMultiError) } if vv[0].(string) != "ssdb" { - t.Error("getmulti error") + t.Error(getMultiError) } if vv[1].(string) != "ssdb1" { - t.Error("getmulti error") + t.Error(getMultiError) } // test clear all done @@ -106,6 +114,6 @@ func TestSsdbcacheCache(t *testing.T) { t.Error("clear all err") } if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") { - t.Error("check err") + t.Error(checkError) } } diff --git a/adapter/config/ini_test.go b/adapter/config/ini_test.go index 60f1febd..07992ba7 100644 --- a/adapter/config/ini_test.go +++ b/adapter/config/ini_test.go @@ -81,7 +81,8 @@ password = ${GOPATH} } ) - f, err := os.Create("testini.conf") + cfgFile := "testini.conf" + f, err := os.Create(cfgFile) if err != nil { t.Fatal(err) } @@ -91,8 +92,8 @@ password = ${GOPATH} t.Fatal(err) } f.Close() - defer os.Remove("testini.conf") - iniconf, err := NewConfig("ini", "testini.conf") + defer os.Remove(cfgFile) + iniconf, err := NewConfig("ini", cfgFile) if err != nil { t.Fatal(err) } diff --git a/adapter/config/json_test.go b/adapter/config/json_test.go index 16f42409..b3c75dc1 100644 --- a/adapter/config/json_test.go +++ b/adapter/config/json_test.go @@ -32,7 +32,8 @@ func TestJsonStartsWithArray(t *testing.T) { "serviceAPI": "http://www.test.com/employee" } ]` - f, err := os.Create("testjsonWithArray.conf") + cfgFileName := "testjsonWithArray.conf" + f, err := os.Create(cfgFileName) if err != nil { t.Fatal(err) } @@ -42,8 +43,8 @@ func TestJsonStartsWithArray(t *testing.T) { t.Fatal(err) } f.Close() - defer os.Remove("testjsonWithArray.conf") - jsonconf, err := NewConfig("json", "testjsonWithArray.conf") + defer os.Remove(cfgFileName) + jsonconf, err := NewConfig("json", cfgFileName) if err != nil { t.Fatal(err) } @@ -132,7 +133,8 @@ func TestJson(t *testing.T) { } ) - f, err := os.Create("testjson.conf") + cfgFileName := "testjson.conf" + f, err := os.Create(cfgFileName) if err != nil { t.Fatal(err) } @@ -142,8 +144,8 @@ func TestJson(t *testing.T) { t.Fatal(err) } f.Close() - defer os.Remove("testjson.conf") - jsonconf, err := NewConfig("json", "testjson.conf") + defer os.Remove(cfgFileName) + jsonconf, err := NewConfig("json", cfgFileName) if err != nil { t.Fatal(err) } diff --git a/adapter/config/xml/xml_test.go b/adapter/config/xml/xml_test.go index 5e43ca0f..95b21fd9 100644 --- a/adapter/config/xml/xml_test.go +++ b/adapter/config/xml/xml_test.go @@ -58,7 +58,8 @@ func TestXML(t *testing.T) { } ) - f, err := os.Create("testxml.conf") + cfgFileName := "testxml.conf" + f, err := os.Create(cfgFileName) if err != nil { t.Fatal(err) } @@ -68,9 +69,9 @@ func TestXML(t *testing.T) { t.Fatal(err) } f.Close() - defer os.Remove("testxml.conf") + defer os.Remove(cfgFileName) - xmlconf, err := config.NewConfig("xml", "testxml.conf") + xmlconf, err := config.NewConfig("xml", cfgFileName) if err != nil { t.Fatal(err) } diff --git a/adapter/config/yaml/yaml_test.go b/adapter/config/yaml/yaml_test.go index d567b554..323b5e87 100644 --- a/adapter/config/yaml/yaml_test.go +++ b/adapter/config/yaml/yaml_test.go @@ -54,7 +54,8 @@ func TestYaml(t *testing.T) { "emptystrings": []string{}, } ) - f, err := os.Create("testyaml.conf") + cfgFileName := "testyaml.conf" + f, err := os.Create(cfgFileName) if err != nil { t.Fatal(err) } @@ -64,8 +65,8 @@ func TestYaml(t *testing.T) { t.Fatal(err) } f.Close() - defer os.Remove("testyaml.conf") - yamlconf, err := config.NewConfig("yaml", "testyaml.conf") + defer os.Remove(cfgFileName) + yamlconf, err := config.NewConfig("yaml", cfgFileName) if err != nil { t.Fatal(err) } diff --git a/adapter/context/param/conv_test.go b/adapter/context/param/conv_test.go index c27d385a..9f6204b4 100644 --- a/adapter/context/param/conv_test.go +++ b/adapter/context/param/conv_test.go @@ -23,6 +23,8 @@ import ( "github.com/beego/beego/v2/adapter/context" ) + +// Demo is used to test, it's empty func Demo(i int) { } diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index e2e25ac4..db5597f9 100644 --- a/client/orm/orm_test.go +++ b/client/orm/orm_test.go @@ -85,11 +85,7 @@ func ValuesCompare(is bool, a interface{}, args ...interface{}) (ok bool, err er } ok = is && ok || !is && !ok if !ok { - if is { - err = fmt.Errorf("expected: `%v`, get `%v`", b, a) - } else { - err = fmt.Errorf("expected: `%v`, get `%v`", b, a) - } + err = fmt.Errorf("expected: `%v`, get `%v`", b, a) } wrongArg: