diff --git a/internal/stage6.go b/internal/stage6.go index d4f3c66c..48f20a82 100644 --- a/internal/stage6.go +++ b/internal/stage6.go @@ -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)), diff --git a/internal/test_cases/single_line_output_test_case.go b/internal/test_cases/single_line_output_test_case.go index 09853952..c048d86f 100644 --- a/internal/test_cases/single_line_output_test_case.go +++ b/internal/test_cases/single_line_output_test_case.go @@ -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. @@ -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) @@ -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 +}