(Kotlin) "Build your own shell" -Stage #RA6 Output does not match expected value

I’m stuck on Stage #RA6.

I’ve tried to reproduce what the tester says manually running my program but I haven’t had any luck.

Here are my logs:

[tester::#RA6] Running tests for Stage #RA6 (Navigation - The cd builtin: Absolute paths)
[tester::#RA6] Running ./your_program.sh
[your-program] $ cd /tmp/apple/strawberry/mango
[tester::#RA6] Output does not match expected value.
[tester::#RA6] Expected: "$ pwd"
[tester::#RA6] Received: "pwd"
[your-program] pwd
[your-program] $ /tmp/apple/strawberry/mango
[your-program] $ 
[tester::#RA6] Assertion failed.
[tester::#RA6] Test failed

And here’s a snippet of my code:

fun main() {
    val shellState = ShellState(
        currentDirectory = File(System.getProperty("user.dir")).canonicalFile,
        environmentVariables = mutableMapOf(
            EnvVar.PATH to (System.getenv("PATH") ?: ""),
            EnvVar.HOME to (System.getProperty("user.home") ?: "/")
        )
    )

    do {
        print("$ ")
        val userInput = readln()
        val splitUserInput = userInput.split(" ")
        val command = splitUserInput[0]
        val args = if (splitUserInput.size > 1) splitUserInput.subList(1, splitUserInput.size) else emptyList()

        val builtin = BuiltinCommand.fromCommand(command)
        if (builtin != null) {
            handleBuiltinCommand(builtin, args, shellState)
        } else {
            executeExternalCommand(command, args, shellState)
        }
    } while (true)
}

fun changeDirectory(shellState: ShellState, newPath: String) {
    val targetDirectory = if (newPath.startsWith("/")) {
        File(newPath)
    } else {
        File(shellState.currentDirectory, newPath)
    }

    if (targetDirectory.exists() && targetDirectory.isDirectory) {
        shellState.currentDirectory = targetDirectory.canonicalFile
    } else {
        println("cd: $newPath: No such file or directory: $newPath")
    }
}

link to my code: GitHub - DanielUH2019/codecrafters-shell-kotlin

Thanks for sharing your code! I’ll take a look and get back to you by the end of the week.

1 Like

Hi,
I am also stuck at this point, with same error. My code is in java. Can someone please help me as well?

[tester::#RA6] Running tests for Stage #RA6 (Navigation - The cd builtin: Absolute paths)
[tester::#RA6] Running ./your_program.sh
[your-program] $ cd /tmp/apple/apple/banana
[tester::#RA6] Output does not match expected value.
[tester::#RA6] Expected: "$ pwd"
[tester::#RA6] Received: "pwd"
[your-program] pwd
[your-program] $ 
[tester::#RA6] Assertion failed.
[tester::#RA6] Test failed

Thanks.

2 Likes

Same here, there must be a problem in the test it should expect pwd not $ pwd probably a bad copy-paste they did on their end.