Skip to content

Commit

Permalink
Write wasm bindings for public cp & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Aug 1, 2023
1 parent da91b1a commit 7f15ee6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
24 changes: 24 additions & 0 deletions wnfs-wasm/src/fs/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ impl PublicDirectory {
}))
}

/// Copies a specific node to another location.
pub fn cp(
&self,
path_segments_from: &Array,
path_segments_to: &Array,
time: &Date,
store: BlockStore,
) -> JsResult<Promise> {
let mut directory = Rc::clone(&self.0);
let store = ForeignBlockStore(store);
let time = DateTime::<Utc>::from(time);
let path_segments_from = utils::convert_path_segments(path_segments_from)?;
let path_segments_to = utils::convert_path_segments(path_segments_to)?;

Ok(future_to_promise(async move {
(&mut directory)
.cp(&path_segments_from, &path_segments_to, time, &store)
.await
.map_err(error("Cannot copy content between directories"))?;

Ok(utils::create_public_op_result(directory, JsValue::NULL)?)
}))
}

/// Creates a new directory at the specified path.
///
/// This method acts like `mkdir -p` in Unix because it creates intermediate directories if they do not exist.
Expand Down
46 changes: 46 additions & 0 deletions wnfs-wasm/tests/public.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,52 @@ test.describe("PublicDirectory", () => {
expect(imagesContent[0].name).toEqual("cats");
});

test("cp can copy content between directories", async ({ page }) => {
const [imagesContent, picturesContent] = await page.evaluate(async () => {
const {
wnfs: { PublicDirectory },
mock: { MemoryBlockStore, sampleCID },
} = await window.setup();

const time = new Date();
const store = new MemoryBlockStore();
const root = new PublicDirectory(time);

var { rootDir } = await root.write(
["pictures", "cats", "luna.jpeg"],
sampleCID,
time,
store
);

var { rootDir } = await rootDir.write(
["pictures", "cats", "tabby.png"],
sampleCID,
time,
store
);

var { rootDir } = await rootDir.mkdir(["images"], time, store);

var { rootDir } = await rootDir.cp(
["pictures", "cats"],
["images", "cats"],
time,
store
);

const imagesContent = await rootDir.ls(["images"], store);

const picturesContent = await rootDir.ls(["pictures"], store);

return [imagesContent, picturesContent];
});

expect(imagesContent.length).toEqual(1);
expect(picturesContent.length).toEqual(1);
expect(imagesContent[0].name).toEqual("cats");
});

test("A PublicDirectory has the correct metadata", async ({ page }) => {
const result = await page.evaluate(async () => {
const {
Expand Down

0 comments on commit 7f15ee6

Please sign in to comment.