Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/sandbox/mod.ts>sandboxSync

🦕 Create a temporary sandbox directory and temporary move into it
Latest
function sandboxSync
import { sandboxSync } from "https://deno.land/x/sandbox@v2.0.1/mod.ts";

Create a sandbox directory and move into it synchronously.

It creates a temporary directory and changes the current working directory to it. When disposed, it changes the current working directory back to the previous one and removes the temporary directory.

import { sandboxSync } from "@lambdalisue/sandbox";

Deno.test("Perform tests in a sandbox directory", () => {
  // Create a sandbox directory and move into it
  using sbox = sandboxSync();

  // Create a file in the sandbox directory
  using _f = Deno.createSync("foo");

  // Change permission
  Deno.chmodSync("foo", 0o700);

  // Create a link
  Deno.linkSync("foo", "bar");

  // Check lstat
  const lstat = Deno.lstatSync("foo");

  // etc...

  // The sandbox directory is removed and the current directory is restored
  // when the function is finished
});

Or call Symbol.dispose to manually dispose the sandbox directory

import { sandboxSync } from "@lambdalisue/sandbox";

Deno.test("Perform tests in a sandbox directory", () => {
  // Create a sandbox directory and move into it
  const sbox = sandboxSync();

  try {
    // Create a file in the sandbox directory
    using _f = Deno.createSync("foo");

    // Change permission
    Deno.chmodSync("foo", 0o700);

    // Create a link
    Deno.linkSync("foo", "bar");

    // Check lstat
    const lstat = Deno.lstatSync("foo");

    // etc...
  } finally {
    // The sandbox directory is removed and the current directory is restored
    // when 'Symbol.dispose' is invoked
    sbox[Symbol.dispose]();
  }
});

Returns

Sandbox & Disposable