Skip to content

Commit

Permalink
Finish TestColorize and cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrews committed Apr 26, 2023
1 parent 5c69fe9 commit 11673b9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
60 changes: 36 additions & 24 deletions colors_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package main

import (
"bytes"
"fmt"
"github.com/fatih/color"
"github.com/fstab/grok_exporter/tailer/fswatcher"
"testing"
)

func TestParseRegexPatterns(t *testing.T) {
// load in the default config file so we get some regex patterns
// Load in the default config file so we get some regex patterns
HandleConfigFile("logstation.default.conf")

// ParseRegexPatterns pulls from the viper config as an input
var compiledRegexColors = ParseRegexPatterns()
compiledRegexColors := ParseRegexPatterns()

// Check the number of CompiledRegexColors
var parsedColors = len(compiledRegexColors)
var expectedColors = 5
parsedColors := len(compiledRegexColors)
expectedColors := 5
if parsedColors != expectedColors {
t.Errorf("Should have had %d CompiledRegexColors. Ended up with %d", expectedColors, parsedColors)
}

// Verify the first color is hired
var parsedColor = compiledRegexColors[0].color
var expectedColor = "hired"
parsedColor := compiledRegexColors[0].color
expectedColor := "hired"
if parsedColor != expectedColor {
t.Errorf("First regex should have been %s. Ended up with %s", expectedColor, parsedColor)
}

// Verify the first color works with an expected ERROR string
var testString = "[ERROR]: There's something wrong going on!"
var parsedRegex = compiledRegexColors[0].regex
testString := "[ERROR]: There's something wrong going on!"
parsedRegex := compiledRegexColors[0].regex
if parsedRegex.MatchString(testString) {
return //it matches!
} else {
Expand All @@ -38,28 +40,38 @@ func TestParseRegexPatterns(t *testing.T) {
}

func TestColorize(t *testing.T) {
// load in the default config file so we get some regex patterns
// load in the default config file, so we get some regex patterns
HandleConfigFile("logstation.default.conf")

// Get the compiledRegexColors
var compiledRegexColors = ParseRegexPatterns()
compiledRegexColors := ParseRegexPatterns()

// Test a ERROR/hired line
var testString = "[ERROR]: There's something wrong going on!"
var testLogFile = "test/logfile.log"
var logMessage = Colorize(testString, testLogFile, compiledRegexColors)
// make a testString HiRed ANSI so we can compare the output of Colorize
var testStringColored = color.HiRedString(testString)
// Enable ANSI colors regardless of terminal state
color.NoColor = false

// Compare the test built string to the Colorized string
// TODO: Something is stripping ANSI escape codes above. This makes the compare always pass. Need to fix this.
var escapedLogMessage = fmt.Sprintf("%q", logMessage.Text)
var escapedTestStringColored = fmt.Sprintf("%q", testStringColored)
if escapedTestStringColored != escapedLogMessage {
t.Errorf("LogMessage.Text should have been colored red. It was not. line: %s", logMessage.Text)
// Test a INFO line
testLine := fswatcher.Line{
Line: "[INFO]: You might want to know about this...",
File: "test/logfile.log",
}
if logMessage.LogFile != testLogFile {
t.Errorf("LogMessage.LogFile should have been %s. Ended up with %s", testLogFile, logMessage.LogFile)
logMessage := Colorize(testLine.Line, testLine.File, compiledRegexColors)

// Setup a read buffer to read back the lines
rb := new(bytes.Buffer)
_, err := fmt.Fprint(rb, logMessage.Text, "\n")
if err != nil {
return
}

// Read the line back from the buffer
line, _ := rb.ReadString('\n')
scannedLine := fmt.Sprintf("%q", line)

// Prepare a colored string to compare to
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m\n", color.FgHiGreen, testLine.Line)
escapedForm := fmt.Sprintf("%q", colored)

if scannedLine != escapedForm {
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
}
}
18 changes: 9 additions & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ import (
)

func TestHandleConfigFile_Default(t *testing.T) {
// blank out all of viper's configs
// Blank out all of viper's configs
viper.Reset()
// process the default config
// Process the default config
HandleConfigFile("logstation.default.conf")

// Should have 3 log files processed
var parsedLogs = len(viper.GetStringSlice("logs"))
var expectedLogs = 3
parsedLogs := len(viper.GetStringSlice("logs"))
expectedLogs := 3
if parsedLogs != expectedLogs {
t.Errorf("Should have had %d log files. Ended up with %d", expectedLogs, parsedLogs)
}

// Should have a logStationName called "logstation"
var parsedName = viper.GetString("logStationName")
var expectedName = "logstation"
parsedName := viper.GetString("logStationName")
expectedName := "logstation"
if parsedName != expectedName {
t.Errorf("Should have had a logstation name of \"%s\". Ended up with %s", expectedName, parsedName)
}
}

func TestHandleConfigFile_BadPath(t *testing.T) {
// blank out all of viper's configs
// Blank out all of viper's configs
viper.Reset()
// attempt to remove logstation.conf
// Attempt to remove logstation.conf
err := os.Remove("logstation.conf")
if !(err == nil || os.IsNotExist(err)) { // continue if no errors or if the conf file doesn't exist
panic(err)
Expand All @@ -49,7 +49,7 @@ func TestHandleConfigFile_BadPath(t *testing.T) {
cmd.Env = append(os.Environ(), "DO_IT=1")
err = cmd.Run()
if err == nil {
// it ran fine. let's check if it wrote the log file
// It ran fine. Let's check if it wrote the log file
_, err = os.Stat("logstation.conf")
if err == nil {
return // logstation.conf exists! Test passes
Expand Down

0 comments on commit 11673b9

Please sign in to comment.