github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/cmd_test.go (about) 1 // +build unit 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/jenkins-x/jx-logging/pkg/log" 12 "github.com/jenkins-x/jx/v2/pkg/cmd/clients/fake" 13 "github.com/spf13/cobra" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestLoggingSetup(t *testing.T) { 18 origLogLevel, exists := os.LookupEnv("JX_LOG_LEVEL") 19 if exists { 20 defer func() { 21 _ = os.Setenv("JX_LOG_LEVEL", origLogLevel) 22 }() 23 } 24 25 var logTests = []struct { 26 envLogLevel string 27 verbose bool 28 expectedOutput string 29 }{ 30 {"trace", false, "TRACE: trace\nDEBUG: debug\ninfo\nWARNING: warn\nERROR: error\n"}, 31 {"trace", true, "TRACE: trace\nDEBUG: debug\ninfo\nWARNING: warn\nERROR: error\n"}, 32 {"debug", false, "DEBUG: debug\ninfo\nWARNING: warn\nERROR: error\n"}, 33 {"debug", true, "DEBUG: debug\ninfo\nWARNING: warn\nERROR: error\n"}, 34 {"info", false, "info\nWARNING: warn\nERROR: error\n"}, 35 {"info", true, "info\nWARNING: warn\nERROR: error\n"}, 36 {"warn", false, "WARNING: warn\nERROR: error\n"}, 37 {"warn", true, "WARNING: warn\nERROR: error\n"}, 38 {"error", false, "ERROR: error\n"}, 39 {"error", true, "ERROR: error\n"}, 40 {"", true, "DEBUG: debug\ninfo\nWARNING: warn\nERROR: error\n"}, 41 {"", false, "info\nWARNING: warn\nERROR: error\n"}, 42 {"foo", false, "info\nWARNING: warn\nERROR: error\n"}, 43 {"foo", true, "info\nWARNING: warn\nERROR: error\n"}, 44 } 45 46 testCommandName := "logtest" 47 for _, logTest := range logTests { 48 t.Run(fmt.Sprintf("JX_LOG_LEVEL=%s verbose=%t", logTest.envLogLevel, logTest.verbose), func(t *testing.T) { 49 if logTest.envLogLevel == "" { 50 err := os.Unsetenv("JX_LOG_LEVEL") 51 assert.NoError(t, err) 52 } else { 53 err := os.Setenv("JX_LOG_LEVEL", logTest.envLogLevel) 54 assert.NoError(t, err) 55 } 56 57 logCommand := &cobra.Command{ 58 Use: testCommandName, 59 Short: "dummy test command", 60 Run: func(cmd *cobra.Command, args []string) { 61 out := log.CaptureOutput(func() { 62 log.Logger().Trace("trace") 63 log.Logger().Debug("debug") 64 log.Logger().Info("info") 65 log.Logger().Warn("warn") 66 log.Logger().Error("error") 67 }) 68 69 assert.Equal(t, logTest.expectedOutput, out) 70 }, 71 } 72 73 rootCmd := NewJXCommand(fake.NewFakeFactory(), os.Stdin, os.Stdout, os.Stderr, nil) 74 rootCmd.AddCommand(logCommand) 75 76 args := []string{testCommandName} 77 if logTest.verbose { 78 args = append(args, "--verbose") 79 } 80 rootCmd.SetArgs(args) 81 _ = log.CaptureOutput(func() { 82 err := rootCmd.Execute() 83 assert.NoError(t, err) 84 }) 85 }) 86 } 87 } 88 89 func TestFindPluginBinary(t *testing.T) { 90 pluginsDir := filepath.Join("test_data", "binary_plugins_dir") 91 92 path := FindPluginBinary(pluginsDir, "jx-dummy-plugin") 93 assert.Equal(t, filepath.Join(pluginsDir, "jx-dummy-plugin-1.2.3"), path, "should have found binary plugin") 94 95 path = FindPluginBinary(pluginsDir, "jx-does-not-exist") 96 assert.Equal(t, "", path, "should have not have found binary plugin") 97 }