github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/cmd/gremlins_test.go (about) 1 /* 2 * Copyright 2022 The Gremlins 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 cmd 18 19 import ( 20 "context" 21 "testing" 22 ) 23 24 func TestGremlins(t *testing.T) { 25 const boolType = "bool" 26 27 c, err := newRootCmd(context.Background(), "1.2.3") 28 if err != nil { 29 t.Fatal("newRootCmd should not fail") 30 } 31 _ = c.execute() 32 cmd := c.cmd 33 34 if cmd.Version != "1.2.3" { 35 t.Errorf("expected %q, got %q", "1.2.3", cmd.Version) 36 } 37 38 cfgFile := cmd.Flag("config") 39 if cfgFile == nil { 40 t.Fatal("expected to have a config flag") 41 } 42 if cfgFile.Value.Type() != "string" { 43 t.Errorf("expected value type to be 'string', got %v", cfgFile.Value.Type()) 44 } 45 if cfgFile.DefValue != "" { 46 t.Errorf("expected default value to be empty, got %v", cfgFile.DefValue) 47 } 48 49 silentFlag := cmd.Flag("silent") 50 if silentFlag == nil { 51 t.Fatal("expected to have a config flag") 52 } 53 if silentFlag.Value.Type() != boolType { 54 t.Errorf("expected value type to be 'bool', got %v", silentFlag.Value.Type()) 55 } 56 if silentFlag.DefValue != "false" { 57 t.Errorf("expected default value to be false, got %v", silentFlag.DefValue) 58 } 59 } 60 61 func TestExecute(t *testing.T) { 62 t.Run("should not fail", func(t *testing.T) { 63 err := Execute(context.Background(), "1.2.3") 64 if err != nil { 65 t.Errorf("execute should not fail") 66 } 67 }) 68 69 t.Run("should fail if version is not set", func(t *testing.T) { 70 err := Execute(context.Background(), "") 71 if err == nil { 72 t.Errorf("expected failure") 73 } 74 75 }) 76 }