github.com/getgauge/gauge@v1.6.9/logger/logger_test.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package logger
     8  
     9  import (
    10  	"fmt"
    11  	"path/filepath"
    12  	"runtime"
    13  	"testing"
    14  
    15  	"os"
    16  
    17  	"github.com/getgauge/gauge/config"
    18  	"github.com/getgauge/gauge/plugin/pluginInfo"
    19  	"github.com/getgauge/gauge/version"
    20  	logging "github.com/op/go-logging"
    21  )
    22  
    23  func TestGetLoggerShouldGetTheLoggerForGivenModule(t *testing.T) {
    24  	Initialize(false, "info", CLI)
    25  
    26  	l := loggersMap.getLogger("gauge-js")
    27  	if l == nil {
    28  		t.Error("Expected a logger to be initilized for gauge-js")
    29  	}
    30  }
    31  
    32  func TestLoggerInitWithInfoLevel(t *testing.T) {
    33  	Initialize(false, "info", CLI)
    34  
    35  	if !loggersMap.getLogger(gaugeModuleID).IsEnabledFor(logging.INFO) {
    36  		t.Error("Expected gaugeLog to be enabled for INFO")
    37  	}
    38  }
    39  
    40  func TestLoggerInitWithDefaultLevel(t *testing.T) {
    41  	Initialize(false, "", CLI)
    42  
    43  	if !loggersMap.getLogger(gaugeModuleID).IsEnabledFor(logging.INFO) {
    44  		t.Error("Expected gaugeLog to be enabled for default log level")
    45  	}
    46  }
    47  
    48  func TestLoggerInitWithDebugLevel(t *testing.T) {
    49  	Initialize(false, "debug", CLI)
    50  
    51  	if !loggersMap.getLogger(gaugeModuleID).IsEnabledFor(logging.DEBUG) {
    52  		t.Error("Expected gaugeLog to be enabled for DEBUG")
    53  	}
    54  }
    55  
    56  func TestLoggerInitWithWarningLevel(t *testing.T) {
    57  	Initialize(false, "warning", CLI)
    58  
    59  	if !loggersMap.getLogger(gaugeModuleID).IsEnabledFor(logging.WARNING) {
    60  		t.Error("Expected gaugeLog to be enabled for WARNING")
    61  	}
    62  }
    63  
    64  func TestLoggerInitWithErrorLevel(t *testing.T) {
    65  	Initialize(false, "error", CLI)
    66  
    67  	if !loggersMap.getLogger(gaugeModuleID).IsEnabledFor(logging.ERROR) {
    68  		t.Error("Expected gaugeLog to be enabled for ERROR")
    69  	}
    70  }
    71  
    72  func TestGetLogFileGivenRelativePathInGaugeProject(t *testing.T) {
    73  	config.ProjectRoot, _ = filepath.Abs("_testdata")
    74  	want := filepath.Join(config.ProjectRoot, logs, apiLogFileName)
    75  
    76  	got := getLogFile(apiLogFileName)
    77  	if got != want {
    78  		t.Errorf("Got %s, want %s", got, want)
    79  	}
    80  }
    81  
    82  func TestGetLogFileWhenLogsDirNotSet(t *testing.T) {
    83  	config.ProjectRoot, _ = filepath.Abs("_testdata")
    84  	want := filepath.Join(config.ProjectRoot, logs, apiLogFileName)
    85  
    86  	got := getLogFile(apiLogFileName)
    87  	if got != want {
    88  		t.Errorf("Got %s, want %s", got, want)
    89  	}
    90  }
    91  
    92  func TestGetLogFileInGaugeProjectWhenRelativeCustomLogsDirIsSet(t *testing.T) {
    93  	myLogsDir := "my_logs"
    94  	os.Setenv(logsDirectory, myLogsDir)
    95  	defer os.Unsetenv(logsDirectory)
    96  
    97  	config.ProjectRoot, _ = filepath.Abs("_testdata")
    98  	want := filepath.Join(config.ProjectRoot, myLogsDir, apiLogFileName)
    99  
   100  	got := getLogFile(apiLogFileName)
   101  
   102  	if got != want {
   103  		t.Errorf("Got %s, want %s", got, want)
   104  	}
   105  }
   106  
   107  func TestGetLogFileInGaugeProjectWhenAbsoluteCustomLogsDirIsSet(t *testing.T) {
   108  	myLogsDir, err := filepath.Abs("my_logs")
   109  	if err != nil {
   110  		t.Errorf("Unable to convert to absolute path, %s", err.Error())
   111  	}
   112  
   113  	os.Setenv(logsDirectory, myLogsDir)
   114  	defer os.Unsetenv(logsDirectory)
   115  
   116  	want := filepath.Join(myLogsDir, apiLogFileName)
   117  
   118  	got := getLogFile(apiLogFileName)
   119  
   120  	if got != want {
   121  		t.Errorf("Got %s, want %s", got, want)
   122  	}
   123  }
   124  
   125  func TestGetErrorText(t *testing.T) {
   126  	tests := []struct {
   127  		gaugeVersion *version.Version
   128  		commitHash   string
   129  		pluginInfos  []pluginInfo.PluginInfo
   130  		expectedText string
   131  	}{
   132  		{
   133  			gaugeVersion: &version.Version{Major: 0, Minor: 9, Patch: 8},
   134  			commitHash:   "",
   135  			pluginInfos:  []pluginInfo.PluginInfo{{Name: "java", Version: &version.Version{Major: 0, Minor: 6, Patch: 6}}},
   136  			expectedText: fmt.Sprintf(`Error ----------------------------------
   137  
   138  An Error has Occurred: some error
   139  
   140  Get Support ----------------------------
   141  	Docs:          https://docs.gauge.org
   142  	Bugs:          https://github.com/getgauge/gauge/issues
   143  	Chat:          https://github.com/getgauge/gauge/discussions
   144  
   145  Your Environment Information -----------
   146  	%s, 0.9.8
   147  	java (0.6.6)`, runtime.GOOS),
   148  		},
   149  		{
   150  			gaugeVersion: &version.Version{Major: 0, Minor: 9, Patch: 8},
   151  			commitHash:   "",
   152  			pluginInfos: []pluginInfo.PluginInfo{
   153  				{Name: "java", Version: &version.Version{Major: 0, Minor: 6, Patch: 6}},
   154  				{Name: "html-report", Version: &version.Version{Major: 0, Minor: 4, Patch: 0}},
   155  			},
   156  			expectedText: fmt.Sprintf(`Error ----------------------------------
   157  
   158  An Error has Occurred: some error
   159  
   160  Get Support ----------------------------
   161  	Docs:          https://docs.gauge.org
   162  	Bugs:          https://github.com/getgauge/gauge/issues
   163  	Chat:          https://github.com/getgauge/gauge/discussions
   164  
   165  Your Environment Information -----------
   166  	%s, 0.9.8
   167  	java (0.6.6), html-report (0.4.0)`, runtime.GOOS),
   168  		},
   169  		{
   170  			gaugeVersion: &version.Version{Major: 0, Minor: 9, Patch: 8},
   171  			commitHash:   "59effa",
   172  			pluginInfos: []pluginInfo.PluginInfo{
   173  				{Name: "java", Version: &version.Version{Major: 0, Minor: 6, Patch: 6}},
   174  				{Name: "html-report", Version: &version.Version{Major: 0, Minor: 4, Patch: 0}},
   175  			},
   176  			expectedText: fmt.Sprintf(`Error ----------------------------------
   177  
   178  An Error has Occurred: some error
   179  
   180  Get Support ----------------------------
   181  	Docs:          https://docs.gauge.org
   182  	Bugs:          https://github.com/getgauge/gauge/issues
   183  	Chat:          https://github.com/getgauge/gauge/discussions
   184  
   185  Your Environment Information -----------
   186  	%s, 0.9.8, 59effa
   187  	java (0.6.6), html-report (0.4.0)`, runtime.GOOS),
   188  		},
   189  	}
   190  
   191  	for _, test := range tests {
   192  		version.CurrentGaugeVersion = test.gaugeVersion
   193  		version.CommitHash = test.commitHash
   194  		pluginInfo.GetAllInstalledPluginsWithVersion = func() ([]pluginInfo.PluginInfo, error) {
   195  			return test.pluginInfos, nil
   196  		}
   197  		fatalErrors = append(fatalErrors, fmt.Sprintf("An Error has Occurred: %s", "some error"))
   198  		got := getFatalErrorMsg()
   199  		want := test.expectedText
   200  
   201  		if got != want {
   202  			t.Errorf("Got %s, want %s", got, want)
   203  		}
   204  		fatalErrors = []string{}
   205  	}
   206  }
   207  
   208  func TestToJSONWithPlainText(t *testing.T) {
   209  	outMessage := &OutMessage{MessageType: "out", Message: "plain text"}
   210  	want := "{\"type\":\"out\",\"message\":\"plain text\"}"
   211  
   212  	got, _ := outMessage.ToJSON()
   213  	if got != want {
   214  		t.Errorf("Got %s, want %s", got, want)
   215  	}
   216  }
   217  
   218  func TestToJSONWithInvalidJSONCharacters(t *testing.T) {
   219  	outMessage := &OutMessage{MessageType: "out", Message: "\n, \t, and \\ needs to be escaped to create a valid JSON"}
   220  	want := "{\"type\":\"out\",\"message\":\"\\n, \\t, and \\\\ needs to be escaped to create a valid JSON\"}"
   221  
   222  	got, _ := outMessage.ToJSON()
   223  	if got != want {
   224  		t.Errorf("Got %s, want %s", got, want)
   225  	}
   226  }