DEV Community

Nic Flemmer
Nic Flemmer

Posted on

Node doesn't expand ~, and my tests wrote a folder called ~

The demo data in my app has project paths like ~/code/acme-shop. They're fixtures: meant to look like a real person's machine.

Then a test started a run and wrote project settings, and a directory named ~ appeared inside the package, as apps/desktop/~/code/..., in the repo.

The reason is that ~ is a shell feature. Your shell turns it into your home directory before a program sees it. Node's fs doesn't. Hand it ~/code/acme-shop and it treats ~ as an ordinary folder name, relative to the current directory, and creates it.

What I do now:

  • Any test that starts a run or writes project settings points the project at a temp directory first. I wrote it into the project's contributor notes, because the failure shows up as a stray folder and not as a red test.
  • Paths that are stored or passed between processes are absolute. If a path can come from a human, I expand ~ once, at the edge, with os.homedir().
  • git status after a test run is a cheap check. A directory called ~ is hard to miss once you look.

It doesn't apply if all your paths are built with path.join(os.homedir(), ...) from the start. Mine weren't, because fixtures are written for people to read.

Top comments (0)