OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / common / os_test.go
1 package common
2
3 import (
4         "bytes"
5         "fmt"
6         "io/ioutil"
7         "os"
8         "testing"
9         "time"
10 )
11
12 func TestWriteFileAtomic(t *testing.T) {
13         data := []byte("Becatron")
14         fname := fmt.Sprintf("/tmp/write-file-atomic-test-%v.txt", time.Now().UnixNano())
15         err := WriteFileAtomic(fname, data, 0664)
16         if err != nil {
17                 t.Fatal(err)
18         }
19         rData, err := ioutil.ReadFile(fname)
20         if err != nil {
21                 t.Fatal(err)
22         }
23         if !bytes.Equal(data, rData) {
24                 t.Fatalf("data mismatch: %v != %v", data, rData)
25         }
26         if err := os.Remove(fname); err != nil {
27                 t.Fatal(err)
28         }
29 }
30
31 func TestGoPath(t *testing.T) {
32         // restore original gopath upon exit
33         path := os.Getenv("GOPATH")
34         defer func() {
35                 _ = os.Setenv("GOPATH", path)
36         }()
37
38         err := os.Setenv("GOPATH", "~/testgopath")
39         if err != nil {
40                 t.Fatal(err)
41         }
42         path = GoPath()
43         if path != "~/testgopath" {
44                 t.Fatalf("should get GOPATH env var value, got %v", path)
45         }
46         os.Unsetenv("GOPATH")
47
48         path = GoPath()
49         if path != "~/testgopath" {
50                 t.Fatalf("subsequent calls should return the same value, got %v", path)
51         }
52 }
53
54 func TestGoPathWithoutEnvVar(t *testing.T) {
55         // restore original gopath upon exit
56         path := os.Getenv("GOPATH")
57         defer func() {
58                 _ = os.Setenv("GOPATH", path)
59         }()
60
61         os.Unsetenv("GOPATH")
62         // reset cache
63         gopath = ""
64
65         path = GoPath()
66         if path == "" || path == "~/testgopath" {
67                 t.Fatalf("should get nonempty result of calling go env GOPATH, got %v", path)
68         }
69 }