@@ -42,6 +42,26 @@ func fatal(err error) {
4242 }
4343}
4444
45+ func equal (s1 , s2 []string ) bool {
46+ if len (s1 ) != len (s2 ) {
47+ return false
48+ }
49+ for i , s1e := range s1 {
50+ if s1e != s2 [i ] {
51+ return false
52+ }
53+ }
54+ return true
55+ }
56+
57+ func createRunitDir () string {
58+ dir , err := ioutil .TempDir ("" , "svctl_tests" )
59+ fatal (err )
60+ cmd := exec .Command ("cp" , "-r" , "_testdata/" , dir )
61+ fatal (cmd .Run ())
62+ return dir
63+ }
64+
4565type runitRunner struct {
4666 basedir string
4767 runsvdir * exec.Cmd
@@ -52,10 +72,7 @@ type runitRunner struct {
5272}
5373
5474func newRunitRunner () * runitRunner {
55- dir , err := ioutil .TempDir ("" , "svctl_tests" )
56- fatal (err )
57- cmd := exec .Command ("cp" , "-r" , "_testdata/" , dir )
58- fatal (cmd .Run ())
75+ dir := createRunitDir ()
5976
6077 r := & runitRunner {
6178 basedir : path .Join (dir , "_testdata" ),
@@ -236,3 +253,60 @@ func TestCmd(t *testing.T) {
236253 svctl .line .Close ()
237254 runit .Close ()
238255}
256+
257+ func TestCompleter (t * testing.T ) {
258+ allCmds := []string {
259+ "up " , "start " , "down " , "stop " , "r " , "restart " , "once " ,
260+ "pause " , "cont " , "hup " , "reload " , "alarm " , "interrupt " ,
261+ "quit " , "1 " , "2 " , "term " , "kill " , "status " , "help " , "exit " ,
262+ }
263+ defs := []struct {
264+ line string
265+ pos int
266+
267+ head string
268+ completions []string
269+ tail string
270+ }{
271+ {"" , 0 , "" , allCmds , "" },
272+ {"u" , 1 , "" , []string {"up " }, "" },
273+ {"u" , 0 , "" , []string {"up " }, "" },
274+ {"sto" , 2 , "" , []string {"stop " }, "" },
275+ {"stop " , 5 , "stop " , []string {"longone " , "o " , "r0 " , "r1 " , "w " }, "" },
276+ {"up r" , 4 , "up " , []string {"r0 " , "r1 " }, "" },
277+ {"up o r" , 6 , "up o " , []string {"r0 " , "r1 " }, "" },
278+ {"up lo r" , 4 , "up " , []string {"longone " }, " r" },
279+ {"? " , 2 , "? " , allCmds , "" },
280+ {"help " , 5 , "help " , allCmds , "" },
281+ {"? st" , 4 , "? " , []string {"start " , "stop " , "status " }, "" },
282+ {"? h term" , 3 , "? " , []string {"hup " , "help " }, " term" },
283+ {"? st term" , 3 , "? " , []string {"start " , "stop " , "status " }, " term" },
284+ }
285+
286+ dir := createRunitDir ()
287+ svctl := ctl {basedir : path .Join (dir , "_testdata" )}
288+
289+ for _ , def := range defs {
290+ head , completions , tail := svctl .completer (def .line , def .pos )
291+ if head != def .head {
292+ t .Errorf (
293+ "ERROR IN HEAD: `%s` != `%s` for `%s:%d`" ,
294+ head , def .head , def .line , def .pos ,
295+ )
296+ }
297+ if ! equal (completions , def .completions ) {
298+ t .Errorf (
299+ "ERROR IN COMPLETIONS: `%v` != `%v` for `%s:%d`" ,
300+ completions , def .completions , def .line , def .pos ,
301+ )
302+ }
303+ if tail != def .tail {
304+ t .Errorf (
305+ "ERROR IN TAIL: `%s` != `%s` for `%s:%d`" ,
306+ tail , def .tail , def .line , def .pos ,
307+ )
308+ }
309+ }
310+
311+ os .RemoveAll (dir )
312+ }
0 commit comments