...

Source file src/github.com/shirou/gopsutil/internal/common/common_test.go

Documentation: github.com/shirou/gopsutil/internal/common

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestReadlines(t *testing.T) {
    13  	ret, err := ReadLines("common_test.go")
    14  	if err != nil {
    15  		t.Error(err)
    16  	}
    17  	if !strings.Contains(ret[0], "package common") {
    18  		t.Error("could not read correctly")
    19  	}
    20  }
    21  
    22  func TestReadLinesOffsetN(t *testing.T) {
    23  	ret, err := ReadLinesOffsetN("common_test.go", 2, 1)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  	fmt.Println(ret[0])
    28  	if !strings.Contains(ret[0], `import (`) {
    29  		t.Error("could not read correctly")
    30  	}
    31  }
    32  
    33  func TestIntToString(t *testing.T) {
    34  	src := []int8{65, 66, 67}
    35  	if dst := IntToString(src); dst != "ABC" {
    36  		t.Error("could not convert")
    37  	}
    38  }
    39  func TestByteToString(t *testing.T) {
    40  	src := []byte{65, 66, 67}
    41  	dst := ByteToString(src)
    42  	if dst != "ABC" {
    43  		t.Error("could not convert")
    44  	}
    45  
    46  	src = []byte{0, 65, 66, 67}
    47  	dst = ByteToString(src)
    48  	if dst != "ABC" {
    49  		t.Error("could not convert")
    50  	}
    51  }
    52  
    53  func TestHexToUint32(t *testing.T) {
    54  	if HexToUint32("FFFFFFFF") != 4294967295 {
    55  		t.Error("Could not convert")
    56  	}
    57  }
    58  
    59  func TestMustParseInt32(t *testing.T) {
    60  	if ret := mustParseInt32("11111"); ret != int32(11111) {
    61  		t.Error("could not parse")
    62  	}
    63  }
    64  func TestMustParseUint64(t *testing.T) {
    65  	ret := mustParseUint64("11111")
    66  	if ret != uint64(11111) {
    67  		t.Error("could not parse")
    68  	}
    69  }
    70  func TestMustParseFloat64(t *testing.T) {
    71  	ret := mustParseFloat64("11111.11")
    72  	if ret != float64(11111.11) {
    73  		t.Error("could not parse")
    74  	}
    75  	ret = mustParseFloat64("11111")
    76  	if ret != float64(11111) {
    77  		t.Error("could not parse")
    78  	}
    79  }
    80  func TestStringsContains(t *testing.T) {
    81  	target, err := ReadLines("common_test.go")
    82  	if err != nil {
    83  		t.Error(err)
    84  	}
    85  	if !StringsContains(target, "func TestStringsContains(t *testing.T) {") {
    86  		t.Error("cloud not test correctly")
    87  	}
    88  }
    89  
    90  func TestPathExists(t *testing.T) {
    91  	if !PathExists("common_test.go") {
    92  		t.Error("exists but return not exists")
    93  	}
    94  	if PathExists("should_not_exists.go") {
    95  		t.Error("not exists but return exists")
    96  	}
    97  }
    98  
    99  func TestHostEtc(t *testing.T) {
   100  	if runtime.GOOS == "windows" {
   101  		t.Skip("windows doesn't have etc")
   102  	}
   103  	if p := HostEtc("mtab"); p != "/etc/mtab" {
   104  		t.Errorf("invalid HostEtc, %s", p)
   105  	}
   106  }
   107  
   108  func TestGetSysctrlEnv(t *testing.T) {
   109  	// Append case
   110  	env := getSysctrlEnv([]string{"FOO=bar"})
   111  	if !reflect.DeepEqual(env, []string{"FOO=bar", "LC_ALL=C"}) {
   112  		t.Errorf("unexpected append result from getSysctrlEnv: %q", env)
   113  	}
   114  
   115  	// Replace case
   116  	env = getSysctrlEnv([]string{"FOO=bar", "LC_ALL=en_US.UTF-8"})
   117  	if !reflect.DeepEqual(env, []string{"FOO=bar", "LC_ALL=C"}) {
   118  		t.Errorf("unexpected replace result from getSysctrlEnv: %q", env)
   119  	}
   120  
   121  	// Test against real env
   122  	env = getSysctrlEnv(os.Environ())
   123  	found := false
   124  	for _, v := range env {
   125  		if v == "LC_ALL=C" {
   126  			found = true
   127  			continue
   128  		}
   129  		if strings.HasPrefix(v, "LC_ALL") {
   130  			t.Fatalf("unexpected LC_ALL value: %q", v)
   131  		}
   132  	}
   133  	if !found {
   134  		t.Errorf("unexpected real result from getSysctrlEnv: %q", env)
   135  	}
   136  }
   137  

View as plain text