summaryrefslogtreecommitdiff
path: root/std/fs
diff options
context:
space:
mode:
authorAli Hasani <a.hassssani@gmail.com>2020-05-19 03:16:02 +0430
committerGitHub <noreply@github.com>2020-05-19 00:46:02 +0200
commit6072755eadb7342a409f43260e5a17b956703a1c (patch)
tree35ec9b10eddafdc2b0dacecc439aa8d9f785529a /std/fs
parent88b24261ba467c20d4ef90224b07c19a71398f0f (diff)
Implement Deno.symlink() for windows (#5533)
Diffstat (limited to 'std/fs')
-rw-r--r--std/fs/copy.ts19
-rw-r--r--std/fs/copy_test.ts35
-rw-r--r--std/fs/ensure_symlink.ts18
-rw-r--r--std/fs/ensure_symlink_test.ts49
4 files changed, 36 insertions, 85 deletions
diff --git a/std/fs/copy.ts b/std/fs/copy.ts
index 8ebec3ae1..d45ac17c9 100644
--- a/std/fs/copy.ts
+++ b/std/fs/copy.ts
@@ -4,6 +4,8 @@ import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { isSubdir, getFileInfoType } from "./_util.ts";
import { assert } from "../testing/asserts.ts";
+const isWindows = Deno.build.os === "windows";
+
export interface CopyOptions {
/**
* overwrite existing file or directory. Default is `false`
@@ -111,7 +113,13 @@ async function copySymLink(
await ensureValidCopy(src, dest, options);
const originSrcFilePath = await Deno.readLink(src);
const type = getFileInfoType(await Deno.lstat(src));
- await Deno.symlink(originSrcFilePath, dest, type);
+ if (isWindows) {
+ await Deno.symlink(originSrcFilePath, dest, {
+ type: type === "dir" ? "dir" : "file",
+ });
+ } else {
+ await Deno.symlink(originSrcFilePath, dest);
+ }
if (options.preserveTimestamps) {
const statInfo = await Deno.lstat(src);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
@@ -129,7 +137,14 @@ function copySymlinkSync(
ensureValidCopySync(src, dest, options);
const originSrcFilePath = Deno.readLinkSync(src);
const type = getFileInfoType(Deno.lstatSync(src));
- Deno.symlinkSync(originSrcFilePath, dest, type);
+ if (isWindows) {
+ Deno.symlinkSync(originSrcFilePath, dest, {
+ type: type === "dir" ? "dir" : "file",
+ });
+ } else {
+ Deno.symlinkSync(originSrcFilePath, dest);
+ }
+
if (options.preserveTimestamps) {
const statInfo = Deno.lstatSync(src);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
diff --git a/std/fs/copy_test.ts b/std/fs/copy_test.ts
index 1f3330226..cb97d4ba7 100644
--- a/std/fs/copy_test.ts
+++ b/std/fs/copy_test.ts
@@ -14,9 +14,6 @@ import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
const testdataDir = path.resolve("fs", "testdata");
-// TODO(axetroy): Add test for Windows once symlink is implemented for Windows.
-const isWindows = Deno.build.os === "windows";
-
function testCopy(name: string, cb: (tempDir: string) => Promise<void>): void {
Deno.test({
name,
@@ -257,14 +254,6 @@ testCopy(
const srcLink = path.join(dir, "0.txt");
const destLink = path.join(tempDir, "0_copy.txt");
- if (isWindows) {
- await assertThrowsAsync(
- // (): Promise<void> => copy(srcLink, destLink),
- (): Promise<void> => ensureSymlink(srcLink, destLink)
- );
- return;
- }
-
assert(
(await Deno.lstat(srcLink)).isSymlink,
`'${srcLink}' should be symlink type`
@@ -285,14 +274,6 @@ testCopy(
const srcLink = path.join(tempDir, "copy_dir_link");
const destLink = path.join(tempDir, "copy_dir_link_copy");
- if (isWindows) {
- await assertThrowsAsync(
- // (): Promise<void> => copy(srcLink, destLink),
- (): Promise<void> => ensureSymlink(srcLink, destLink)
- );
- return;
- }
-
await ensureSymlink(srcDir, srcLink);
assert(
@@ -497,14 +478,6 @@ testCopySync(
const srcLink = path.join(dir, "0.txt");
const destLink = path.join(tempDir, "0_copy.txt");
- if (isWindows) {
- assertThrows(
- // (): void => copySync(srcLink, destLink),
- (): void => ensureSymlinkSync(srcLink, destLink)
- );
- return;
- }
-
assert(
Deno.lstatSync(srcLink).isSymlink,
`'${srcLink}' should be symlink type`
@@ -525,14 +498,6 @@ testCopySync(
const srcLink = path.join(tempDir, "copy_dir_link");
const destLink = path.join(tempDir, "copy_dir_link_copy");
- if (isWindows) {
- assertThrows(
- // (): void => copySync(srcLink, destLink),
- (): void => ensureSymlinkSync(srcLink, destLink)
- );
- return;
- }
-
ensureSymlinkSync(originDir, srcLink);
assert(
diff --git a/std/fs/ensure_symlink.ts b/std/fs/ensure_symlink.ts
index 2a184bb4f..a07f97220 100644
--- a/std/fs/ensure_symlink.ts
+++ b/std/fs/ensure_symlink.ts
@@ -28,7 +28,14 @@ export async function ensureSymlink(src: string, dest: string): Promise<void> {
await ensureDir(path.dirname(dest));
- await Deno.symlink(src, dest, srcFilePathType);
+ ensureDirSync(path.dirname(dest));
+ if (Deno.build.os === "windows") {
+ await Deno.symlink(src, dest, {
+ type: srcFilePathType === "dir" ? "dir" : "file",
+ });
+ } else {
+ await Deno.symlink(src, dest);
+ }
}
/**
@@ -54,6 +61,11 @@ export function ensureSymlinkSync(src: string, dest: string): void {
}
ensureDirSync(path.dirname(dest));
-
- Deno.symlinkSync(src, dest, srcFilePathType);
+ if (Deno.build.os === "windows") {
+ Deno.symlinkSync(src, dest, {
+ type: srcFilePathType === "dir" ? "dir" : "file",
+ });
+ } else {
+ Deno.symlinkSync(src, dest);
+ }
}
diff --git a/std/fs/ensure_symlink_test.ts b/std/fs/ensure_symlink_test.ts
index 5fda1c86a..d90495cbf 100644
--- a/std/fs/ensure_symlink_test.ts
+++ b/std/fs/ensure_symlink_test.ts
@@ -9,7 +9,6 @@ import * as path from "../path/mod.ts";
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
const testdataDir = path.resolve("fs", "testdata");
-const isWindows = Deno.build.os === "windows";
Deno.test("ensureSymlinkIfItNotExist", async function (): Promise<void> {
const testDir = path.join(testdataDir, "link_file_1");
@@ -52,17 +51,7 @@ Deno.test("ensureSymlinkIfItExist", async function (): Promise<void> {
await Deno.mkdir(testDir, { recursive: true });
await Deno.writeFile(testFile, new Uint8Array());
- if (isWindows) {
- await assertThrowsAsync(
- (): Promise<void> => ensureSymlink(testFile, linkFile),
- Error,
- "not implemented"
- );
- await Deno.remove(testDir, { recursive: true });
- return;
- } else {
- await ensureSymlink(testFile, linkFile);
- }
+ await ensureSymlink(testFile, linkFile);
const srcStat = await Deno.lstat(testFile);
const linkStat = await Deno.lstat(linkFile);
@@ -81,17 +70,7 @@ Deno.test("ensureSymlinkSyncIfItExist", function (): void {
Deno.mkdirSync(testDir, { recursive: true });
Deno.writeFileSync(testFile, new Uint8Array());
- if (isWindows) {
- assertThrows(
- (): void => ensureSymlinkSync(testFile, linkFile),
- Error,
- "not implemented"
- );
- Deno.removeSync(testDir, { recursive: true });
- return;
- } else {
- ensureSymlinkSync(testFile, linkFile);
- }
+ ensureSymlinkSync(testFile, linkFile);
const srcStat = Deno.lstatSync(testFile);
@@ -111,17 +90,7 @@ Deno.test("ensureSymlinkDirectoryIfItExist", async function (): Promise<void> {
await Deno.mkdir(testDir, { recursive: true });
await Deno.writeFile(testFile, new Uint8Array());
- if (isWindows) {
- await assertThrowsAsync(
- (): Promise<void> => ensureSymlink(testDir, linkDir),
- Error,
- "not implemented"
- );
- await Deno.remove(testDir, { recursive: true });
- return;
- } else {
- await ensureSymlink(testDir, linkDir);
- }
+ await ensureSymlink(testDir, linkDir);
const testDirStat = await Deno.lstat(testDir);
const linkDirStat = await Deno.lstat(linkDir);
@@ -143,17 +112,7 @@ Deno.test("ensureSymlinkSyncDirectoryIfItExist", function (): void {
Deno.mkdirSync(testDir, { recursive: true });
Deno.writeFileSync(testFile, new Uint8Array());
- if (isWindows) {
- assertThrows(
- (): void => ensureSymlinkSync(testDir, linkDir),
- Error,
- "not implemented"
- );
- Deno.removeSync(testDir, { recursive: true });
- return;
- } else {
- ensureSymlinkSync(testDir, linkDir);
- }
+ ensureSymlinkSync(testDir, linkDir);
const testDirStat = Deno.lstatSync(testDir);
const linkDirStat = Deno.lstatSync(linkDir);