summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHuabing Zhao <zhaohuabing@gmail.com>2018-08-01 03:11:00 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-01 03:11:00 +0000
commit3f4940799e6dd72bf314c748201719b000a58d1b (patch)
tree5101c2c2871874887fbb09ad56bc21665ce5d499
parent33333e71c31c527166809371c38564ddc301a1ff (diff)
parent45198f48b37f69c1b4764f93f423c5ceeaece06a (diff)
Merge "add ut for app setting"
-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)
+ }
+}