TypeScript test #MG5 error, can't figure out

import { createInterface } from "readline";
import { existsSync } from "fs";

const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: '$ '
});


const commands = ['echo', 'exit', 'type'];
const paths = process.env["PATH"]?.split(":") || [];

rl.prompt();

const type = (input: string) => {
  if (commands.includes(input)) {
    console.log(`${input} is a shell builtin`);
  } else {
    for (const p of paths) {
      const filePath = `${p}/${input}`
      if (existsSync(filePath)) {
        console.log(`${input} is ${filePath}`);
        return;
      }
    }
    console.log(`${input}: not found`);
  }
}

const main = () => {
  rl.on('line', (command) => {
    if (command.trim() === 'exit 0') {
      process.exit(0);
    }
    if (command.startsWith("echo ")) {
      console.log(command.substring(5))
    } else if (command.startsWith("type ")) {
      type(command.substring(5));
    } else {
      console.log(`${command}: command not found`)
    }
    rl.prompt();
  })
}

main();

remote: [your-program] $ type my_exe
remote: [your-program] my_exe is /tmp/bar/my_exe
remote: [tester::#MG5] ^ Line does not match expected value.
remote: [tester::#MG5] Expected: “my_exe is /tmp/baz/my_exe”
remote: [tester::#MG5] Received: “my_exe is /tmp/bar/my_exe”
remote: [your-program] $
remote: [tester::#MG5] Assertion failed.
remote: [tester::#MG5] Test failed

and When I reverse the array
error is
remote: [your-program] my_exe is /tmp/qux/my_exe
remote: [tester::#MG5] ^ Line does not match expected value.
remote: [tester::#MG5] Expected: “my_exe is /tmp/baz/my_exe”
remote: [tester::#MG5] Received: “my_exe is /tmp/qux/my_exe”
remote: [your-program] $
remote: [tester::#MG5] Assertion failed.
remote: [tester::#MG5] Test failed

Hey @Sunsettings , could you double-check if your code implements the following logic?

Note that there are multiple files with the same name in different locations, but not all of them are executable. Only the first executable should be picked up.

Thanks, The test passed after I added the following code.
Clipboard_Screenshot_1762937445

1 Like

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