Skip to content

Commit

Permalink
Test without color formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gopinath-langote committed Oct 22, 2019
1 parent 268f54b commit 467ef74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 57 deletions.
50 changes: 23 additions & 27 deletions testing/fixtures/execute_cmd_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/gopinath-langote/1build/testing/utils"
"github.com/stretchr/testify/assert"
)

func featureExecuteCmdTestData() []Test {
Expand Down Expand Up @@ -34,7 +33,7 @@ Phase Command
build echo building project
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------
building project
`
Expand All @@ -46,7 +45,7 @@ building project
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput) &&
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
Expand All @@ -59,7 +58,7 @@ commands:
- build: echo building project
`

expectedOutput := utils.ColoredB("\nError building execution plan. Command \"random\" not found.", utils.RED) + `
expectedOutput := "\nError building execution plan. Command \"random\" not found." + `
------------------------------------------------------------------------
project: Sample Project
commands:
Expand All @@ -74,7 +73,7 @@ build | echo building project
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput)
return utils.AssertContains(t, actualOutput, expectedOutput)
},
}
}
Expand All @@ -94,9 +93,9 @@ before echo running pre-command
build echo building project
-------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]-------------------------------
-------------------------------[ ` + "before" + ` ]-------------------------------
running pre-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------
building project
`
Expand All @@ -108,7 +107,7 @@ building project
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput) &&
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
Expand All @@ -129,9 +128,9 @@ build echo building project
after echo running post-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------
building project
-------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "after" + ` ]--------------------------------
running post-command
`
Expand All @@ -143,7 +142,7 @@ running post-command
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput) &&
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
Expand All @@ -166,11 +165,11 @@ build echo building project
after echo running post-command
-------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]-------------------------------
-------------------------------[ ` + "before" + ` ]-------------------------------
running pre-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------
building project
-------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "after" + ` ]--------------------------------
running post-command
`
Expand All @@ -182,7 +181,7 @@ running post-command
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput) &&
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
Expand All @@ -205,7 +204,7 @@ build echo building project
after echo running post-command
-------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]-------------------------------
-------------------------------[ ` + "before" + ` ]-------------------------------
`
return Test{
Feature: feature,
Expand All @@ -215,7 +214,7 @@ after echo running post-command
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput) &&
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertFailureMessage(t, actualOutput, "before", "10") &&
assertFailureBanner(t, actualOutput)

Expand All @@ -240,9 +239,9 @@ build invalid_command
after echo running post-command
-------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]-------------------------------
-------------------------------[ ` + "before" + ` ]-------------------------------
running pre-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------
`
return Test{
Feature: feature,
Expand All @@ -252,25 +251,22 @@ running pre-command
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput) &&
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertFailureMessage(t, actualOutput, "build", "127") &&
assertFailureBanner(t, actualOutput)

},
}
}

func assertSuccessBanner(t *testing.T, actualOutput string) bool {
bannerOutput := utils.ColoredB("SUCCESS", utils.CYAN) + " - Total Time"
return assert.Contains(t, actualOutput, bannerOutput)
return utils.AssertContains(t, actualOutput, "SUCCESS - Total Time")
}

func assertFailureMessage(t *testing.T, actualOutput string, phase string, exitCode string) bool {
errorMessage := utils.Colored("\nExecution failed in phase '"+phase+"' – exit code: "+exitCode, utils.RED)
return assert.Contains(t, actualOutput, errorMessage)
errorText := "\nExecution failed in phase '" + phase + "' – exit code: " + exitCode
return utils.AssertContains(t, actualOutput, errorText)
}

func assertFailureBanner(t *testing.T, actualOutput string) bool {
bannerOutput := utils.ColoredB("FAILURE", utils.RED) + " - Total Time"
return assert.Contains(t, actualOutput, bannerOutput)
return utils.AssertContains(t, actualOutput, "FAILURE - Total Time")
}
41 changes: 11 additions & 30 deletions testing/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package utils

import (
"github.com/gopinath-langote/1build/testing/def"
"github.com/logrusorgru/aurora"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"regexp"
"strings"
"testing"
)

const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

var regex = regexp.MustCompile(ansi)

// CreateConfigFile creates a config file
func CreateConfigFile(dir string, content string) error {
return ioutil.WriteFile(dir+"/"+def.ConfigFileName, []byte(content), 0777)
Expand Down Expand Up @@ -36,38 +42,13 @@ const (
MaxOutputWidth = 72
)

// OneBuildColor represents type for color enum
type OneBuildColor int

const (
// CYAN is 1build's default color standard
CYAN OneBuildColor = 0

// RED is used in failure messages
RED OneBuildColor = 1
)

// PlainBanner return dashes with fixed length - 72
func PlainBanner() string {
return strings.Repeat("-", MaxOutputWidth)
}

// ColoredB return text in color with bold format
func ColoredB(text string, color OneBuildColor) string {
return colorize(text, color).Bold().String()
}

// Colored return text in color
func Colored(text string, color OneBuildColor) string {
return colorize(text, color).String()
}

func colorize(text string, color OneBuildColor) aurora.Value {
var coloredText aurora.Value
if color == CYAN {
coloredText = aurora.BrightCyan(text)
} else {
coloredText = aurora.BrightRed(text)
}
return coloredText
// AssertContains checks two string without ANSI colors
func AssertContains(t *testing.T, actualOutput string, expectedOutput string) bool {
actualOutput = regex.ReplaceAllString(actualOutput, "")
return assert.Contains(t, actualOutput, expectedOutput)
}

0 comments on commit 467ef74

Please sign in to comment.