From ffda56de34f101664fb060b7f73c0a8f66811947 Mon Sep 17 00:00:00 2001 From: Andy Li <1450947+andy1li@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:45:07 +0800 Subject: [PATCH] refactor(test_cases/type_of_command_test_case): Simplify RunForExecutable method and remove unnecessary code from RunForBuiltin --- internal/stage7.go | 8 +++++++- internal/stage9.go | 3 +-- .../test_cases/type_of_command_test_case.go | 17 ++++------------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/internal/stage7.go b/internal/stage7.go index f881b4fc..df0a3efd 100644 --- a/internal/stage7.go +++ b/internal/stage7.go @@ -42,7 +42,13 @@ func testType2(stageHarness *test_case_harness.TestCaseHarness) error { testCase := test_cases.TypeOfCommandTestCase{ Command: executable, } - if err := testCase.RunForExecutable(asserter, shell, logger, customExecutablePath); err != nil { + + var expectedPath = "" + if executable == "my_exe" { + expectedPath = customExecutablePath + } + + if err := testCase.RunForExecutable(asserter, shell, logger, expectedPath); err != nil { return err } } diff --git a/internal/stage9.go b/internal/stage9.go index 65ae591f..1352861c 100644 --- a/internal/stage9.go +++ b/internal/stage9.go @@ -27,8 +27,7 @@ func testpwd(stageHarness *test_case_harness.TestCaseHarness) error { } typeOfPwdTestCase := test_cases.TypeOfCommandTestCase{ - Command: "pwd", - SuccessMessage: "✓ Received 'pwd is a shell builtin'", + Command: "pwd", } if err := typeOfPwdTestCase.RunForBuiltin(asserter, shell, logger); err != nil { return err diff --git a/internal/test_cases/type_of_command_test_case.go b/internal/test_cases/type_of_command_test_case.go index ce65cde2..c42df74f 100644 --- a/internal/test_cases/type_of_command_test_case.go +++ b/internal/test_cases/type_of_command_test_case.go @@ -11,20 +11,15 @@ import ( ) type TypeOfCommandTestCase struct { - Command string - SuccessMessage string + Command string } func (t *TypeOfCommandTestCase) RunForBuiltin(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger) error { - if t.SuccessMessage == "" { - t.SuccessMessage = "✓ Received expected response" - } - testCase := CommandResponseTestCase{ Command: fmt.Sprintf("type %s", t.Command), ExpectedOutput: fmt.Sprintf(`%s is a shell builtin`, t.Command), FallbackPatterns: []*regexp.Regexp{regexp.MustCompile(fmt.Sprintf(`^%s is a( special)? shell builtin$`, t.Command))}, - SuccessMessage: t.SuccessMessage, + SuccessMessage: "✓ Received expected response", } if err := testCase.Run(asserter, shell, logger); err != nil { @@ -34,12 +29,8 @@ func (t *TypeOfCommandTestCase) RunForBuiltin(asserter *logged_shell_asserter.Lo return nil } -func (t *TypeOfCommandTestCase) RunForExecutable(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger, customExecutablePath string) error { - var expectedPath string - - if t.Command == "my_exe" { - expectedPath = customExecutablePath - } else { +func (t *TypeOfCommandTestCase) RunForExecutable(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger, expectedPath string) error { + if expectedPath == "" { path, err := exec.LookPath(t.Command) if err != nil { return fmt.Errorf("CodeCrafters internal error. Error finding %s in PATH", t.Command)