#WH6 looks correct locally but fails tests


in the screenshot, on each line i press tab, it looks correct but the test results fail?:

[compile] Moved ./.codecrafters/run.sh β†’ ./your_program.sh

[compile] Compilation successful.

[tester::#WH6] Running tests for Stage #WH6 (Autocompletion - Multiple completions)

[tester::#WH6] [setup] export PATH=/tmp/ant:$PATH

[tester::#WH6] [setup] export PATH=/tmp/bee:$PATH

[tester::#WH6] [setup] export PATH=/tmp/fox:$PATH

[tester::#WH6] [setup] Available executables:

[tester::#WH6] [setup] - xyz_dog

[tester::#WH6] [setup] - xyz_fox

[tester::#WH6] [setup] - xyz_cow

[tester::#WH6] Running ./your_program.sh

[tester::#WH6] βœ“ Received prompt ($ )

[tester::#WH6] Typed "xyz_"

[your-program] $ xyz_

[tester::#WH6] βœ“ Prompt line matches "$ xyz_"

[tester::#WH6] Pressed "<TAB>" (expecting bell to ring)

[tester::#WH6] βœ“ Received bell

[tester::#WH6] Pressed "<TAB>" (expecting autocomplete to "xyz_cow xyz_dog xyz_fox")

[tester::#WH6] Didn't find expected line.

[tester::#WH6] Expected: "xyz_cow xyz_dog xyz_fox"

[tester::#WH6] Received: "" (no line received)

[tester::#WH6] Assertion failed.

[tester::#WH6] Test failed

snippet:

TAB_COUNT = 0
LAST_BUFFER = ""
CACHED_MATCHES = []

def completer(text: str, state: int):
    global TAB_COUNT, LAST_BUFFER, CACHED_MATCHES

    if state != 0: return None

    buffer = readline.get_line_buffer()

    if readline.get_begidx() != 0: return None

    if buffer != LAST_BUFFER:
        TAB_COUNT = 0
        LAST_BUFFER = buffer

    matches = []

    for cmd in commands.BUILTINS.keys():
        if cmd.startswith(text):
            matches.append(cmd)

    for _, files in commands.get_path_files().items():
        for file in files:
            if file.startswith(text):
                matches.append(file)

    matches = sorted(set(matches))
    CACHED_MATCHES = matches

    if not matches:
        return None

    if len(matches) == 1:
        TAB_COUNT = 0
        return matches[0] + " "

    TAB_COUNT += 1

    if TAB_COUNT == 1:
        sys.stdout.write("\x07")
        sys.stdout.flush()
        return None

    if TAB_COUNT == 2:
        sys.stdout.write("\n")
        sys.stdout.write("  ".join(matches) + "\n")
        sys.stdout.write("$ " + buffer)
        sys.stdout.flush()
        TAB_COUNT = 0
        return None

    return None

Hey @Zeviraty, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Sorry i forgot to share the link, here it is: GitHub - Zeviraty/Zhell: Codecrafters course for creating a shell

when i switch over the python version i use when running the program changes the readline from libedit β†’ gnu, and then i get the same result as in the tests

But i cant figure out how to make them act the same….?

Fixed it by having the logic in the completer function itself, and also setting the bind to: `tab: complete`

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.