vitess.io/vitess@v0.16.2/go/cmd/vtctldclient/command/root_test.go (about)

     1  /*
     2  Copyright 2023 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package command_test
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/cmd/vtctldclient/command"
    27  	"vitess.io/vitess/go/vt/vtctl/localvtctldclient"
    28  
    29  	vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice"
    30  )
    31  
    32  type emptyLocalServer struct {
    33  	vtctlservicepb.UnimplementedVtctldServer
    34  }
    35  
    36  func TestRoot(t *testing.T) {
    37  	t.Run("error on unknown subcommand", func(t *testing.T) {
    38  		args := append([]string{}, os.Args...)
    39  		protocol := command.VtctldClientProtocol
    40  		localvtctldclient.SetServer(&emptyLocalServer{})
    41  
    42  		t.Cleanup(func() {
    43  			os.Args = append([]string{}, args...)
    44  			command.VtctldClientProtocol = protocol
    45  		})
    46  
    47  		os.Args = []string{"vtctldclient", "this-is-bunk"}
    48  		command.VtctldClientProtocol = "local"
    49  
    50  		err := command.Root.Execute()
    51  		require.Error(t, err, "root command should error on unknown command")
    52  		assert.Contains(t, err.Error(), "unknown command")
    53  	})
    54  }