github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/completion_test.go (about)

     1  // Copyright 2017 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"io/ioutil"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/rkt/rkt/pkg/log"
    24  )
    25  
    26  func TestBashCompletion(t *testing.T) {
    27  	tests := []struct {
    28  		ExitCode int
    29  		Args     []string
    30  	}{
    31  		// Command expects at least one parameter.
    32  		{254, []string{}},
    33  		// Only single argument is expected.
    34  		{254, []string{"two", "args"}},
    35  		// Fish shell is not supported.
    36  		{254, []string{"fish"}},
    37  		// Bash completion should succeed.
    38  		{0, []string{"bash"}},
    39  	}
    40  
    41  	var buf bytes.Buffer
    42  	handler := newCompletion(&buf)
    43  	stderr = log.New(ioutil.Discard, "", false)
    44  
    45  	for ii, tt := range tests {
    46  		buf.Reset()
    47  		code := handler(cmdCompletion, tt.Args)
    48  		if code != tt.ExitCode {
    49  			t.Errorf("#%d: got %v exit code, want %v", ii, code, tt.ExitCode)
    50  		}
    51  	}
    52  
    53  	// Check that generated output contains custom bash functions.
    54  	buf.Reset()
    55  	handler(cmdCompletion, []string{"bash"})
    56  
    57  	output := buf.String()
    58  	if !strings.Contains(output, bashCompletionFunc) {
    59  		t.Errorf("it is expected custom bash functions in the output")
    60  	}
    61  }