Node.js traps · Chapter 11 of 16

exec runs a shell

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Modules
  • 3 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.js: print a title wrong on purpose

    Write reproduce() in repro.js. Print a label with printLabel from label-printer.js for the ticket's exact title, Datta's "Pick" $(echo of the week) (an apostrophe, two double quotes, and a $( ) with an echo in it), and resolve to the label it printed. Write the title in a template literal so both kinds of quote fit.

    The check runs your reproduce() twice: beside the original label-printer.js it must resolve to Datta's Pick of the week (the shell removed the quotes and ran the echo), and beside a fixed label-printer.js to the title exactly as written. No real waiting: reproduce() must finish within 5 seconds (starting the printer program takes a fraction of that).

  2. Task 2

    label-printer.js: every title printed as it was written

    Fix printLabel(title) in label-printer.js so the title reaches the printer program, print-label.js, exactly as it was written, whatever characters it has, and no part of it ever runs as a command: semicolons, apostrophes, double quotes, $( ), backticks, &&, a dollar sign, a pipe and line breaks are all printed as they are.

    Keep the log line labels: printed <label> and the return value: a Promise of the label the program printed (its stdout without the last newline). The check prints its own ten titles, one of them with an apostrophe, two of them built to create a file if any part of them ran, and checks that no file appears.

  3. Task 3

    label-printer.test.js: a test that would have caught it

    Write a vitest test file, label-printer.test.js, that would have caught the wrong labels before they printed. The check runs your file three times: beside the original label-printer.js, where at least one test must fail; beside the fixed label-printer.js, where every test must pass; and beside a wrong fix that wraps the title in single quotes inside the same command string, where at least one test must fail.

    So print titles with characters a shell treats as its own and expect each label to be the title exactly: double quotes and a $( ) (the original changes them), and an apostrophe (the single-quote fix fails on it). Each test must finish within 10 seconds (each label starts a program).

What you will learn

  • repro.js: print a title wrong on purpose
  • label-printer.js: every title printed as it was written
  • label-printer.test.js: a test that would have caught it

More in Modules

Build "exec runs a shell" yourself

Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.