Cannot get past stage #NI6

I’m stuck on Stage #NI6

I’ve tried:
I completed what I thought was my implementation for this stage but I feel stuck currently between two tests. When I fix for one, the other fails and vice versa.

Here are my logs:
1st Error I run into:

remote: [tester::#NI6] Writing file "/tmp/foo/f   19" with content "strawberry grape."
remote: [tester::#NI6] Writing file "/tmp/foo/f   62" with content "pineapple strawberry."
remote: [tester::#NI6] Writing file "/tmp/foo/f   87" with content "apple pineapple."
remote: [your-program] $ cat '/tmp/foo/f   19' '/tmp/foo/f   62' '/tmp/foo/f   87'
remote: [your-program] strawberry grape.pineapple strawberry.apple pineapple.
remote: [tester::#NI6] Expected prompt ("$ ") but received ""
remote: [your-program] $
remote: [tester::#NI6] Assertion failed.
remote: [tester::#NI6] Test failed

At first I took this as "I should be putting a ‘$’ infront of the cat commands output but when I go ahead and do that I get the following failed test:

remote: [tester::#NI6] Writing file "/tmp/baz/f   30" with content "strawberry mango."
remote: [tester::#NI6] Writing file "/tmp/baz/f   65" with content "orange apple."
remote: [tester::#NI6] Writing file "/tmp/baz/f   13" with content "orange strawberry."
remote: [your-program] $ cat '/tmp/baz/f   30' '/tmp/baz/f   65' '/tmp/baz/f   13'
remote: [your-program] $ strawberry mango.orange apple.orange strawberry.
remote: [tester::#NI6] Output does not match expected value.
remote: [tester::#NI6] Expected: "strawberry mango.orange apple.orange strawberry."
remote: [tester::#NI6] Received: "$ strawberry mango.orange apple.orange strawberry."
remote: [your-program] $
remote: [your-program] $
remote: [tester::#NI6] Assertion failed.
remote: [tester::#NI6] Test failed

This tells me we should not have the ‘$’ for the output.

At this point I am unsure what to do. I thought maybe the first test failing is refering to the line after the command is run but when I test locally I still see that this works as I would expect,
e.g.

$ cat 1.bat
apple blueberry.
$ 

I feel like at this point I am really just missing one of the requirements and its flying over my head. Any help or guidence would be super appreciated! TIA

And here’s a snippet of my code:

while (true) {
            print("$ ")
            val input = readlnOrNull()?.trim() //
            if (input.isNullOrBlank()) continue

            val commandNameAndArgs = input.split(" ", limit=2)
            val commandName = commandNameAndArgs.first()
            val argumentsString = commandNameAndArgs.getOrNull(1)
            val formattedArguments = formatArguments(argumentsString)
            val arguments = if (commandName == "echo") listOf(formattedArguments?.raw ?: "")
                            else formattedArguments?.normalized ?: emptyList()

            when(val result = handler.executeCommand(commandName, arguments)) {
                is CommandResult.Success -> {
                    result.message?.let { message ->
                        if (commandName != "echo" && commandName != "cat") {
                            message.lines().forEach { println("$ $it") }
                        } else {
                            println(message)
                        }
                    }
                }

                is CommandResult.Failure -> {
                    result.message?.let { message ->
                        if (commandName != "echo" && commandName != "cat") {
                            message.lines().forEach { println("$ $it") }
                        } else {
                            println(message)
                        }
                    }
                }

                is CommandResult.Terminate -> {
                    if (result.code == 0) { kotlin.system.exitProcess(0) } else return
                }

            }

        }

I’ll take a look shortly.

1 Like

Hi @platypus-191, I tried running your code against the previous stages, but it’s actually no longer passing the second stage #CZ2 (Handle invalid commands) due to an extra "$ ".

After I fixed the issue above, it’s still not passing #MG5 (The type builtin: executable files):

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

Thank you Andy1li this was very helpful

I fixed my previous tests but now am back to:

[tester::#NI6] ✓ Received expected response
[tester::#NI6] Writing file "/tmp/foo/f   47" with content "mango apple."
[tester::#NI6] Writing file "/tmp/foo/f   24" with content "pear banana."
[tester::#NI6] Writing file "/tmp/foo/f   16" with content "apple strawberry."
[your-program] $ cat '/tmp/foo/f   47' '/tmp/foo/f   24' '/tmp/foo/f   16'
[your-program] mango apple.pear banana.apple strawberry.
[tester::#NI6] Expected prompt ("$ ") but received ""
[your-program] $ 
[tester::#NI6] Assertion failed.
[tester::#NI6] Test failed

Would you be able to help me better understand where I should be placing the ?? TIA

I was able to get the tests to pass by using

when(val result = handler.executeCommand(commandName, arguments)) {
                is CommandResult.Success -> {
                    result.message?.let { message ->
                        print(message.trimEnd() + '\n')
                    }
                }

                is CommandResult.Failure -> {
                    result.message?.let { message ->
                        print(message.trimEnd() + '\n')
                    }
                }

                is CommandResult.Terminate -> {
                    if (result.code == 0) { kotlin.system.exitProcess(0) } else return
                }

            }
print(message.trimEnd() + '\n')

I am a little confused though why this works as opposed to simply just using

println(message)

The outputs from the cat command do not contain newlines in their output so to me I would expect them to behave the same in terms of newlines.