Skip to content

Commit

Permalink
refactor: streamline command setup in stage7.go
Browse files Browse the repository at this point in the history
- Removed unnecessary PATH manipulation and integrated command setup directly within the testType2 function.
- Utilized the CommandDetails struct for defining custom commands, specifically for the "my_exe" executable, enhancing code clarity and maintainability.
- Improved consistency in command setup across stages by aligning with previous refactoring efforts.
  • Loading branch information
ryan-gang committed Jan 17, 2025
1 parent d484d9c commit 6bf38ca
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions internal/stage7.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package internal

import (
"fmt"
"os"
"path/filepath"

custom_executable "github.com/codecrafters-io/shell-tester/internal/custom_executable/build"
"github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter"
"github.com/codecrafters-io/shell-tester/internal/shell_executable"
"github.com/codecrafters-io/shell-tester/internal/test_cases"
"github.com/codecrafters-io/tester-utils/test_case_harness"
)

func testType2(stageHarness *test_case_harness.TestCaseHarness) error {
// Add the random directory to PATH (where the my_exe file is created)
randomDir, err := getRandomDirectory(stageHarness)
if err != nil {
return err
}

path := os.Getenv("PATH")
logger := stageHarness.Logger
shell := shell_executable.NewShellExecutable(stageHarness)
shell.Setenv("PATH", fmt.Sprintf("%s:%s", randomDir, path))
asserter := logged_shell_asserter.NewLoggedShellAsserter(shell)

customExecutablePath := filepath.Join(randomDir, "my_exe")
err = custom_executable.CreateSignaturePrinterExecutable(getRandomString(), customExecutablePath)
executableName := "my_exe"
executableDir, err := SetUpCustomCommands(stageHarness, shell, []CommandDetails{
{CommandType: "signature_printer", CommandName: executableName, CommandMetadata: getRandomString()},
}, true)
if err != nil {
return err
}
asserter := logged_shell_asserter.NewLoggedShellAsserter(shell)

if err := asserter.StartShellAndAssertPrompt(true); err != nil {
return err
Expand All @@ -44,7 +34,7 @@ func testType2(stageHarness *test_case_harness.TestCaseHarness) error {

var expectedPath = ""
if executable == "my_exe" {
expectedPath = customExecutablePath
expectedPath = filepath.Join(executableDir, executableName)
}

if err := testCase.RunForExecutable(asserter, shell, logger, expectedPath); err != nil {
Expand Down

0 comments on commit 6bf38ca

Please sign in to comment.