Skip to content

Commit

Permalink
Pretty print exec results (gopinath-langote#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
gopinath-langote authored and themayurkumbhar committed Oct 8, 2019
1 parent 5dd2e3f commit 3b8a9f8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 86 deletions.
6 changes: 2 additions & 4 deletions cmd/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func (oneBuildConfiguration *OneBuildConfiguration) GetCommand(name string) (val

// Print prints the configuration to the console
func (oneBuildConfiguration *OneBuildConfiguration) Print() {
fmt.Println(utils.BANNER())
fmt.Println("project: " + oneBuildConfiguration.Project)
fmt.Println(utils.Dash() + "\nproject: " + oneBuildConfiguration.Project)
if oneBuildConfiguration.Before != "" {
fmt.Println("before: " + oneBuildConfiguration.Before)
}
Expand All @@ -76,6 +75,5 @@ func (oneBuildConfiguration *OneBuildConfiguration) Print() {
fmt.Println(strings.TrimSpace(k) + " | " + strings.TrimSpace(v))
}
}

fmt.Println(utils.BANNER())
fmt.Println(utils.Dash())
}
34 changes: 25 additions & 9 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,17 @@ func ExecutePlan(commands ...string) {
executeAndStopIfFailed(executionPlan.After, executeStart)
}

utils.PrintResultsBanner(true, executeStart)
printResultsBanner(true, executeStart)
}

