Installations
npm install with-local-tmp-dir
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=14
Node Version
16.20.2
NPM Version
7.24.2
Score
67.6
Supply Chain
96.8
Quality
77.8
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (98.07%)
Shell (1.93%)
Developer
dword-design
Download Statistics
Total Downloads
196,782
Last Day
149
Last Week
551
Last Month
2,054
Last Year
30,188
GitHub Statistics
5 Stars
246 Commits
1 Forks
2 Watching
11 Branches
6 Contributors
Bundle Size
6.40 kB
Minified
2.43 kB
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
5.1.1
Package Id
with-local-tmp-dir@5.1.1
Unpacked Size
12.24 kB
Size
4.49 kB
File Count
5
NPM Version
7.24.2
Node Version
16.20.2
Publised On
12 Mar 2024
Total Downloads
Cumulative downloads
Total Downloads
196,782
Last day
132.8%
149
Compared to previous day
Last week
-1.3%
551
Compared to previous week
Last month
-32.1%
2,054
Compared to previous month
Last year
-55.3%
30,188
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
3
with-local-tmp-dir
Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
This package can be used to write file-based tests with real files. See below for some examples.
Install
1# npm 2$ npm install with-local-tmp-dir 3 4# Yarn 5$ yarn add with-local-tmp-dir
Basic Usage
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3 4await withLocalTmpDir(() => { 5 console.log(process.cwd()) 6 //> /Users/max/project/tmp-18815DudQxmdn03Rz 7 8 // Create some files 9 await fs.outputFile('foo.txt', 'foo bar') 10 await fs.outputFile('bar.txt', 'foo bar') 11}) 12// Now the folder does not exist anymore 13 14// The folder is removed even if an exception is thrown 15await withLocalTmpDir(() => throw new Error('File could not be found'))
Reset
Instead of a function you can reset the state yourself instead of passing a function, i.e. change to the previous folder and remove the folder:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3 4// You can still pass options 5const reset = await withLocalTmpDir() 6 7console.log(process.cwd()) 8//> /Users/max/project/tmp-18815DudQxmdn03Rz 9 10// Create some files 11await fs.outputFile('foo.txt', 'foo bar') 12await fs.outputFile('bar.txt', 'foo bar') 13 14await reset() 15 16// Now the folder does not exist anymore
Options
You can specify an options object that is passed down to tmp-promise. Check the readme for details. Some default values are adjusted, but they still can be overridden.
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3 4// do not cleanup if there are files 5await withLocalTmpDir(() => { 6 await fs.outputFile('foo.txt', 'foo bar') 7}, { unsafeCleanup: false }) 8 9// run in a subfolder 10await withLocalTmpDir(() => { 11 console.log(process.cwd()) 12 //> /Users/max/project/foo/tmp-18815DudQxmdn03Rz 13}, { dir: 'foo' }) 14 15// use a different prefix 16await withLocalTmpDir(() => { 17 console.log(process.cwd()) 18 //> /Users/max/project/foo-18815DudQxmdn03Rz 19}, { prefix: 'foo' })
Unit Tests with output-files and endent
The most common use case for this package is probably unit tests with Mocha or Jest. The package itself is framework-agnostic, so you can use any of them. To make life easier when writing multiple files with multi-line text content, there are two handy packages: output-files and endent.
The following is an example for Mocha:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const outputFiles = require('output-files') 3const endent = require('endent') 4 5const funcToTest = require('.') 6 7beforeEach(async function () { 8 this.resetWithLocalTmpDir = await withLocalTmpDir() 9}) 10 11afterEach(async function () { 12 await this.resetWithLocalTmpDir() 13}) 14 15it('works', async () => { 16 await outputFiles({ 17 'foo.txt': endent` 18 Lorem ipsum 19 dolor sit 20 `, 21 'index.js': endent` 22 export default { 23 foo: 1, 24 bar: 2, 25 } 26 `, 27 }) 28 funcToTest() 29})
Git-based tests
It is also possible to test libraries that make use of Git. You can instantiate a local Git repository inside the temporary folder and run Git operations on it:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3const execa = require('execa') 4 5beforeEach(async function () { 6 this.resetWithLocalTmpDir = await withLocalTmpDir() 7}) 8 9afterEach(async function () { 10 await this.resetWithLocalTmpDir() 11}) 12 13it('works', async () => { 14 await execa.command('git init') 15 await execa.command('git config user.email "foo@bar.de"') 16 await execa.command('git config user.name "foo"') 17 18 await outputFile('foo.txt', 'foo bar') 19 20 await execa.command('git add .') 21 await execa.command('git commit -m "feat: init"') 22 funcToTest() 23})
Be careful though, if the repository is not properly initialized, your user Git config might be overridden!
Contribute
Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️
Support
Hey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:
If you want to send me a one time donation. The coffee is pretty good 😊.
Also for one time donations if you like PayPal.
Here you can support me regularly, which is great so I can steadily work on projects.
Thanks a lot for your support! ❤️
See also
- output-files: Output a tree of files and directories by providing an object. Especially useful for testing with real files.
- expect-mocha-image-snapshot: A wrapper around jest-image-snapshot that makes it compatible to Mocha.
- jest-image-matcher: A Jest matcher for image comparisons based on pixelmatch. Can also be used with Mocha. Useful for visual regression testing.
- unify-mocha-output: Adjusts a Mocha output so that it is consistent across platforms and can be used for snapshot testing. Basically adjusts the checkmark symbol and removes time values.
- mock-argv: Temporarily overrides the command line arguments. This is useful for testing.
License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 2/24 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Warn: no topLevel permission defined: .github/workflows/deprecated-dependencies.yml:1
- Warn: no topLevel permission defined: .github/workflows/sync-labels.yml:1
- Warn: no topLevel permission defined: .github/workflows/sync-metadata.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:45: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:53: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:58: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:8: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deprecated-dependencies.yml:5: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/deprecated-dependencies.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deprecated-dependencies.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/deprecated-dependencies.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deprecated-dependencies.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/deprecated-dependencies.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deprecated-dependencies.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/deprecated-dependencies.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deprecated-dependencies.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/deprecated-dependencies.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/sync-labels.yml:5: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/sync-labels.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/sync-labels.yml:8: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/sync-labels.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/sync-metadata.yml:5: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/sync-metadata.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/sync-metadata.yml:6: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/sync-metadata.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/sync-metadata.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/dword-design/with-local-tmp-dir/sync-metadata.yml/master?enable=pin
- Warn: containerImage not pinned by hash: .gitpod.Dockerfile:2: pin your Docker image by updating gitpod/workspace-full:latest to gitpod/workspace-full:latest@sha256:bec45ebdcc9b9c5ec28d5c61c16bf599200aa0d2dc1e69e2ed8ab0a424bae6db
- Warn: downloadThenRun not pinned by hash: .gitpod.Dockerfile:4
- Info: 0 out of 8 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 9 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 containerImage dependencies pinned
- Info: 0 out of 1 downloadThenRun dependencies pinned
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Reason
20 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-hj9c-8jmm-8c52
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-x2pg-mjhr-2m5x
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-f9xv-q969-pqx4
Score
2.6
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More