tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sandboxed-fs-test-helpers.js (1413B)


      1 // This file defines a directory_test() function that can be used to define
      2 // tests that require a FileSystemDirectoryHandle. The implementation of that
      3 // function in this file will return an empty directory in the sandboxed file
      4 // system.
      5 //
      6 // Another implementation of this function exists in
      7 // file-system-access/local-fs-test-helpers.js, where that version uses the
      8 // local file system instead.
      9 
     10 function getFileSystemType() {
     11  return 'sandboxed';
     12 }
     13 
     14 async function cleanupDirectory(dir, ignoreRejections) {
     15  // Get a snapshot of the entries.
     16  const entries = await Array.fromAsync(dir.values());
     17 
     18  // Call removeEntry on all of them.
     19  const remove_entry_promises = entries.map(
     20      entry =>
     21          dir.removeEntry(entry.name, {recursive: entry.kind === 'directory'}));
     22 
     23  // Wait for them all to resolve or reject, ignoring any rejections.
     24  await Promise.allSettled(remove_entry_promises);
     25 }
     26 
     27 function directory_test(func, description) {
     28  promise_test(async t => {
     29    const dir = await navigator.storage.getDirectory();
     30 
     31    // To be extra resilient against bad tests, cleanup before every test.
     32    await cleanupDirectory(dir);
     33 
     34    // Cleanup after every test.
     35    t.add_cleanup(async () => {
     36      // Ignore any rejections since other cleanup code may have deleted them
     37      // before we could.
     38      await cleanupDirectory(dir);
     39    });
     40 
     41 
     42    await func(t, dir);
     43  }, description);
     44 }