|
| 1 | +import { FSMetaData, WebR } from '../../webR/webr-main'; |
| 2 | +import fs from 'fs'; |
| 3 | + |
| 4 | +const webR = new WebR({ |
| 5 | + baseUrl: '../dist/', |
| 6 | + RArgs: ['--quiet'], |
| 7 | +}); |
| 8 | + |
| 9 | +beforeAll(async () => { |
| 10 | + await webR.init(); |
| 11 | + await webR.evalRVoid('dir.create("/mnt")'); |
| 12 | +}); |
| 13 | + |
| 14 | +async function cleanupMnt() { |
| 15 | + try { |
| 16 | + await webR.FS.unmount("/mnt"); |
| 17 | + } catch (e) { |
| 18 | + const err = e as Error; |
| 19 | + if (err.message !== "FS error") throw err; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('Mount filesystem using R API', () => { |
| 24 | + test('Mount v1.0 filesystem image', async () => { |
| 25 | + await expect(webR.evalRVoid( |
| 26 | + 'webr::mount("/mnt", "tests/webR/data/test_image.data", "workerfs")' |
| 27 | + )).resolves.not.toThrow(); |
| 28 | + expect(await webR.evalRString("list.files('/mnt/abc')[2]")).toEqual("foo.csv"); |
| 29 | + expect(await webR.evalRString("readLines('/mnt/abc/bar.csv')[1]")).toEqual("a, b, c"); |
| 30 | + await cleanupMnt(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('Mount v2.0 filesystem image', async () => { |
| 34 | + await expect(webR.evalRVoid( |
| 35 | + 'webr::mount("/mnt", "tests/webR/data/test_image.tar.gz", "workerfs")' |
| 36 | + )).resolves.not.toThrow(); |
| 37 | + expect(await webR.evalRString("list.files('/mnt/abc')[2]")).toEqual("foo.csv"); |
| 38 | + expect(await webR.evalRString("readLines('/mnt/abc/bar.csv')[1]")).toEqual("a, b, c"); |
| 39 | + await cleanupMnt(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('Mount v2.0 filesystem image - no metadata hint', async () => { |
| 43 | + await expect(webR.evalRVoid( |
| 44 | + 'webr::mount("/mnt", "tests/webR/data/test_image_no_hint.tgz", "workerfs")' |
| 45 | + )).resolves.not.toThrow(); |
| 46 | + expect(await webR.evalRString("list.files('/mnt/abc')[2]")).toEqual("foo.csv"); |
| 47 | + expect(await webR.evalRString("readLines('/mnt/abc/bar.csv')[1]")).toEqual("a, b, c"); |
| 48 | + await cleanupMnt(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('Mount filesystem image from URL', async () => { |
| 52 | + const url = "https://repo.r-wasm.org/bin/emscripten/contrib/4.4/cli_3.6.3.js.metadata"; |
| 53 | + await expect(webR.evalRVoid(` |
| 54 | + webr::mount("/mnt", "${url}", "workerfs") |
| 55 | + `)).resolves.not.toThrow(); |
| 56 | + expect(await webR.evalRString("readLines('/mnt/DESCRIPTION')[1]")).toEqual("Package: cli"); |
| 57 | + await cleanupMnt(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('Mount NODEFS filesystem type', async () => { |
| 61 | + await expect(webR.evalRVoid(` |
| 62 | + webr::mount("/mnt", "tests/webR/data/testing", "nodefs") |
| 63 | + `)).resolves.not.toThrow(); |
| 64 | + expect(await webR.evalRString("readLines('/mnt/foo.csv')[2]")).toEqual("1, 2, 3"); |
| 65 | + await cleanupMnt(); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('Mount filesystem using JS API', () => { |
| 70 | + test('Mount filesystem image using Buffer', async () => { |
| 71 | + const data = fs.readFileSync("tests/webR/data/test_image.data"); |
| 72 | + const buf = fs.readFileSync("tests/webR/data/test_image.js.metadata"); |
| 73 | + const metadata = JSON.parse(new TextDecoder().decode(buf)) as FSMetaData; |
| 74 | + await expect( |
| 75 | + webR.FS.mount("WORKERFS", { packages: [{ blob: data, metadata: metadata }] }, '/mnt') |
| 76 | + ).resolves.not.toThrow(); |
| 77 | + expect(await webR.evalRString("list.files('/mnt/abc')[2]")).toEqual("foo.csv"); |
| 78 | + expect(await webR.evalRString("readLines('/mnt/abc/bar.csv')[1]")).toEqual("a, b, c"); |
| 79 | + await cleanupMnt(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('Mount filesystem image using Blob', async () => { |
| 83 | + const data = new Blob([fs.readFileSync("tests/webR/data/test_image.data")]); |
| 84 | + const buf = fs.readFileSync("tests/webR/data/test_image.js.metadata"); |
| 85 | + const metadata = JSON.parse(new TextDecoder().decode(buf)) as FSMetaData; |
| 86 | + await expect( |
| 87 | + webR.FS.mount("WORKERFS", { packages: [{ blob: data, metadata: metadata }] }, '/mnt') |
| 88 | + ).resolves.not.toThrow(); |
| 89 | + expect(await webR.evalRString("list.files('/mnt/abc')[2]")).toEqual("foo.csv"); |
| 90 | + expect(await webR.evalRString("readLines('/mnt/abc/bar.csv')[1]")).toEqual("a, b, c"); |
| 91 | + await cleanupMnt(); |
| 92 | + }); |
| 93 | + |
| 94 | + test('Mount NODEFS filesystem type', async () => { |
| 95 | + await expect( |
| 96 | + webR.FS.mount("NODEFS", { root: 'tests/webR/data/testing' }, '/mnt') |
| 97 | + ).resolves.not.toThrow(); |
| 98 | + expect(await webR.evalRString("readLines('/mnt/foo.csv')[2]")).toEqual("1, 2, 3"); |
| 99 | + await cleanupMnt(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments