github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/edit/completion/complete_arg_unix_test.go (about) 1 // +build !windows,!plan9 2 3 // TODO: Enable on Windows. 4 package completion 5 6 import ( 7 "os" 8 "reflect" 9 "sort" 10 "testing" 11 12 "github.com/u-root/u-root/cmds/elvish/edit/ui" 13 "github.com/u-root/u-root/cmds/elvish/util" 14 ) 15 16 var ( 17 fileStyle = ui.StylesFromString("1") 18 exeStyle = ui.StylesFromString("2") 19 dirStyle = ui.StylesFromString("4") 20 ) 21 22 var complFilenameInnerTests = []struct { 23 head string 24 executableOnly bool 25 wantCandidates rawCandidates 26 }{ 27 // Match all non-hidden files and dirs, in alphabetical order. 28 // Files have suffix " " and directories "/". Styles are set according to 29 // the LS_COLORS variable, which are set in the beginning of the test. 30 {"haha", false, rawCandidates{ 31 &complexCandidate{stem: "Documents", codeSuffix: "/", style: dirStyle}, 32 &complexCandidate{stem: "bar", codeSuffix: " ", style: fileStyle}, 33 &complexCandidate{stem: "elvish", codeSuffix: " ", style: exeStyle}, 34 &complexCandidate{stem: "foo", codeSuffix: " ", style: fileStyle}, 35 }}, 36 // Only match executables and directories. 37 {"haha", true, rawCandidates{ 38 &complexCandidate{stem: "Documents", codeSuffix: "/", style: dirStyle}, 39 &complexCandidate{stem: "elvish", codeSuffix: " ", style: exeStyle}, 40 }}, 41 // Match hidden files and directories. 42 {".haha", false, rawCandidates{ 43 &complexCandidate{stem: ".elvish", codeSuffix: "/", style: dirStyle}, 44 &complexCandidate{stem: ".vimrc", codeSuffix: " ", style: fileStyle}, 45 }}, 46 } 47 48 func testComplFilenameInner(t *testing.T) { 49 os.Setenv("LS_COLORS", "rs=1:ex=2:di=4") 50 util.InTempDir(func(string) { 51 create("foo", 0600) 52 create(".vimrc", 0600) 53 create("bar", 0600) 54 55 create("elvish", 0700) 56 57 mkdir("Documents", 0700) 58 mkdir(".elvish", 0700) 59 60 for _, test := range complFilenameInnerTests { 61 var ( 62 err error 63 cands rawCandidates 64 gets = make(chan rawCandidate) 65 ) 66 go func() { 67 defer close(gets) 68 err = complFilenameInner(test.head, test.executableOnly, gets) 69 }() 70 for v := range gets { 71 cands = append(cands, v) 72 } 73 if err != nil { 74 t.Errorf("complFilenameInner(%v, %v) returns error %v, want nil", 75 test.head, test.executableOnly, err) 76 } 77 sort.Sort(cands) 78 if !reflect.DeepEqual(cands, test.wantCandidates) { 79 t.Errorf("complFilenameInner(%v, %v) returns %v, want %v", 80 test.head, test.executableOnly, cands, test.wantCandidates) 81 t.Log("returned candidates are:") 82 for _, cand := range cands { 83 t.Logf("%#v", cand) 84 } 85 } 86 } 87 }) 88 } 89 90 func mkdir(dirname string, perm os.FileMode) { 91 err := os.Mkdir(dirname, perm) 92 if err != nil { 93 panic(err) 94 } 95 } 96 97 func create(fname string, perm os.FileMode) { 98 f, err := os.OpenFile(fname, os.O_CREATE, perm) 99 if err != nil { 100 panic(err) 101 } 102 f.Close() 103 }