Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign unique name to sandbox directory #171

Merged
merged 2 commits into from
Mar 5, 2024

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Mar 1, 2024

withinSandbox is designed for tests that need to act on the filesystem. It creates a temporary directory and passes it to the function so that it can do whatever it needs to do within the directory in a safer fashion.

The name of the sandbox directory is generated from the current time to ensure that each one is unique. However, this causes problems when Jest is running more than one test file, each of which make use of the sandbox. Jest runs test files in parallel, so paired with the naming — and the fact that time can be frozen in tests — it is possible for two tests to create and use the same sandbox simultaneously. withinSandbox double-checks that the directory it would have created does not already exist, so when the first test creates the directory it will cause the second test to fail. Also, since withinSandbox removes the sandbox directory after it runs its function, this will also cause the second test to fail if it's still running and using that directory.

To fix this, this commit changes withinSandbox to use a UUID to name the sandbox directory instead of the current time.


Fixes #166.

`withinSandbox` is designed for tests that need to act on the
filesystem. It creates a temporary directory and passes it to the
function so that it can do whatever it needs to do within the directory
in a safer fashion.

The name of the sandbox directory is generated from the current time to
ensure that each one is unique. However, this causes problems when Jest
is running more than one test file, each of which make use of the
sandbox. Jest runs test files in parallel, so paired with the naming —
and the fact that time can be frozen in tests — it is possible for two
tests to create and use the same sandbox simultaneously. `withinSandbox`
double-checks that the directory it would have created does not already
exist, so when the first test creates the directory it will cause the
second test to fail. Also, since `withinSandbox` removes the sandbox
directory after it runs its function, this will also cause the second
test to fail if it's still running and using that directory.

To fix this, this commit changes `withinSandbox` to use a UUID to name
the sandbox directory instead of the current time.
@mcmire mcmire requested a review from a team as a code owner March 1, 2024 23:07
Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/[email protected] None 0 6.74 kB types
npm/[email protected] None 0 123 kB ctavan

View full report↗︎

Copy link

@kanthesha kanthesha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with module-lint, worked fine.
LGTM.

@mcmire mcmire merged commit de4b119 into main Mar 5, 2024
17 checks passed
@mcmire mcmire deleted the use-unique-name-for-sandbox-directory branch March 5, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

withinSandbox may fail to create sandbox directory due to non-unique name
2 participants