shut error printing in unit test cases for gcfg/gview

This commit is contained in:
John 2019-06-13 20:29:40 +08:00
parent 9cddb7ed9a
commit 7348d14fef
7 changed files with 96 additions and 18 deletions

View File

@ -57,7 +57,9 @@ func New(file...string) *Config {
if gfile.Exists(envPath) {
_ = c.SetPath(envPath)
} else {
glog.Errorf("Configuration directory path does not exist: %s", envPath)
if errorPrint() {
glog.Errorf("Configuration directory path does not exist: %s", envPath)
}
}
} else {
// Dir path of working dir.
@ -97,7 +99,9 @@ func (c *Config) filePath(file...string) (path string) {
} else {
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" with no path set/add", name))
}
glog.Error(buffer.String())
if errorPrint() {
glog.Error(buffer.String())
}
}
return path
}
@ -133,13 +137,17 @@ func (c *Config) SetPath(path string) error {
buffer.WriteString(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" does not exist`, path))
}
err := errors.New(buffer.String())
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// Should be a directory.
if !gfile.IsDir(realPath) {
err := errors.New(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" should be directory type`, path))
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// Repeated path check.
@ -191,12 +199,16 @@ func (c *Config) AddPath(path string) error {
buffer.WriteString(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" does not exist`, path))
}
err := errors.New(buffer.String())
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
if !gfile.IsDir(realPath) {
err := errors.New(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" should be directory type`, path))
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// Repeated path check.
@ -275,11 +287,13 @@ func (c *Config) getJson(file...string) *gjson.Json {
}
return j
} else {
if filePath != "" {
glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error())
} else {
glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error())
}
if errorPrint() {
if filePath != "" {
glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error())
} else {
glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error())
}
}
}
return nil
})

22
g/os/gcfg/gcfg_error.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcfg
import (
"github.com/gogf/gf/g/internal/cmdenv"
)
const (
// gERROR_PRINT_KEY is used to specify the key controlling error printing to stdout.
// This error is designed not to be returned by functions.
gERROR_PRINT_KEY = "gf.gcfg.errorprint"
)
// errorPrint checks whether printing error to stdout.
func errorPrint() bool {
return cmdenv.Get(gERROR_PRINT_KEY, true).Bool()
}

View File

@ -19,6 +19,10 @@ import (
"testing"
)
func init() {
os.Setenv("GF_GCFG_ERRORPRINT", "false")
}
func Test_Basic(t *testing.T) {
config := `
v1 = 1
@ -379,7 +383,7 @@ func TestCfg_Get(t *testing.T) {
gtest.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12")
gtest.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12")
gtest.Assert(c.GetDuration("time").String(), "0s")
t.Log(c.GetString("person"))
//t.Log(c.GetString("person"))
err := c.GetToStruct("person", &name)
gtest.Assert(err, nil)
gtest.Assert(name.Name, "gf")

View File

@ -69,7 +69,9 @@ func New(path...string) *View {
if gfile.Exists(envPath) {
view.SetPath(envPath)
} else {
glog.Errorf("Template directory path does not exist: %s", envPath)
if errorPrint() {
glog.Errorf("Template directory path does not exist: %s", envPath)
}
}
} else {
// Dir path of working dir.
@ -146,13 +148,17 @@ func (view *View) SetPath(path string) error {
buffer.WriteString(fmt.Sprintf(`[gview] SetPath failed: path "%s" does not exist`, path))
}
err := errors.New(buffer.String())
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// Should be a directory.
if !gfile.IsDir(realPath) {
err := errors.New(fmt.Sprintf(`[gview] SetPath failed: path "%s" should be directory type`, path))
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// Repeated path check.
@ -194,13 +200,17 @@ func (view *View) AddPath(path string) error {
buffer.WriteString(fmt.Sprintf(`[gview] AddPath failed: path "%s" does not exist`, path))
}
err := errors.New(buffer.String())
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// realPath should be type of folder.
if !gfile.IsDir(realPath) {
err := errors.New(fmt.Sprintf(`[gview] AddPath failed: path "%s" should be directory type`, path))
glog.Error(err)
if errorPrint() {
glog.Error(err)
}
return err
}
// Repeated path check.

View File

@ -88,7 +88,9 @@ func (view *View) searchFile(file string) (path string, folder string, err error
} else {
buffer.WriteString(fmt.Sprintf("[gview] cannot find template file \"%s\" with no path set/add", file))
}
glog.Error(buffer.String())
if errorPrint() {
glog.Error(buffer.String())
}
err = errors.New(fmt.Sprintf(`template file "%s" not found`, file))
}
return

22
g/os/gview/gview_error.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gview
import (
"github.com/gogf/gf/g/internal/cmdenv"
)
const (
// gERROR_PRINT_KEY is used to specify the key controlling error printing to stdout.
// This error is designed not to be returned by functions.
gERROR_PRINT_KEY = "gf.gview.errorprint"
)
// errorPrint checks whether printing error to stdout.
func errorPrint() bool {
return cmdenv.Get(gERROR_PRINT_KEY, true).Bool()
}

View File

@ -11,6 +11,10 @@ import (
"testing"
)
func init() {
os.Setenv("GF_GVIEW_ERRORPRINT", "false")
}
func TestView_Basic(t *testing.T) {
gtest.Case(t, func() {
str := `hello {{.name}},version:{{.version}};hello {{GetName}},version:{{GetVersion}};{{.other}}`