github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/go/testdata/src/testcache/testcache_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package testcache 6 7 import ( 8 "io/ioutil" 9 "os" 10 "runtime" 11 "testing" 12 ) 13 14 func TestChdir(t *testing.T) { 15 os.Chdir("..") 16 defer os.Chdir("testcache") 17 info, err := os.Stat("testcache/file.txt") 18 if err != nil { 19 t.Fatal(err) 20 } 21 if info.Size()%2 != 1 { 22 t.Fatal("even file") 23 } 24 } 25 26 func TestOddFileContent(t *testing.T) { 27 f, err := os.Open("file.txt") 28 if err != nil { 29 t.Fatal(err) 30 } 31 data, err := ioutil.ReadAll(f) 32 f.Close() 33 if err != nil { 34 t.Fatal(err) 35 } 36 if len(data)%2 != 1 { 37 t.Fatal("even file") 38 } 39 } 40 41 func TestOddFileSize(t *testing.T) { 42 info, err := os.Stat("file.txt") 43 if err != nil { 44 t.Fatal(err) 45 } 46 if info.Size()%2 != 1 { 47 t.Fatal("even file") 48 } 49 } 50 51 func TestOddGetenv(t *testing.T) { 52 val := os.Getenv("TESTKEY") 53 if len(val)%2 != 1 { 54 t.Fatal("even env value") 55 } 56 } 57 58 func TestLookupEnv(t *testing.T) { 59 _, ok := os.LookupEnv("TESTKEY") 60 if !ok { 61 t.Fatal("env missing") 62 } 63 } 64 65 func TestDirList(t *testing.T) { 66 f, err := os.Open(".") 67 if err != nil { 68 t.Fatal(err) 69 } 70 f.Readdirnames(-1) 71 f.Close() 72 } 73 74 func TestExec(t *testing.T) { 75 if runtime.GOOS == "plan9" || runtime.GOOS == "windows" || runtime.GOOS == "nacl" { 76 t.Skip("non-unix") 77 } 78 79 // Note: not using os/exec to make sure there is no unexpected stat. 80 p, err := os.StartProcess("./script.sh", []string{"script"}, new(os.ProcAttr)) 81 if err != nil { 82 t.Fatal(err) 83 } 84 ps, err := p.Wait() 85 if err != nil { 86 t.Fatal(err) 87 } 88 if !ps.Success() { 89 t.Fatalf("script failed: %v", err) 90 } 91 }