Socket
Socket
Sign inDemoInstall

using-temporary-files

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

using-temporary-files

usingTemporaryFiles() - a utility for testing code that accesses the file system


Version published
Maintainers
1
Created
Source

usingTemporaryFiles()

A utility for working with tests that need to write to / read from the file system.

It creates a temporary directory, copies the specified files into it, provides some functions for working with files in the temporary directory, and then deletes the directory when the callback returns or an error occurs.

Installation

npm install --save-dev using-temporary-files

Usage

import { usingTemporaryFiles } from "using-temporary-files";

await usingTemporaryFiles(async ({ path, add, addDirectory, remove }) => {
  path("."); // full path to the temporary directory
  path("file.txt"); // full path to a particular file
  await add("file.txt", "content"); // add a file
  const text = await read("file.txt" /*, encoding (optional) */); // read the contents of a file
  await addDirectory("dir"); // add a directory
  await remove("file.txt"); // remove a file
});

Background

This code was extracted from Counterfact so that it can be used in other projects. The original function was named withTemporaryFiles(). It was renamed to usingTemporaryFiles so that it resembles the Explicit Resource Management proposal.

To understand how it's used in practice, see the tests in Counterfact

Note that until I get around to updating Counterfact to use this package, the API for withTemporaryFiles() has a few minor differences. Mainly extra arguments that we can do without.

FAQ

Accessing the file system is slow. Isn't it better to mock the file system?

Yes, it is. And that's what I do most of the time. But it's good to have a couple of end-to-end tests that exercise the real file system. This utility makes it easier to write those tests.

FAQs

Package last updated on 26 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc