Problem with 'cd' command and "/app" output in #RA6

I’m stuck on Stage #RA6. I’m not sure why—when I run the same commands locally, everything works fine. For the pwd command, the path is /tmp/strawberry/grape/mango, as expected. However, when I run the tests, they fail and show /app instead.

Here are my logs:

[your-program] $ cd /tmp/strawberry/grape/mango
[your-program] $ pwd
[your-program] /app
[tester::#RA6] ^ Line does not match expected value.
[tester::#RA6] Expected: "/tmp/strawberry/grape/mango"
[tester::#RA6] Received: "/app"

And here’s a snippets of my code:

public class CdCommandHandler implements CommandHandler {

    @Override
    public boolean canHandle(String command) {
        return command.startsWith("cd");
    }

    @Override
    public void handle(String command) {
        String[] commandParts = command.split("\\s+");
        File file = new File(commandParts[1]);

        if (file.isDirectory()) {
            ShellUtils.CURRENT_DIRECTORY = commandParts[1];
        } else {
            System.out.println("cd: " + commandParts[1] + ": No such file or directory");
        }
    }
}
public class PwdCommandHandler implements CommandHandler {

    @Override
    public boolean canHandle(String command) {
        return command.startsWith("pwd");
    }

    @Override
    public void handle(String command) {
        System.out.println(ShellUtils.CURRENT_DIRECTORY);
    }
}

Hi, thanks for your post!

I’m currently out of the office and will be back on March 23. I’ll get back to you as soon as possible once I return.

Are you actually changing the working directory of your shell process, or just setting a variable?

But I guess they run the test inside some container or like, and in their your application runs from /app.

As I understand it, in Java I can’t change the shell’s working directory, so I tried to simulate it. I noticed that others have taken a similar approach. Basically, I’m storing the path in a variable and then printing it when pwd is called.

Hey @FilipWiacek94, the order of command handlers matters in your Main.java:

The ExternalCommandHandler is placed before CdCommandHandler. This causes cd to be picked up as an external command instead of your builtin.

On many systems, there’s an external binary with the same name as shell builtins (e.g. /usr/bin/cd). But running that won’t change your Java process’s working directory, it only affects the subprocess, which exits immediately.