github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/eval/testmain_test.go (about) 1 package eval 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "sort" 8 "strings" 9 "testing" 10 11 "github.com/u-root/u-root/cmds/core/elvish/util" 12 ) 13 14 var ( 15 filesToCreate = []string{ 16 "a1", "a2", "a3", "a10", 17 "b1", "b2", "b3", 18 "c1", "c2", 19 "foo", "bar", "lorem", "ipsum", 20 } 21 dirsToCreate = []string{"dir", "dir2"} 22 fileListing = getFileListing() 23 ) 24 25 func getFileListing() []string { 26 var x []string 27 x = append(x, filesToCreate...) 28 x = append(x, dirsToCreate...) 29 sort.Strings(x) 30 return x 31 } 32 33 func getFilesWithPrefix(prefixes ...string) []string { 34 var x []string 35 for _, name := range fileListing { 36 for _, prefix := range prefixes { 37 if strings.HasPrefix(name, prefix) { 38 x = append(x, name) 39 break 40 } 41 } 42 } 43 sort.Strings(x) 44 return x 45 } 46 47 func getFilesBut(excludes ...string) []string { 48 var x []string 49 for _, name := range fileListing { 50 excluded := false 51 for _, exclude := range excludes { 52 if name == exclude { 53 excluded = true 54 break 55 } 56 } 57 if !excluded { 58 x = append(x, name) 59 } 60 } 61 sort.Strings(x) 62 return x 63 } 64 65 var mods = map[string]string{ 66 "lorem": "name = lorem; fn put-name { put $name }", 67 "d": "name = d", 68 "a/b/c/d": "name = a/b/c/d", 69 "a/b/c/x": "use ./d; d = $d:name; use ../../../lorem; lorem = $lorem:name", 70 "has/init": "put has/init", 71 "put-x": "put $x", 72 } 73 74 var libDir string 75 76 func testMain(m *testing.M) { 77 var exitCode int 78 util.InTempDir(func(tmpHome string) { 79 oldHome := os.Getenv("HOME") 80 os.Setenv("HOME", tmpHome) 81 defer os.Setenv("HOME", oldHome) 82 83 for _, filename := range filesToCreate { 84 file, err := os.Create(filename) 85 if err != nil { 86 panic(err) 87 } 88 file.Close() 89 } 90 91 for _, dirname := range dirsToCreate { 92 err := os.Mkdir(dirname, 0700) 93 if err != nil { 94 panic(err) 95 } 96 } 97 98 util.WithTempDir(func(dir string) { 99 libDir = dir 100 101 for mod, content := range mods { 102 fname := filepath.Join(libDir, mod+".elv") 103 os.MkdirAll(filepath.Dir(fname), 0700) 104 err := ioutil.WriteFile(fname, []byte(content), 0600) 105 if err != nil { 106 panic(err) 107 } 108 } 109 110 exitCode = m.Run() 111 }) 112 }) 113 os.Exit(exitCode) 114 } 115 116 func runTests(t *testing.T, tests []Test) { 117 if false { 118 if err := RunTests(tests, func() *Evaler { 119 ev := NewEvaler() 120 ev.SetLibDir(libDir) 121 return ev 122 }); err != nil { 123 t.Error(err) 124 } 125 } 126 }