Run a program #IP1 - fails (Python)

I’m stuck on Stage #IP1.

Somewhat noob here.

Here are my logs:

 [compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

Debug = true

[tester::#IP1] Running tests for Stage #IP1 (Run a program)
[tester::#IP1] [setup] export PATH=/tmp/baz:$PATH
[tester::#IP1] [setup] Available executables:
[tester::#IP1] [setup] - custom_exe_3064
[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_3064 Maria
[your-program] Program was passed 2 args (including program name).
[your-program] Arg #0 (program name): custom_exe_3064
[your-program] Arg #1: Maria
[your-program] $ 
[tester::#IP1] Output does not match expected value.
[tester::#IP1] Expected: "Program Signature: 7310207972"
[tester::#IP1] Received: "$ "
[tester::#IP1] Assertion failed.
[tester::#IP1] Test failed

View our article on debugging test failures: https://codecrafters.io/debug
(.venv) hcheema@haumaijitas-MacBook-Pro codecrafters-shell-python % codecrafters test 
Initiating test run...

Our test runners might be experiencing downtime: https://status.codecrafters.io

Running tests. Logs should appear shortly...

[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

Debug = true

[tester::#IP1] Running tests for Stage #IP1 (Run a program)
[tester::#IP1] [setup] export PATH=/tmp/qux:$PATH
[tester::#IP1] [setup] Available executables:
[tester::#IP1] [setup] - custom_exe_5882
[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_5882 Emily
[your-program] Program was passed 2 args (including program name).
[your-program] Arg #0 (program name): custom_exe_5882
[your-program] Arg #1: Emily$ 
[tester::#IP1] Output does not match expected value.
[tester::#IP1] Expected: "Arg #1: Emily"
[tester::#IP1] Received: "Arg #1: Emily$ "
[tester::#IP1] Assertion failed.
[tester::#IP1] Test failed

And here’s a snippet of my code:

import sys
import os 
from pathlib import Path 

def main():
    path = os.getenv("PATH")
    builtins = ["echo", "exit", "type"]

    while True:
        sys.stdout.write("$ ")
        
    # Wait for user input
        inp = input()
        inpsplit = inp.split()

        match inpsplit:
            case ["echo", *args]:
                print(*args)
            case ["exit", "0"]:
                sys.exit(0)
            case ["type", arg]:
                if arg in builtins:
                    print(f"{arg} is a shell builtin")
                elif res := search_in_dir(arg):
                    print(f"{arg} is {res}")
                else:
                    print(f"{arg}: not found")
            case _:
                if search_in_dir(inpsplit[0]):
                    print(f"Program was passed {len(inpsplit)} args (including program name).")
                    print(f"Arg #0 (program name): {inpsplit[0]}")
                    
                    for i, cm in enumerate(inpsplit[1:], start=1):
                        # print(f"Arg #{i}: {cm}")
                        sys.stdout.write(f"Arg #{i}: {cm}")
                else: 
                    print(f"{inp}: command not found")

def search_in_dir(commandName):
    dirs = os.environ["PATH"].split(":") 
    
    for d in dirs:
        path = Path(d) / commandName

        if path.exists():   
            return str(path)
    
    return None   
      
if __name__ == "__main__":
    main()

Hey @gnidocnuf, you’ll need to actually run the executable instead of hardcoding the output:

You can use subprocess.run to execute the command and capture its output.

Let me know if you need further clarification!

This helped. Thank you :folded_hands:

1 Like

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