Skip to content

Commit

Permalink
refactor(test_cases/type_of_command_test_case): Simplify RunForExecut…
Browse files Browse the repository at this point in the history
…able method and remove unnecessary code from RunForBuiltin
  • Loading branch information
andy1li committed Dec 27, 2024
1 parent 8b1ebbd commit ffda56d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
8 changes: 7 additions & 1 deletion internal/stage7.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/stage9.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 4 additions & 13 deletions internal/test_cases/type_of_command_test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit ffda56d

Please sign in to comment.