summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLvbo163 <lv.bo163@zte.com.cn>2018-08-01 09:23:00 +0800
committerLvbo163 <lv.bo163@zte.com.cn>2018-08-01 09:23:00 +0800
commit45198f48b37f69c1b4764f93f423c5ceeaece06a (patch)
tree814752f8f54169265b8b18859643f50fdc97c3d0
parent9fc37cf8ae07db747c5c7745b01b33085761ba0b (diff)
add ut for app setting
uts for getConfigPath Issue-ID: MSB-240 Change-Id: I440a29be3fc0d922b5c2a98bafb7787897867c7a Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
-rw-r--r--msb2pilot/src/msb2pilot/util/common.go4
-rw-r--r--msb2pilot/src/msb2pilot/util/common_test.go102
2 files changed, 105 insertions, 1 deletions
diff --git a/msb2pilot/src/msb2pilot/util/common.go b/msb2pilot/src/msb2pilot/util/common.go
index 6053116..1bb10df 100644
--- a/msb2pilot/src/msb2pilot/util/common.go
+++ b/msb2pilot/src/msb2pilot/util/common.go
@@ -58,10 +58,12 @@ func GetGoPath() []string {
return strings.Split(paths, ";")
} else if strings.Contains(paths, ":") { // linux
return strings.Split(paths, ":")
- } else { // only one
+ } else if paths != "" { // only one
path := make([]string, 1, 1)
path[0] = paths
return path
+ } else {
+ return make([]string, 0, 0)
}
}
diff --git a/msb2pilot/src/msb2pilot/util/common_test.go b/msb2pilot/src/msb2pilot/util/common_test.go
new file mode 100644
index 0000000..6204b6f
--- /dev/null
+++ b/msb2pilot/src/msb2pilot/util/common_test.go
@@ -0,0 +1,102 @@
+/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and the Apache License 2.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial Project
+ */
+package util
+
+import (
+ "os"
+ "strings"
+ "testing"
+)
+
+func TestGetCfgPath(t *testing.T) {
+ got := GetCfgPath()
+ if !strings.Contains(got, "conf") {
+ t.Errorf("GetCfgPath() => got %v, should contains `ocnf`", got)
+ }
+}
+
+func TestGetGoPath(t *testing.T) {
+ oldPaths := os.Getenv("GOPATH")
+ cases := []struct {
+ in string
+ want []string
+ }{
+ { // window
+ in: `path1;path2;path3`,
+ want: []string{
+ `path1`,
+ `path2`,
+ `path3`,
+ },
+ },
+ { // linux
+ in: `path1:path2:path3`,
+ want: []string{
+ `path1`,
+ `path2`,
+ `path3`,
+ },
+ },
+ { // single Path
+ in: `path1`,
+ want: []string{
+ `path1`,
+ },
+ },
+ { // single Path
+ in: `;`,
+ want: []string{
+ ``, ``,
+ },
+ },
+ }
+
+ for _, cas := range cases {
+ os.Setenv("GOPATH", cas.in)
+ got := GetGoPath()
+
+ if len(cas.want) != len(got) {
+ t.Errorf("GetGoPath() => different size, got %d, want %d, %v, %v", len(got), len(cas.want), got, cas.want)
+ }
+
+ for i, item := range cas.want {
+ if item != got[i] {
+ t.Errorf("GetGoPath() => got %v, want %v", got, cas.want)
+ break
+ }
+ }
+ }
+
+ // unset test
+ os.Unsetenv("GOPATH")
+ got := GetGoPath()
+ if len(got) != 0 {
+ t.Errorf("GetGoPath() => unset env test got len %d, want 0", len(got))
+ }
+
+ os.Setenv("GOPATH", oldPaths)
+}
+
+func TestFileExists(t *testing.T) {
+ existFile := `common_test.go`
+ notExistFile := `common_test.go_11`
+
+ exist := FileExists(existFile)
+ if !exist {
+ t.Errorf("FileExists(%s) => got false, want true", existFile)
+ }
+
+ exist = FileExists(notExistFile)
+ if exist {
+ t.Errorf("FileExists(%s) => got true, want false", notExistFile)
+ }
+}