Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions internal/stage6.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func testType1(stageHarness *test_case_harness.TestCaseHarness) error {
for _, invalidCommand := range invalidCommands {
command := fmt.Sprintf("type %s", invalidCommand)

// ToDo: Should be match not contains
testCase := test_cases.SingleLineOutputTestCase{
Command: command,
ExpectedPattern: regexp.MustCompile(fmt.Sprintf(`^(bash: type: )?%s[:]? not found$`, invalidCommand)),
Expand Down
22 changes: 21 additions & 1 deletion internal/test_cases/single_line_output_test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"errors"
"fmt"
"regexp"
"strings"
"time"

"github.com/codecrafters-io/shell-tester/internal/shell_executable"
"github.com/codecrafters-io/tester-utils/logger"
"github.com/fatih/color"
)

// SingleLineOutputTestCase verifies a prompt exists, sends a command and matches the output against a string.
Expand Down Expand Up @@ -69,7 +71,7 @@ func (t SingleLineOutputTestCase) Run(shell *shell_executable.ShellExecutable, l
if err == nil {
shell.LogOutput(sanitizeLogOutput(restOfOutput))
}
return fmt.Errorf("Expected first line of output to %s, got %q", t.ExpectedPatternExplanation, string(cleanedOutput))
return fmt.Errorf(getColoredErrorMessage(t.ExpectedPatternExplanation, string(cleanedOutput)))
}

logger.Successf("✓ %s", t.SuccessMessage)
Expand Down Expand Up @@ -103,3 +105,21 @@ func sanitizeLogOutput(buf []byte) []byte {
buf = stripLineEnding(buf)
return buf
}

func colorizeString(colorToUse color.Attribute, msg string) string {
c := color.New(colorToUse)
return c.Sprint(msg)
}

func getColoredErrorMessage(expectedPatternExplanation string, cleanedOutput string) string {
indent := 32 - 3 + len(strings.Split(expectedPatternExplanation, " ")[0]) + 1

errorMsg := "Expected first line of output to" // 32
errorMsg += " " + colorizeString(color.FgGreen, expectedPatternExplanation)
errorMsg += "\n"
errorMsg += strings.Repeat(" ", indent)
errorMsg += colorizeString(color.FgRed, "got") // 3
errorMsg += " " + colorizeString(color.FgRed, cleanedOutput)

return errorMsg
}
Loading