Skip to content

Commit

Permalink
feat: add randomness to cd commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-gang committed May 24, 2024
1 parent 9d723e7 commit 878ed00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
21 changes: 19 additions & 2 deletions internal/stage10.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package internal

import (
"fmt"
"os"
"path"
"regexp"

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

Expand All @@ -17,9 +20,13 @@ func testCd1(stageHarness *test_case_harness.TestCaseHarness) error {
return err
}

directory := "/usr/local/bin"
directory, err := getRandomDirectory()
if err != nil {
return err
}

testCase := test_cases.CDAndPWDTestCase{Directory: directory, Response: directory}
err := testCase.Run(shell, logger)
err = testCase.Run(shell, logger)
if err != nil {
return err
}
Expand All @@ -40,3 +47,13 @@ func testCd1(stageHarness *test_case_harness.TestCaseHarness) error {

return assertShellIsRunning(shell, logger)
}

// getRandomDirectory creates a random directory in /tmp, creates the directories and returns the full path
// directory is of the form `/tmp/<random-word>/<random-word>/<random-word>`
func getRandomDirectory() (string, error) {
randomDir := path.Join("/tmp", random.RandomWord(), random.RandomWord(), random.RandomWord())
if err := os.MkdirAll(randomDir, 0755); err != nil {
return "", fmt.Errorf("CodeCrafters internal error. Error creating directory %s: %v", randomDir, err)
}
return randomDir, nil
}
26 changes: 22 additions & 4 deletions internal/stage11.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package internal

import (
"fmt"
"os"
"path"
"strings"

"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"
Expand All @@ -14,19 +19,32 @@ func testCd2(stageHarness *test_case_harness.TestCaseHarness) error {
return err
}

testCase1 := test_cases.CDAndPWDTestCase{Directory: "/usr/", Response: "/usr"}
err := testCase1.Run(shell, logger)
directory, err := getRandomDirectory()
if err != nil {
return err
}

separator := os.PathSeparator
parentDirs := strings.Split(directory, string(separator))
fmt.Println(directory, separator, parentDirs, len(parentDirs))

dir := string(separator) + path.Join(parentDirs[:len(parentDirs)-2]...)
testCase1 := test_cases.CDAndPWDTestCase{Directory: dir, Response: dir}
err = testCase1.Run(shell, logger)
if err != nil {
return err
}

testCase2 := test_cases.CDAndPWDTestCase{Directory: "./local/bin", Response: "/usr/local/bin"}
dir = "." + string(separator) + path.Join(parentDirs[len(parentDirs)-2:]...)
absoluteDir := string(separator) + path.Join(parentDirs...)
testCase2 := test_cases.CDAndPWDTestCase{Directory: dir, Response: absoluteDir}
err = testCase2.Run(shell, logger)
if err != nil {
return err
}

testCase3 := test_cases.CDAndPWDTestCase{Directory: "../../", Response: "/usr"}
absoluteDir = string(separator) + path.Join(parentDirs[:len(parentDirs)-3]...)
testCase3 := test_cases.CDAndPWDTestCase{Directory: "../../../", Response: absoluteDir}
err = testCase3.Run(shell, logger)
if err != nil {
return err
Expand Down
9 changes: 7 additions & 2 deletions internal/stage12.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ func testCd3(stageHarness *test_case_harness.TestCaseHarness) error {
return err
}

testCase1 := test_cases.CDAndPWDTestCase{Directory: "/usr/local/bin", Response: "/usr/local/bin"}
err := testCase1.Run(shell, logger)
directory, err := getRandomDirectory()
if err != nil {
return err
}

testCase1 := test_cases.CDAndPWDTestCase{Directory: directory, Response: directory}
err = testCase1.Run(shell, logger)
if err != nil {
return err
}
Expand Down

0 comments on commit 878ed00

Please sign in to comment.