Skip to content

Commit

Permalink
Improve the colors unit tests to test all colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrews committed Jun 17, 2023
1 parent 33d0a3c commit 05b2771
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 25 deletions.
62 changes: 37 additions & 25 deletions colors_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"bytes"
"fmt"
"github.com/fatih/color"
"github.com/jdrews/go-tailer/fswatcher"
"strings"
"testing"
)

Expand Down Expand Up @@ -45,43 +45,55 @@ func TestParseRegexPatterns(t *testing.T) {

// TestColorize is a unit test for the Colorize function
//
// It loads up the default config and parses the color-to-regex patterns in the default config
// It loads up a test config and parses the color-to-regex patterns in the config
// It then colorizes the log line per ANSI specs
// To test if the colors work, it writes the line to a Buffer and then reads it back
// comparing it to an expected log line.
// To test if the colors work,
// it compares ANSI escaped lines for the Colorize output and a locally constructed colored line
func TestColorize(t *testing.T) {
// Load in the default config file, so we get some regex patterns
HandleConfigFile("logstation.default.conf")
// Load in a test config file, which has all the possible colors defined in it
HandleConfigFile("logstation.test.conf")

// Get the compiledRegexColors
compiledRegexColors := ParseRegexPatterns()

// Enable ANSI colors regardless of terminal state
color.NoColor = false

// Test a INFO line
testLine := fswatcher.Line{
Line: "[INFO]: You might want to know about this...",
File: "test/logfile.log",
// Create a map of color name to ansi color
colorMap := map[string]color.Attribute{
"red": color.FgRed,
"green": color.FgGreen,
"yellow": color.FgYellow,
"blue": color.FgBlue,
"magenta": color.FgMagenta,
"cyan": color.FgCyan,
"hired": color.FgHiRed,
"higreen": color.FgHiGreen,
"hiyellow": color.FgHiYellow,
"hiblue": color.FgHiBlue,
"himagenta": color.FgHiMagenta,
"hicyan": color.FgHiCyan,
}
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
}
// Loop through all the types of colors and ensure Colorize can pick it up
for _, regexColor := range compiledRegexColors {
// Create an example line, defined for each of the types of colors
testLine := fswatcher.Line{
Line: "#" + strings.ToUpper(regexColor.color) + "#: You might want to know about this...",
File: "test/logfile.log",
}
// Have Colorize color the line based on regex
logMessage := Colorize(testLine.Line, testLine.File, compiledRegexColors)

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

// 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)
// Prepare a colored string to compare to
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m\n", colorMap[regexColor.color], testLine.Line)
coloredEscaped := fmt.Sprintf("%q", colored)

if scannedLine != escapedForm {
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
if logMessageEscaped != coloredEscaped {
t.Errorf("When testing %s: Expecting %s, got '%s'\n", regexColor.color, coloredEscaped, logMessageEscaped)
}
}

}
65 changes: 65 additions & 0 deletions logstation.test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# _ _ _ _
#| | ___ __ _ ___ | |_ __ _ | |_ (_) ___ _ _
#| | / _ \ / _` | (_-< | _| / _` | | _| | | / _ \ | ' \
#|_| \___/ \__, | /__/ \__| \__,_| \__| |_| \___/ |_||_|
# |___/
#
# logstaion config file
# https://github.com/jdrews/logstation

# List of log files to tail
# The list can be single log files or a wildcard glob to pick up
# all files in a directory or rotating log files
# Windows example of setting up logs
logs = [
'e:\git\logstation\test\logfile.log',
'e:\git\logstation\test\with space\logfile.log',
'e:\git\logstation\test\*.log'
]
# Unix example of setting up logs
#logs = [
# '/home/jdrews/git/logstation/logfile.log',
# '/home/jdrews/git/logstation/*.log'
#]

# Unique name for logstation instance
# This name will be prepended to the browser tab
# Can be useful when connecting to multiple logstations
logStationName = "logstation"

# Setup your syntax highlighting below
# matching gives priority to the top most
# if the regular experssion passes, the entire log line will be colored
#
# "color" can be any of of the following
# red, green, yellow, blue, magenta, cyan
# hired, higreen, hiyellow, hiblue, himagenta, hicyan
# NOTE: these are ANSI colors.
# The "hi" colors above are recommended as they show up much better on black backgrounds
#
# "regex" is a regular expression matched against a log line
# syntax in particular follows https://github.com/google/re2/wiki/Syntax
syntaxColors = [
{ color="red", regex=".*#RED#.*" },
{ color="green", regex=".*#GREEN#.*" },
{ color="yellow", regex=".*#YELLOW#.*" },
{ color="blue", regex=".*#BLUE#.*" },
{ color="magenta", regex=".*#MAGENTA#.*" },
{ color="cyan", regex=".*#CYAN#.*" },
{ color="hired", regex=".*#HIRED#.*" },
{ color="higreen", regex=".*#HIGREEN#.*" },
{ color="hiyellow", regex=".*#HIYELLOW#.*" },
{ color="hiblue", regex=".*#HIBLUE#.*" },
{ color="himagenta", regex=".*#HIMAGENTA#.*" },
{ color="hicyan", regex=".*#HICYAN#.*" },
]

[server_settings]
webServerPort = 8884 # Webserver port to listen on
webServerAddress = "0.0.0.0" # Webserver address to listen on

# Disable CORS checking on the server
# This is a security vulnerability if you disable CORS. Please be careful!
# Read more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
disableCORS = false

0 comments on commit 05b2771

Please sign in to comment.