Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print total results banner at the end of execution #160

Merged
merged 3 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package exec

import (
"fmt"
"time"

"github.com/codeskyblue/go-sh"
"github.com/gopinath-langote/1build/cmd/config"
"github.com/gopinath-langote/1build/cmd/models"
Expand All @@ -11,6 +13,8 @@ import (
// ExecutePlan executes the Execution plan
func ExecutePlan(commands ...string) {

executeStart := time.Now()

configuration, err := config.LoadOneBuildConfiguration()
if err != nil {
fmt.Println(err)
Expand All @@ -21,30 +25,29 @@ func ExecutePlan(commands ...string) {
executionPlan.Print()

if executionPlan.HasBefore() {
executeAndStopIfFailed(executionPlan.Before)
executeAndStopIfFailed(executionPlan.Before, executeStart)
}

if executionPlan.HasCommands() {
for _, commandContext := range executionPlan.Commands {
executeAndStopIfFailed(commandContext)
executeAndStopIfFailed(commandContext, executeStart)
}
}

if executionPlan.HasAfter() {
executeAndStopIfFailed(executionPlan.After)
executeAndStopIfFailed(executionPlan.After, executeStart)
}

fmt.Println()
fmt.Println(utils.ColoredB("SUCCESS", utils.CYAN))

utils.PrintResultsBanner(true, executeStart)
}

func executeAndStopIfFailed(command *models.CommandContext) {
func executeAndStopIfFailed(command *models.CommandContext, executeStart time.Time) {
command.PrintBanner()
err := command.CommandSession.Run()
if err != nil {
exitCode := (err.Error())[12:]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing missing is removing the dashed banner from the failure message.
Screenshot 2019-10-01 at 17 43 13

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check issue description #155 for more details.

Copy link
Owner

@gopinath-langote gopinath-langote Oct 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With message Execution failed: phase 'a' - exit code: 127

phase and exit code could be anything

utils.PrintlnDashedErr("Execution failed during phase \"" + command.Name + "\" - Execution of the script \"" + command.Command + "\" returned non-zero exit code : " + exitCode)
utils.PrintResultsBanner(false, executeStart)
utils.ExitWithCode(exitCode)
}
}
Expand Down
30 changes: 28 additions & 2 deletions cmd/utils/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package utils

import (
"fmt"
"github.com/logrusorgru/aurora"
"strings"
"time"

"github.com/logrusorgru/aurora"
)

// BANNER return dashes with fixed length - 72
Expand All @@ -19,7 +21,7 @@ const (
CYAN OneBuildColor = 0

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

// ColoredB return text in color with bold format
Expand Down Expand Up @@ -55,3 +57,27 @@ func colorize(text string, color OneBuildColor) aurora.Value {
}
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)

fmt.Println()
fmt.Println(BANNER())
fmt.Println(s)
fmt.Println(BANNER())
}
17 changes: 9 additions & 8 deletions testing/fixtures/execute_cmd_fixtures.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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 @@ -37,8 +42,7 @@ build echo building project
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
building project

` + utils.ColoredB("SUCCESS", utils.CYAN) + `
`
` + successBanner()
return Test{
Feature: feature,
Name: "shouldExecuteAvailableCommand",
Expand Down Expand Up @@ -99,8 +103,7 @@ running pre-command
-------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]--------------------------------
building project

` + utils.ColoredB("SUCCESS", utils.CYAN) + `
`
` + successBanner()
return Test{
Feature: feature,
Name: "shouldExecuteBeforeCommand",
Expand Down Expand Up @@ -134,8 +137,7 @@ building project
-------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]--------------------------------
running post-command

` + utils.ColoredB("SUCCESS", utils.CYAN) + `
`
` + successBanner()
return Test{
Feature: feature,
Name: "shouldExecuteAfterCommand",
Expand Down Expand Up @@ -173,8 +175,7 @@ building project
-------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]--------------------------------
running post-command

` + utils.ColoredB("SUCCESS", utils.CYAN) + `
`
` + successBanner()
return Test{
Feature: feature,
Name: "shouldExecuteBeforeAndAfterCommand",
Expand Down