func executeAndStopIfFailed(command *models.CommandContext, executeStart time.Time) {
command.PrintBanner()
command.PrintPhaseBanner()
err := command.CommandSession.Run()
if err != nil {
exitCode := (err.Error())[12:]
utils.PrintlnErr("Execution failed during phase \"" +
command.Name +
"\" - Execution of the script \"" +
command.Command +
"\" returned non-zero exit code : " + exitCode)
utils.PrintResultsBanner(false, executeStart)
text := "\nExecution failed in phase '" + command.Name + "' – exit code: " + exitCode
fmt.Println(utils.Colored(text, utils.RED))
printResultsBanner(false, executeStart)
utils.ExitWithCode(exitCode)
}
}
Expand All @@ -70,7 +67,7 @@ func buildExecutionPlan(onebuildConfig config.OneBuildConfiguration, commands ..
if executionCommand == "" {
fmt.Println(utils.ColoredB("\nError building execution plan. Command \""+name+"\" not found.", utils.RED))
onebuildConfig.Print()
utils.Exit(127)
utils.ExitWithCode("127")
}
executionPlan.Commands = append(executionPlan.Commands, &models.CommandContext{
Name: name, Command: executionCommand, CommandSession: bashCommand(sh.NewSession(), executionCommand)})
Expand All @@ -88,3 +85,22 @@ func buildExecutionPlan(onebuildConfig config.OneBuildConfiguration, commands ..
func bashCommand(s *sh.Session, command string) *sh.Session {
return s.Command("bash", "-c", command)
}

// PrintResultsBanner prints result banner at the end of the test
func printResultsBanner(isSuccess bool, startTime time.Time) {
timeDelta := time.Since(startTime)
minutes := int64(timeDelta.Minutes())
secs := int64(timeDelta.Seconds()) % 60
var timeStr string
if minutes == 0 {
timeStr = fmt.Sprintf("%.2ds", secs)
} else {
timeStr = fmt.Sprintf("%.2dm %.2ds", minutes, secs)
}
result := utils.ColoredB("SUCCESS", utils.CYAN)
if !isSuccess {
result = utils.ColoredB("FAILURE", utils.RED)
}
result = fmt.Sprintf("%s - Total Time: %s", result, timeStr)
fmt.Println("\n" + utils.Dash() + "\n" + result + "\n" + utils.Dash())
}
8 changes: 4 additions & 4 deletions cmd/models/onebuild-execution-plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (executionPlan *OneBuildExecutionPlan) HasCommands() bool {
// Print prints execution plan
func (executionPlan *OneBuildExecutionPlan) Print() {
fmt.Println()
fmt.Println(utils.ColoredBU("Execution plan", utils.CYAN))
fmt.Println(utils.ColoredU("Execution plan", utils.CYAN))
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', tabwriter.TabIndent)

phase, cmd := longestPhaseAndCommandValue(executionPlan)
Expand Down Expand Up @@ -98,8 +98,8 @@ func dashesOfLength(text string) string {
return strings.Repeat("-", len(text))
}

// PrintBanner prints the CommandContext's name in a banner of the standard length
func (c *CommandContext) PrintBanner() {
// PrintPhaseBanner prints the CommandContext's name in a banner of the standard length
func (c *CommandContext) PrintPhaseBanner() {
centreLength := utf8.RuneCountInString(c.Name) +
utf8.RuneCountInString(bannerOpen) +
utf8.RuneCountInString(bannerClose)
Expand All @@ -122,4 +122,4 @@ func (c *CommandContext) PrintBanner() {
bannerClose,
strings.Repeat("-", numDashesRight),
)
}
}
49 changes: 7 additions & 42 deletions cmd/utils/printer.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package utils

import (
"fmt"
"strings"
"time"

"github.com/logrusorgru/aurora"
"strings"
)

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

Expand All @@ -34,49 +31,17 @@ func Colored(text string, color OneBuildColor) string {
return colorize(text, color).String()
}

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

// PrintlnErr prints error line to console in bold Red
func PrintlnErr(text string) {
fmt.Println()
fmt.Println(ColoredB(text, RED))
// ColoredU return text in color with bold and underline format
func ColoredU(text string, color OneBuildColor) string {
return colorize(text, color).Underline().String()
}

func colorize(text string, color OneBuildColor) aurora.Value {
var coloredText aurora.Value
if color == CYAN {
coloredText = aurora.BrightCyan(text)
} else {
coloredText = aurora.Red(text)
coloredText = aurora.BrightRed(text)
}
return coloredText
}

// PrintResultsBanner prints result banner at the end of the test
func PrintResultsBanner(isSuccess bool, startTime time.Time) {
timeDelta := time.Since(startTime).Round(time.Second)
mins := int64(timeDelta / time.Minute)
secs := int64((timeDelta % time.Minute) / time.Second)
var timeStr string
if mins == 0 {
timeStr = fmt.Sprintf("%.2ds", secs)
} else {
timeStr = fmt.Sprintf("%.2dm %.2ds", mins, secs)
}
result := aurora.BrightCyan("SUCCESS")
if !isSuccess {
result = aurora.Red("FAILURE")
}

s := fmt.Sprintf("%s - Total Time: %s", result, timeStr)

if isSuccess {
fmt.Println()
}
fmt.Println(BANNER())
fmt.Println(s)
fmt.Println(BANNER())
}
7 changes: 1 addition & 6 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ const (
MaxOutputWidth = 72
)

// Exit exits the current process with specified exit code
func Exit(code int) {
os.Exit(code)
}

// ExitWithCode exits the current process with specified exit code provided as string
func ExitWithCode(code string) {
exitCode, _ := strconv.Atoi(code)
Expand All @@ -23,7 +18,7 @@ func ExitWithCode(code string) {

// ExitError exit the program with non success code
func ExitError() {
Exit(1)
ExitWithCode("1")
}

// SliceIndex find the index of element matching given predicate
Expand Down
2 changes: 1 addition & 1 deletion testing/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestMain(m *testing.M) {
binaryPath = testDir + "/" + binaryName
buildBinary(binaryPath)

fmt.Println(utils2.BANNER() + "\nBinary Path:- '" + binaryPath + "'\n" + utils2.BANNER())
fmt.Println(utils2.Dash() + "\nBinary Path:- '" + binaryPath + "'\n" + utils2.Dash())

exitCode := m.Run()

Expand Down
54 changes: 35 additions & 19 deletions testing/fixtures/execute_cmd_fixtures.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package fixtures

import (
"strings"
"testing"

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

func successBanner() string {
return strings.Repeat("-", 72) + "\n" + utils.Colored("SUCCESS", utils.CYAN)
}

func featureExecuteCmdTestData() []Test {
feature := "exec"

Expand Down Expand Up @@ -42,7 +37,7 @@ build echo building project
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
building project
` + successBanner()
`
return Test{
Feature: feature,
Name: "shouldExecuteAvailableCommand",
Expand All @@ -51,7 +46,8 @@ building project
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput)
return assert.Contains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}
Expand Down Expand Up @@ -103,7 +99,7 @@ running pre-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
building project
` + successBanner()
`
return Test{
Feature: feature,
Name: "shouldExecuteBeforeCommand",
Expand All @@ -112,7 +108,8 @@ building project
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput)
return assert.Contains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}
Expand All @@ -137,7 +134,7 @@ building project
-------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]--------------------------------
running post-command
` + successBanner()
`
return Test{
Feature: feature,
Name: "shouldExecuteAfterCommand",
Expand All @@ -146,7 +143,8 @@ 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 assert.Contains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}
Expand Down Expand Up @@ -175,7 +173,7 @@ building project
-------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]--------------------------------
running post-command
` + successBanner()
`
return Test{
Feature: feature,
Name: "shouldExecuteBeforeAndAfterCommand",
Expand All @@ -184,7 +182,8 @@ 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 assert.Contains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}
Expand All @@ -207,8 +206,6 @@ after echo running post-command
-------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]-------------------------------
` + utils.ColoredB("Execution failed during phase \"before\" - Execution of the script \"exit 10\" returned non-zero exit code : 10", utils.RED) + `
`
return Test{
Feature: feature,
Expand All @@ -218,7 +215,10 @@ 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 assert.Contains(t, actualOutput, expectedOutput) &&
assertFailureMessage(t, actualOutput, "before", "10") &&
assertFailureBanner(t, actualOutput)

},
}
}
Expand All @@ -243,8 +243,6 @@ after echo running post-command
-------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]-------------------------------
running pre-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
` + utils.ColoredB("Execution failed during phase \"build\" - Execution of the script \"invalid_command\" returned non-zero exit code : 127", utils.RED) + `
`
return Test{
Feature: feature,
Expand All @@ -254,7 +252,25 @@ 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 assert.Contains(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)
}

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)
}

func assertFailureBanner(t *testing.T, actualOutput string) bool {
bannerOutput := utils.ColoredB("FAILURE", utils.RED) + " - Total Time"
return assert.Contains(t, actualOutput, bannerOutput)
}
2 changes: 1 addition & 1 deletion testing/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func colorize(text string, color OneBuildColor) aurora.Value {
if color == CYAN {
coloredText = aurora.BrightCyan(text)
} else {
coloredText = aurora.Red(text)
coloredText = aurora.BrightRed(text)
}
return coloredText
}

0 comments on commit 3b8a9f8

Please sign in to comment.