2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
|
|
|
|
import * as path from "@std/path";
|
2024-07-25 05:53:22 +00:00
|
|
|
import { move, moveSync } from "./move.ts";
|
2019-03-12 09:11:30 +00:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
2022-08-24 04:28:54 +00:00
|
|
|
import { existsSync } from "./exists.ts";
|
2019-03-12 09:11:30 +00:00
|
|
|
|
2020-09-08 09:43:43 +00:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata");
|
2019-03-12 09:11:30 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() rejects if src dir does not exist", async function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_1");
|
|
|
|
const destDir = path.join(testdataDir, "move_test_dest_1");
|
|
|
|
// if src directory not exist
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-04-24 11:41:23 +00:00
|
|
|
await move(srcDir, destDir);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-03-12 09:11:30 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() creates dest dir if it does not exist", async function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_2");
|
|
|
|
const destDir = path.join(testdataDir, "move_test_dest_2");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
await Deno.mkdir(srcDir, { recursive: true });
|
2019-03-12 09:11:30 +00:00
|
|
|
|
|
|
|
// if dest directory not exist
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-03-12 09:11:30 +00:00
|
|
|
await move(srcDir, destDir);
|
|
|
|
throw new Error("should not throw error");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"should not throw error",
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(destDir);
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test(
|
2023-11-23 06:59:59 +00:00
|
|
|
"move() creates dest dir if it does not exist and overwrite option is set to true",
|
2021-04-05 11:49:05 +00:00
|
|
|
async function () {
|
2020-04-28 10:33:09 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_2");
|
|
|
|
const destDir = path.join(testdataDir, "move_test_dest_2");
|
|
|
|
|
|
|
|
await Deno.mkdir(srcDir, { recursive: true });
|
|
|
|
|
|
|
|
// if dest directory not exist
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2020-04-28 10:33:09 +00:00
|
|
|
await move(srcDir, destDir, { overwrite: true });
|
|
|
|
throw new Error("should not throw error");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"should not throw error",
|
2020-04-28 10:33:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(destDir);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2020-04-28 10:33:09 +00:00
|
|
|
);
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() rejects if src file does not exist", async function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcFile = path.join(testdataDir, "move_test_src_3", "test.txt");
|
|
|
|
const destFile = path.join(testdataDir, "move_test_dest_3", "test.txt");
|
|
|
|
|
|
|
|
// if src directory not exist
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-04-24 11:41:23 +00:00
|
|
|
await move(srcFile, destFile);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-03-12 09:11:30 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() moves file and can overwrite content", async function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_4");
|
|
|
|
const destDir = path.join(testdataDir, "move_test_dest_4");
|
|
|
|
const srcFile = path.join(srcDir, "test.txt");
|
|
|
|
const destFile = path.join(destDir, "test.txt");
|
|
|
|
const srcContent = new TextEncoder().encode("src");
|
|
|
|
const destContent = new TextEncoder().encode("dest");
|
|
|
|
|
|
|
|
// make sure files exists
|
|
|
|
await Promise.all([ensureFile(srcFile), ensureFile(destFile)]);
|
|
|
|
|
|
|
|
// write file content
|
|
|
|
await Promise.all([
|
|
|
|
Deno.writeFile(srcFile, srcContent),
|
2020-03-28 17:03:49 +00:00
|
|
|
Deno.writeFile(destFile, destContent),
|
2019-03-12 09:11:30 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// make sure the test file have been created
|
2023-11-03 04:41:18 +00:00
|
|
|
assertEquals(await Deno.readTextFile(srcFile), "src");
|
|
|
|
assertEquals(await Deno.readTextFile(destFile), "dest");
|
2019-03-12 09:11:30 +00:00
|
|
|
|
|
|
|
// move it without override
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-03-12 09:11:30 +00:00
|
|
|
await move(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"dest already exists",
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// move again with overwrite
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-03-12 09:11:30 +00:00
|
|
|
await move(srcFile, destFile, { overwrite: true });
|
|
|
|
throw new Error("should not throw error");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"should not throw error",
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
|
2022-08-24 04:28:54 +00:00
|
|
|
await assertRejects(async () => await Deno.lstat(srcFile));
|
2023-11-03 04:41:18 +00:00
|
|
|
assertEquals(await Deno.readTextFile(destFile), "src");
|
2019-03-12 09:11:30 +00:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
await Promise.all([
|
|
|
|
Deno.remove(srcDir, { recursive: true }),
|
2020-03-28 17:03:49 +00:00
|
|
|
Deno.remove(destDir, { recursive: true }),
|
2019-03-12 09:11:30 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() moves dir", async function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_5");
|
|
|
|
const destDir = path.join(testdataDir, "move_test_dest_5");
|
|
|
|
const srcFile = path.join(srcDir, "test.txt");
|
|
|
|
const destFile = path.join(destDir, "test.txt");
|
|
|
|
const srcContent = new TextEncoder().encode("src");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
await Deno.mkdir(srcDir, { recursive: true });
|
2022-08-24 04:28:54 +00:00
|
|
|
assert(await Deno.lstat(srcDir));
|
2019-03-12 09:11:30 +00:00
|
|
|
await Deno.writeFile(srcFile, srcContent);
|
|
|
|
|
|
|
|
await move(srcDir, destDir);
|
|
|
|
|
2022-08-24 04:28:54 +00:00
|
|
|
await assertRejects(async () => await Deno.lstat(srcDir));
|
|
|
|
assert(await Deno.lstat(destDir));
|
|
|
|
assert(await Deno.lstat(destFile));
|
2019-03-12 09:11:30 +00:00
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
const destFileContent = await Deno.readTextFile(destFile);
|
2019-03-12 09:11:30 +00:00
|
|
|
assertEquals(destFileContent, "src");
|
|
|
|
|
|
|
|
await Deno.remove(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test(
|
2023-11-23 06:59:59 +00:00
|
|
|
"move() moves files if src and dest exist and can overwrite content",
|
2021-04-05 11:49:05 +00:00
|
|
|
async function () {
|
2020-04-28 10:33:09 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_6");
|
|
|
|
const destDir = path.join(testdataDir, "move_test_dest_6");
|
|
|
|
const srcFile = path.join(srcDir, "test.txt");
|
|
|
|
const destFile = path.join(destDir, "test.txt");
|
|
|
|
const srcContent = new TextEncoder().encode("src");
|
|
|
|
const destContent = new TextEncoder().encode("dest");
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
Deno.mkdir(srcDir, { recursive: true }),
|
|
|
|
Deno.mkdir(destDir, { recursive: true }),
|
|
|
|
]);
|
2022-08-24 04:28:54 +00:00
|
|
|
assert(await Deno.lstat(srcDir));
|
|
|
|
assert(await Deno.lstat(destDir));
|
2020-04-28 10:33:09 +00:00
|
|
|
await Promise.all([
|
|
|
|
Deno.writeFile(srcFile, srcContent),
|
|
|
|
Deno.writeFile(destFile, destContent),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await move(srcDir, destDir, { overwrite: true });
|
|
|
|
|
2022-08-24 04:28:54 +00:00
|
|
|
await assertRejects(async () => await Deno.lstat(srcDir));
|
|
|
|
assert(await Deno.lstat(destDir));
|
|
|
|
assert(await Deno.lstat(destFile));
|
2020-04-28 10:33:09 +00:00
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
const destFileContent = await Deno.readTextFile(destFile);
|
2020-04-28 10:33:09 +00:00
|
|
|
assertEquals(destFileContent, "src");
|
|
|
|
|
|
|
|
await Deno.remove(destDir, { recursive: true });
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2020-04-28 10:33:09 +00:00
|
|
|
);
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() rejects when dest is its own sub dir", async function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_test_src_7");
|
|
|
|
const destDir = path.join(srcDir, "nest");
|
|
|
|
|
|
|
|
await ensureDir(destDir);
|
|
|
|
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-03-12 09:11:30 +00:00
|
|
|
await move(srcDir, destDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
`Cannot move '${srcDir}' to a subdirectory of itself, '${destDir}'.`,
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
await Deno.remove(srcDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() throws if src dir does not exist", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_1");
|
|
|
|
const destDir = path.join(testdataDir, "move_sync_test_dest_1");
|
|
|
|
// if src directory not exist
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
moveSync(srcDir, destDir);
|
|
|
|
});
|
2019-03-12 09:11:30 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() creates dest dir if it does not exist", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_2");
|
|
|
|
const destDir = path.join(testdataDir, "move_sync_test_dest_2");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(srcDir, { recursive: true });
|
2019-03-12 09:11:30 +00:00
|
|
|
|
|
|
|
// if dest directory not exist
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-03-12 09:11:30 +00:00
|
|
|
moveSync(srcDir, destDir);
|
|
|
|
throw new Error("should not throw error");
|
2020-04-26 20:27:24 +00:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"should not throw error",
|
2020-04-26 20:27:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(destDir);
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() creates dest dir if it does not exist and overwrite option is set to true", function () {
|
2020-04-26 20:27:24 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_2");
|
|
|
|
const destDir = path.join(testdataDir, "move_sync_test_dest_2");
|
|
|
|
|
|
|
|
Deno.mkdirSync(srcDir, { recursive: true });
|
|
|
|
|
|
|
|
// if dest directory not exist width overwrite
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2020-04-26 20:27:24 +00:00
|
|
|
moveSync(srcDir, destDir, { overwrite: true });
|
|
|
|
throw new Error("should not throw error");
|
2019-03-12 09:11:30 +00:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"should not throw error",
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(destDir);
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() throws if src file does not exist", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcFile = path.join(testdataDir, "move_sync_test_src_3", "test.txt");
|
|
|
|
const destFile = path.join(testdataDir, "move_sync_test_dest_3", "test.txt");
|
|
|
|
|
|
|
|
// if src directory not exist
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
moveSync(srcFile, destFile);
|
|
|
|
});
|
2019-03-12 09:11:30 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() moves file and can overwrite content", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_4");
|
|
|
|
const destDir = path.join(testdataDir, "move_sync_test_dest_4");
|
|
|
|
const srcFile = path.join(srcDir, "test.txt");
|
|
|
|
const destFile = path.join(destDir, "test.txt");
|
|
|
|
const srcContent = new TextEncoder().encode("src");
|
|
|
|
const destContent = new TextEncoder().encode("dest");
|
|
|
|
|
|
|
|
// make sure files exists
|
|
|
|
ensureFileSync(srcFile);
|
|
|
|
ensureFileSync(destFile);
|
|
|
|
|
|
|
|
// write file content
|
|
|
|
Deno.writeFileSync(srcFile, srcContent);
|
|
|
|
Deno.writeFileSync(destFile, destContent);
|
|
|
|
|
|
|
|
// make sure the test file have been created
|
|
|
|
assertEquals(new TextDecoder().decode(Deno.readFileSync(srcFile)), "src");
|
|
|
|
assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "dest");
|
|
|
|
|
|
|
|
// move it without override
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-03-12 09:11:30 +00:00
|
|
|
moveSync(srcFile, destFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"dest already exists",
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// move again with overwrite
|
2019-03-19 17:22:33 +00:00
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-03-12 09:11:30 +00:00
|
|
|
moveSync(srcFile, destFile, { overwrite: true });
|
|
|
|
throw new Error("should not throw error");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
"should not throw error",
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(existsSync(srcFile), false);
|
|
|
|
assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "src");
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
Deno.removeSync(srcDir, { recursive: true });
|
|
|
|
Deno.removeSync(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() moves dir", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_5");
|
|
|
|
const destDir = path.join(testdataDir, "move_sync_test_dest_5");
|
|
|
|
const srcFile = path.join(srcDir, "test.txt");
|
|
|
|
const destFile = path.join(destDir, "test.txt");
|
|
|
|
const srcContent = new TextEncoder().encode("src");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(srcDir, { recursive: true });
|
2019-03-12 09:11:30 +00:00
|
|
|
assertEquals(existsSync(srcDir), true);
|
|
|
|
Deno.writeFileSync(srcFile, srcContent);
|
|
|
|
|
|
|
|
moveSync(srcDir, destDir);
|
|
|
|
|
|
|
|
assertEquals(existsSync(srcDir), false);
|
|
|
|
assertEquals(existsSync(destDir), true);
|
|
|
|
assertEquals(existsSync(destFile), true);
|
|
|
|
|
|
|
|
const destFileContent = new TextDecoder().decode(Deno.readFileSync(destFile));
|
|
|
|
assertEquals(destFileContent, "src");
|
|
|
|
|
|
|
|
Deno.removeSync(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() moves files if src and dest exist and can overwrite content", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_6");
|
|
|
|
const destDir = path.join(testdataDir, "move_sync_test_dest_6");
|
|
|
|
const srcFile = path.join(srcDir, "test.txt");
|
|
|
|
const destFile = path.join(destDir, "test.txt");
|
|
|
|
const srcContent = new TextEncoder().encode("src");
|
|
|
|
const destContent = new TextEncoder().encode("dest");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(srcDir, { recursive: true });
|
|
|
|
Deno.mkdirSync(destDir, { recursive: true });
|
2019-03-12 09:11:30 +00:00
|
|
|
assertEquals(existsSync(srcDir), true);
|
|
|
|
assertEquals(existsSync(destDir), true);
|
|
|
|
Deno.writeFileSync(srcFile, srcContent);
|
|
|
|
Deno.writeFileSync(destFile, destContent);
|
|
|
|
|
|
|
|
moveSync(srcDir, destDir, { overwrite: true });
|
|
|
|
|
|
|
|
assertEquals(existsSync(srcDir), false);
|
|
|
|
assertEquals(existsSync(destDir), true);
|
|
|
|
assertEquals(existsSync(destFile), true);
|
|
|
|
|
|
|
|
const destFileContent = new TextDecoder().decode(Deno.readFileSync(destFile));
|
|
|
|
assertEquals(destFileContent, "src");
|
|
|
|
|
|
|
|
Deno.removeSync(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() throws when dest is its own sub dir", function () {
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcDir = path.join(testdataDir, "move_sync_test_src_7");
|
|
|
|
const destDir = path.join(srcDir, "nest");
|
|
|
|
|
|
|
|
ensureDirSync(destDir);
|
|
|
|
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2023-04-05 11:19:24 +00:00
|
|
|
moveSync(srcDir, destDir, { overwrite: true });
|
2019-03-12 09:11:30 +00:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
`Cannot move '${srcDir}' to a subdirectory of itself, '${destDir}'.`,
|
2019-03-12 09:11:30 +00:00
|
|
|
);
|
|
|
|
Deno.removeSync(srcDir, { recursive: true });
|
|
|
|
});
|
2023-04-05 11:19:24 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() accepts overwrite option set to true for file content", async function () {
|
2023-04-05 11:19:24 +00:00
|
|
|
const dir = path.join(testdataDir, "move_same_file_1");
|
|
|
|
const file = path.join(dir, "test.txt");
|
|
|
|
const url = path.toFileUrl(file);
|
|
|
|
const content = new TextEncoder().encode("test");
|
|
|
|
|
|
|
|
// Make sure test file exists
|
|
|
|
await ensureFile(file);
|
|
|
|
await Deno.writeFile(file, content);
|
|
|
|
assert(await Deno.lstat(dir));
|
|
|
|
|
|
|
|
// Test varying pairs of `string` and `URL` params.
|
|
|
|
const pairs = [
|
|
|
|
[file, file],
|
|
|
|
[file, url],
|
|
|
|
[url, file],
|
|
|
|
[url, url],
|
2024-02-07 00:08:47 +00:00
|
|
|
] as const;
|
2023-04-05 11:19:24 +00:00
|
|
|
|
|
|
|
for (const p of pairs) {
|
|
|
|
const src = p[0];
|
|
|
|
const dest = p[1];
|
|
|
|
|
|
|
|
await move(src, dest, { overwrite: true });
|
|
|
|
assertEquals(await Deno.readTextFile(src), "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
await Deno.remove(dir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() accepts overwrite option set to true for directories", async function () {
|
2023-04-05 11:19:24 +00:00
|
|
|
const dir = path.join(testdataDir, "move_same_dir_1");
|
|
|
|
const url = path.toFileUrl(dir);
|
|
|
|
|
|
|
|
// Make sure test dir exists
|
|
|
|
await ensureDir(dir);
|
|
|
|
assert(await Deno.lstat(dir));
|
|
|
|
|
|
|
|
// Test varying pairs of `string` and `URL params.
|
|
|
|
const pairs = [
|
|
|
|
[dir, dir],
|
|
|
|
[dir, url],
|
|
|
|
[url, dir],
|
|
|
|
[url, url],
|
2024-02-07 00:08:47 +00:00
|
|
|
] as const;
|
2023-04-05 11:19:24 +00:00
|
|
|
|
|
|
|
for (const p of pairs) {
|
|
|
|
const src = p[0];
|
|
|
|
const dest = p[1];
|
|
|
|
|
2024-07-25 05:53:22 +00:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await move(src, dest);
|
|
|
|
},
|
|
|
|
Deno.errors.NotSupported,
|
|
|
|
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
|
|
|
|
);
|
2023-04-05 11:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await Deno.remove(dir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("moveSync() accepts overwrite option set to true for file content", function () {
|
2023-04-05 11:19:24 +00:00
|
|
|
const dir = path.join(testdataDir, "move_sync_same_file_1");
|
|
|
|
const file = path.join(dir, "test.txt");
|
|
|
|
const url = path.toFileUrl(file);
|
|
|
|
const content = new TextEncoder().encode("test");
|
|
|
|
|
|
|
|
// Make sure test file exists
|
|
|
|
ensureFileSync(file);
|
|
|
|
Deno.writeFileSync(file, content);
|
|
|
|
assert(Deno.lstatSync(dir));
|
|
|
|
|
|
|
|
// Test varying pairs of `string` and `URL` params.
|
|
|
|
const pairs = [
|
|
|
|
[file, file],
|
|
|
|
[file, url],
|
|
|
|
[url, file],
|
|
|
|
[url, url],
|
2024-02-07 00:08:47 +00:00
|
|
|
] as const;
|
2023-04-05 11:19:24 +00:00
|
|
|
|
|
|
|
for (const p of pairs) {
|
|
|
|
const src = p[0];
|
|
|
|
const dest = p[1];
|
|
|
|
|
|
|
|
moveSync(src, dest, { overwrite: true });
|
|
|
|
assertEquals(Deno.readTextFileSync(src), "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.removeSync(dir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("move() accepts overwrite option set to true for directories", function () {
|
2023-04-05 11:19:24 +00:00
|
|
|
const dir = path.join(testdataDir, "move_sync_same_dir_1");
|
|
|
|
const url = path.toFileUrl(dir);
|
|
|
|
|
|
|
|
// Make sure test dir exists
|
|
|
|
ensureDirSync(dir);
|
|
|
|
assert(Deno.lstatSync(dir));
|
|
|
|
|
|
|
|
// Test varying pairs of `string` and `URL params.
|
|
|
|
const pairs = [
|
|
|
|
[dir, dir],
|
|
|
|
[dir, url],
|
|
|
|
[url, dir],
|
|
|
|
[url, url],
|
2024-02-07 00:08:47 +00:00
|
|
|
] as const;
|
2023-04-05 11:19:24 +00:00
|
|
|
|
|
|
|
for (const p of pairs) {
|
|
|
|
const src = p[0];
|
|
|
|
const dest = p[1];
|
|
|
|
|
2024-07-25 05:53:22 +00:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
moveSync(src, dest);
|
|
|
|
},
|
|
|
|
Deno.errors.NotSupported,
|
|
|
|
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
|
|
|
|
);
|
2023-04-05 11:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Deno.removeSync(dir, { recursive: true });
|
|
|
|
});
|