While implementing my own version of ‘ls’, can’t get file edition times consistent with order in which setup file-editing functions are called, using os.path.getmtime() builtin.
My sorting code:
call = command.strip().split(' ')
file_list = os.listdir(call[-1])
file_list = sorted([f for f in file_list], key = lambda x: os.path.getmtime(call[-1] + chr(47) + x))
What i’ve tried:
using similar getatime() and getctime() functions
While trying different ideas, my code managed to pass this particular test by luck, as it sholud in 1/6th of the attempts (test modifies 3 files) - this makes me confident that the problem lies in retrieving file modification time.
Example log with printed file list after sorting:
remote: [tester::#JV1] Running tests for Stage #JV1 (Redirection - Redirect stdout)
remote: [tester::#JV1] [setup] export PATH=/tmp/orange/banana/orange:$PATH
remote: [tester::#JV1] Running ./your_program.sh
remote: [tester::#JV1] [setup] echo -n "mango" > "/tmp/quz/mango"
remote: [tester::#JV1] [setup] echo -n "orange" > "/tmp/quz/orange"
remote: [tester::#JV1] [setup] echo -n "strawberry" > "/tmp/quz/strawberry"
remote: [your-program] $ ls -1 /tmp/quz > /tmp/baz/bar.md
remote: [your-program] file_list: ['orange', 'strawberry', 'mango']
remote: [tester::#JV1] Expected prompt ("$ ") but received "file_list: ['orange', 'strawberry', 'mango']"
remote: [your-program] $
remote: [tester::#JV1] Assertion failed.
remote: [tester::#JV1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
remote: [your-program] $ ls -1 /tmp/foo > /tmp/qux/bar.md
remote: [your-program] flag provided but not defined: -1 /tmp/foo
remote: [tester::#JV1] Expected prompt ("$ ") but received "flag provided but not defined: -1 /tmp/foo"
remote: [your-program] Usage of ls:
remote: [your-program] -1 list one file per line
remote: [your-program] panic: ls: invalid option: flag provided but not defined: -1 /tmp/foo
remote: [your-program] goroutine 1 [running]:
remote: [your-program] main.main()
remote: [your-program] /Users/ryang/Developer/work/course-testers/shell-tester/internal/custom_executable/ls/ls.go:19 +0x479
remote: [your-program] $
remote: [tester::#JV1] Assertion failed.
error while just using subprocess.run() - i assume this comes from my machine running on Windows and Powershell but i’m not experienced enough in this topic, so just reimplementing the function seemed like an easier idea. (If You have any idea how to fix this bug, i’d be extremely happy to go back to this solution:grinning_face_with_smiling_eyes:)
The modification time is needed to recover the order in which listed filenames are sorted, as os.listdir() returns random ordering, which causes fail at the next step:
remote: [tester::#JV1] Running tests for Stage #JV1 (Redirection - Redirect stdout)
remote: [tester::#JV1] [setup] export PATH=/tmp/blueberry/mango/grape:$PATH
remote: [tester::#JV1] Running ./your_program.sh
remote: [tester::#JV1] [setup] echo -n "blueberry" > "/tmp/baz/blueberry"
remote: [tester::#JV1] [setup] echo -n "pear" > "/tmp/baz/pear"
remote: [tester::#JV1] [setup] echo -n "strawberry" > "/tmp/baz/strawberry"
remote: [your-program] $ ls -1 /tmp/baz > /tmp/foo/baz.md
remote: [your-program] $ cat /tmp/foo/baz.md
remote: [your-program] strawberry
remote: [tester::#JV1] Output does not match expected value.
remote: [tester::#JV1] Expected: "blueberry"
remote: [tester::#JV1] Received: "strawberry"
remote: [your-program] pear
remote: [your-program] blueberry
remote: [your-program] $
remote: [tester::#JV1] Assertion failed.
remote: [tester::#JV1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)