github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/cli_test.go (about) 1 // Copyright 2020 Google LLC 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 // https://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 cli 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/actions-on-google/gactions/api/sdk" 22 "github.com/actions-on-google/gactions/log/log" 23 "github.com/spf13/cobra" 24 ) 25 26 func TestCommandDebugSet(t *testing.T) { 27 old := log.Severity 28 defer func() { 29 log.Severity = old 30 }() 31 cmd := Command(context.Background(), "gactions", true, "") 32 // CLI sets logging at runtime, so need to simulate execution 33 cmd.RunE = func(*cobra.Command, []string) error { 34 return nil 35 } 36 _ = Execute(cmd) 37 if log.Severity != log.DebugLevel { 38 t.Errorf("Command set severity to %v, but want %v", log.Severity, log.DebugLevel) 39 } 40 } 41 42 func TestCommandDebugNotSet(t *testing.T) { 43 old := log.Severity 44 defer func() { 45 log.Severity = old 46 }() 47 cmd := Command(context.Background(), "gactions", false, "") 48 // CLI sets logging at runtime, so need to simulate execution 49 cmd.RunE = func(*cobra.Command, []string) error { 50 return nil 51 } 52 _ = Execute(cmd) 53 if log.Severity != log.WarnLevel { 54 t.Errorf("Command set severity to %v, but want %v", log.Severity, log.WarnLevel) 55 } 56 // check debug flags 57 debugFlags := []string{"--env=foo", "--cookie=abc"} 58 for _, v := range debugFlags { 59 cmd.SetArgs([]string{v}) 60 code := Execute(cmd) 61 if code != 1 { 62 t.Errorf("Executed returned %v, but want %v when %v flag is set.", code, 1, v) 63 } 64 } 65 } 66 67 func TestCommandEnvFlagDebugSet(t *testing.T) { 68 old := sdk.CurEnv 69 defer func() { 70 sdk.CurEnv = old 71 }() 72 cmd := Command(context.Background(), "gactions", true, "") 73 // CLI sets logging at runtime, so need to simulate execution 74 cmd.RunE = func(*cobra.Command, []string) error { 75 return nil 76 } 77 // case 1 78 cmd.SetArgs([]string{"--env=prod"}) 79 code := Execute(cmd) 80 if code != 0 { 81 t.Errorf("Execute returned %v, but want %v", code, 0) 82 } 83 if sdk.CurEnv != "prod" { 84 t.Errorf("Expected to set CurEnv to %v, but got %v", "prod", sdk.CurEnv) 85 } 86 // case 2 87 cmd.SetArgs([]string{"--env=foo"}) 88 code = Execute(cmd) 89 if code != 1 { 90 t.Errorf("Executed returned %v, but want %v", code, 1) 91 } 92 if sdk.CurEnv != "prod" { 93 t.Errorf("Expected CurEnv to remain %v, but got %v", "prod", sdk.CurEnv) 94 } 95 }