add ut for app setting 97/58297/1
authorLvbo163 <lv.bo163@zte.com.cn>
Wed, 1 Aug 2018 01:23:00 +0000 (09:23 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Wed, 1 Aug 2018 01:23:00 +0000 (09:23 +0800)
uts for getConfigPath

Issue-ID: MSB-240

Change-Id: I440a29be3fc0d922b5c2a98bafb7787897867c7a
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/util/common.go
msb2pilot/src/msb2pilot/util/common_test.go [new file with mode: 0644]

index 6053116..1bb10df 100644 (file)
@@ -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 (file)
index 0000000..6204b6f
--- /dev/null
@@ -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)
+       }
+}