Hello there!
Stuck on this step somehow. Locally, I get the expected output instantly, yet when testing with the CLI it somehow gets printed after the second test is being run (or not printed when there is only one case).
Python noob though, so I might get something wrong, but I thought that subprocess.run()
was synchronous therefore I’m kind of lost as to why in prints a new line instead of the expected output.
Here is my code :
import sys, os, subprocess
from os.path import exists
def main():
builtins = ['type', 'echo', 'exit']
while True:
sys.stdout.write("$ ")
# Wait for user input
command = input()
inputSplit = command.split()
match inputSplit:
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 path := search_in_path(arg):
print(f'{arg} is {path}/{arg}')
else:
print(f'{arg}: not found')
case _:
if search_in_path(inputSplit[0]):
print(f'Program was passed {len(inputSplit)} args (including program name).')
print(f'Arg #0 (program name): {inputSplit[0]}')
for idx, arg in enumerate(inputSplit[1:]):
print(f'Arg #{idx+1}: {arg}')
output = subprocess.run(inputSplit, shell=True, capture_output=True, universal_newlines=True, timeout=5)
print(f'{output.stdout}')
else: print(f'{command}: command not found')
def search_in_path(arg: str):
path = os.environ['PATH'].split(':')
for p in path:
if exists(p) and arg in os.listdir(p):
return p
return None
if __name__ == "__main__":
main()
And here is the output :
[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_6119
[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_6119 David
[your-program] Program was passed 2 args (including program name).
[your-program] Arg #0 (program name): custom_exe_6119
[your-program] Arg #1: David
[your-program] Program was passed 1 args (including program name).
[tester::#IP1] Output does not match expected value.
[tester::#IP1] Expected: "Program Signature: 9826198781"
[tester::#IP1] Received: "Program was passed 1 args (including program name)."
[your-program] Arg #0 (program name): custom_exe_6119
[your-program] Arg #1: <no argument provided>
[your-program] Program Signature: 9826198781
[your-program] $
[tester::#IP1] Assertion failed.
[tester::#IP1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
Thanks