diff options
Diffstat (limited to 'cli/tests/unit')
-rw-r--r-- | cli/tests/unit/chmod_test.ts | 38 | ||||
-rw-r--r-- | cli/tests/unit/chown_test.ts | 40 | ||||
-rw-r--r-- | cli/tests/unit/copy_file_test.ts | 63 | ||||
-rw-r--r-- | cli/tests/unit/files_test.ts | 113 | ||||
-rw-r--r-- | cli/tests/unit/path_from_url_test.ts | 31 | ||||
-rw-r--r-- | cli/tests/unit/read_dir_test.ts | 24 | ||||
-rw-r--r-- | cli/tests/unit/read_file_test.ts | 31 | ||||
-rw-r--r-- | cli/tests/unit/read_text_file_test.ts | 27 | ||||
-rw-r--r-- | cli/tests/unit/remove_test.ts | 30 | ||||
-rw-r--r-- | cli/tests/unit/stat_test.ts | 111 | ||||
-rw-r--r-- | cli/tests/unit/test_util.ts | 7 | ||||
-rw-r--r-- | cli/tests/unit/unit_tests.ts | 1 | ||||
-rw-r--r-- | cli/tests/unit/write_file_test.ts | 38 | ||||
-rw-r--r-- | cli/tests/unit/write_text_file_test.ts | 30 |
14 files changed, 575 insertions, 9 deletions
diff --git a/cli/tests/unit/chmod_test.ts b/cli/tests/unit/chmod_test.ts index d0d17629d..02a5fb22e 100644 --- a/cli/tests/unit/chmod_test.ts +++ b/cli/tests/unit/chmod_test.ts @@ -18,6 +18,25 @@ unitTest( } ); +unitTest( + { ignore: Deno.build.os === "windows", perms: { read: true, write: true } }, + function chmodSyncUrl(): void { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL(`file://${tempDir}/test.txt`); + Deno.writeFileSync(fileUrl, data, { mode: 0o666 }); + + Deno.chmodSync(fileUrl, 0o777); + + const fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.mode); + assertEquals(fileInfo.mode & 0o777, 0o777); + + Deno.removeSync(tempDir, { recursive: true }); + } +); + // Check symlink when not on windows unitTest( { @@ -89,6 +108,25 @@ unitTest( } ); +unitTest( + { ignore: Deno.build.os === "windows", perms: { read: true, write: true } }, + async function chmodUrl(): Promise<void> { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL(`file://${tempDir}/test.txt`); + Deno.writeFileSync(fileUrl, data, { mode: 0o666 }); + + await Deno.chmod(fileUrl, 0o777); + + const fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.mode); + assertEquals(fileInfo.mode & 0o777, 0o777); + + Deno.removeSync(tempDir, { recursive: true }); + } +); + // Check symlink when not on windows unitTest( diff --git a/cli/tests/unit/chown_test.ts b/cli/tests/unit/chown_test.ts index 724ea5a21..bcd5ab9fe 100644 --- a/cli/tests/unit/chown_test.ts +++ b/cli/tests/unit/chown_test.ts @@ -127,6 +127,26 @@ if (Deno.build.os !== "windows") { unitTest( { perms: { run: true, write: true } }, + async function chownSyncWithUrl(): Promise<void> { + // TODO: same as chownSyncSucceed + const { uid, gid } = await getUidAndGid(); + + const enc = new TextEncoder(); + const dirPath = Deno.makeTempDirSync(); + const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`); + const fileData = enc.encode("Hello"); + Deno.writeFileSync(fileUrl, fileData); + + // the test script creates this file with the same uid and gid, + // here chown is a noop so it succeeds under non-priviledged user + Deno.chownSync(fileUrl, uid, gid); + + Deno.removeSync(dirPath, { recursive: true }); + } + ); + + unitTest( + { perms: { run: true, write: true } }, async function chownSucceed(): Promise<void> { // TODO: same as chownSyncSucceed const { uid, gid } = await getUidAndGid(); @@ -144,4 +164,24 @@ if (Deno.build.os !== "windows") { Deno.removeSync(dirPath, { recursive: true }); } ); + + unitTest( + { perms: { run: true, write: true } }, + async function chownWithUrl(): Promise<void> { + // TODO: same as chownSyncSucceed + const { uid, gid } = await getUidAndGid(); + + const enc = new TextEncoder(); + const dirPath = await Deno.makeTempDir(); + const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`); + const fileData = enc.encode("Hello"); + await Deno.writeFile(fileUrl, fileData); + + // the test script creates this file with the same uid and gid, + // here chown is a noop so it succeeds under non-priviledged user + await Deno.chown(fileUrl, uid, gid); + + Deno.removeSync(dirPath, { recursive: true }); + } + ); } diff --git a/cli/tests/unit/copy_file_test.ts b/cli/tests/unit/copy_file_test.ts index 986bab53b..5a1492e50 100644 --- a/cli/tests/unit/copy_file_test.ts +++ b/cli/tests/unit/copy_file_test.ts @@ -1,19 +1,22 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { unitTest, assert, assertEquals } from "./test_util.ts"; -function readFileString(filename: string): string { +function readFileString(filename: string | URL): string { const dataRead = Deno.readFileSync(filename); const dec = new TextDecoder("utf-8"); return dec.decode(dataRead); } -function writeFileString(filename: string, s: string): void { +function writeFileString(filename: string | URL, s: string): void { const enc = new TextEncoder(); const data = enc.encode(s); Deno.writeFileSync(filename, data, { mode: 0o666 }); } -function assertSameContent(filename1: string, filename2: string): void { +function assertSameContent( + filename1: string | URL, + filename2: string | URL +): void { const data1 = Deno.readFileSync(filename1); const data2 = Deno.readFileSync(filename2); assertEquals(data1, data2); @@ -31,6 +34,29 @@ unitTest( assertEquals(readFileString(fromFilename), "Hello world!"); // Original == Dest assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, + function copyFileSyncByUrl(): void { + const tempDir = Deno.makeTempDirSync(); + const fromUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt` + ); + const toUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt` + ); + writeFileString(fromUrl, "Hello world!"); + Deno.copyFileSync(fromUrl, toUrl); + // No change to original file + assertEquals(readFileString(fromUrl), "Hello world!"); + // Original == Dest + assertSameContent(fromUrl, toUrl); + + Deno.removeSync(tempDir, { recursive: true }); } ); @@ -49,6 +75,8 @@ unitTest( } assert(!!err); assert(err instanceof Deno.errors.NotFound); + + Deno.removeSync(tempDir, { recursive: true }); } ); @@ -94,6 +122,8 @@ unitTest( assertEquals(readFileString(fromFilename), "Hello world!"); // Original == Dest assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); } ); @@ -109,6 +139,29 @@ unitTest( assertEquals(readFileString(fromFilename), "Hello world!"); // Original == Dest assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, + async function copyFileByUrl(): Promise<void> { + const tempDir = Deno.makeTempDirSync(); + const fromUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt` + ); + const toUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt` + ); + writeFileString(fromUrl, "Hello world!"); + await Deno.copyFile(fromUrl, toUrl); + // No change to original file + assertEquals(readFileString(fromUrl), "Hello world!"); + // Original == Dest + assertSameContent(fromUrl, toUrl); + + Deno.removeSync(tempDir, { recursive: true }); } ); @@ -127,6 +180,8 @@ unitTest( } assert(!!err); assert(err instanceof Deno.errors.NotFound); + + Deno.removeSync(tempDir, { recursive: true }); } ); @@ -144,6 +199,8 @@ unitTest( assertEquals(readFileString(fromFilename), "Hello world!"); // Original == Dest assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); } ); diff --git a/cli/tests/unit/files_test.ts b/cli/tests/unit/files_test.ts index 46bcb2173..e5db0c613 100644 --- a/cli/tests/unit/files_test.ts +++ b/cli/tests/unit/files_test.ts @@ -198,6 +198,54 @@ unitTest( ); unitTest( + { + perms: { read: true, write: true }, + }, + function openSyncUrl(): void { + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test_open.txt` + ); + const file = Deno.openSync(fileUrl, { + write: true, + createNew: true, + mode: 0o626, + }); + file.close(); + const pathInfo = Deno.statSync(fileUrl); + if (Deno.build.os !== "windows") { + assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask()); + } + + Deno.removeSync(tempDir, { recursive: true }); + } +); + +unitTest( + { + perms: { read: true, write: true }, + }, + async function openUrl(): Promise<void> { + const tempDir = await Deno.makeTempDir(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test_open.txt` + ); + const file = await Deno.open(fileUrl, { + write: true, + createNew: true, + mode: 0o626, + }); + file.close(); + const pathInfo = Deno.statSync(fileUrl); + if (Deno.build.os !== "windows") { + assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask()); + } + + Deno.removeSync(tempDir, { recursive: true }); + } +); + +unitTest( { perms: { write: false } }, async function writePermFailure(): Promise<void> { const filename = "tests/hello.txt"; @@ -377,6 +425,71 @@ unitTest( unitTest( { perms: { read: true, write: true } }, + async function createFileWithUrl(): Promise<void> { + const tempDir = await Deno.makeTempDir(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + const f = await Deno.create(fileUrl); + let fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.isFile); + assert(fileInfo.size === 0); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + await f.write(data); + fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.size === 5); + f.close(); + + await Deno.remove(tempDir, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, + async function createSyncFile(): Promise<void> { + const tempDir = await Deno.makeTempDir(); + const filename = tempDir + "/test.txt"; + const f = Deno.createSync(filename); + let fileInfo = Deno.statSync(filename); + assert(fileInfo.isFile); + assert(fileInfo.size === 0); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + await f.write(data); + fileInfo = Deno.statSync(filename); + assert(fileInfo.size === 5); + f.close(); + + // TODO: test different modes + await Deno.remove(tempDir, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, + async function createSyncFileWithUrl(): Promise<void> { + const tempDir = await Deno.makeTempDir(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + const f = Deno.createSync(fileUrl); + let fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.isFile); + assert(fileInfo.size === 0); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + await f.write(data); + fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.size === 5); + f.close(); + + await Deno.remove(tempDir, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, async function openModeWrite(): Promise<void> { const tempDir = Deno.makeTempDirSync(); const encoder = new TextEncoder(); diff --git a/cli/tests/unit/path_from_url_test.ts b/cli/tests/unit/path_from_url_test.ts new file mode 100644 index 000000000..5e7203a58 --- /dev/null +++ b/cli/tests/unit/path_from_url_test.ts @@ -0,0 +1,31 @@ +import { assertThrows, assertEquals, unitTest } from "./test_util.ts"; + +// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol +const { pathFromURL } = Deno[Deno.internal]; + +unitTest( + { ignore: Deno.build.os === "windows" }, + function pathFromURLPosix(): void { + assertEquals(pathFromURL("file:///test/directory"), "/test/directory"); + assertThrows(() => pathFromURL("file://host/test/directory")); + assertThrows(() => pathFromURL("https://deno.land/welcome.ts")); + } +); + +unitTest( + { ignore: Deno.build.os !== "windows" }, + function pathFromURLWin32(): void { + assertEquals(pathFromURL("file:///c:/windows/test"), "c:\\windows\\test"); + assertThrows(() => pathFromURL("file:///thing/test")); + assertThrows(() => pathFromURL("https://deno.land/welcome.ts")); + /* TODO(ry) Add tests for these situations + * ampersand_&.tx file:///D:/weird_names/ampersand_&.txt + * at_@.txt file:///D:/weird_names/at_@.txt + * emoji_🙃.txt file:///D:/weird_names/emoji_%F0%9F%99%83.txt + * percent_%.txt file:///D:/weird_names/percent_%25.txt + * pound_#.txt file:///D:/weird_names/pound_%23.txt + * space_ .txt file:///D:/weird_names/space_%20.txt + * swapped_surrogate_pair_��.txt file:///D:/weird_names/swapped_surrogate_pair_%EF%BF%BD%EF%BF%BD.txt + */ + } +); diff --git a/cli/tests/unit/read_dir_test.ts b/cli/tests/unit/read_dir_test.ts index 79e2a1507..d9a5244c7 100644 --- a/cli/tests/unit/read_dir_test.ts +++ b/cli/tests/unit/read_dir_test.ts @@ -1,5 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { unitTest, assert, assertEquals } from "./test_util.ts"; +import { + unitTest, + assert, + assertEquals, + pathToAbsoluteFileUrl, +} from "./test_util.ts"; function assertSameContent(files: Deno.DirEntry[]): void { let counter = 0; @@ -19,6 +24,11 @@ unitTest({ perms: { read: true } }, function readDirSyncSuccess(): void { assertSameContent(files); }); +unitTest({ perms: { read: true } }, function readDirSyncWithUrl(): void { + const files = [...Deno.readDirSync(pathToAbsoluteFileUrl("cli/tests"))]; + assertSameContent(files); +}); + unitTest({ perms: { read: false } }, function readDirSyncPerm(): void { let caughtError = false; try { @@ -68,6 +78,18 @@ unitTest({ perms: { read: true } }, async function readDirSuccess(): Promise< assertSameContent(files); }); +unitTest({ perms: { read: true } }, async function readDirWithUrl(): Promise< + void +> { + const files = []; + for await (const dirEntry of Deno.readDir( + pathToAbsoluteFileUrl("cli/tests") + )) { + files.push(dirEntry); + } + assertSameContent(files); +}); + unitTest({ perms: { read: false } }, async function readDirPerm(): Promise< void > { diff --git a/cli/tests/unit/read_file_test.ts b/cli/tests/unit/read_file_test.ts index 0d3cdf422..cae9037ab 100644 --- a/cli/tests/unit/read_file_test.ts +++ b/cli/tests/unit/read_file_test.ts @@ -1,5 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { unitTest, assert, assertEquals } from "./test_util.ts"; +import { + unitTest, + assert, + assertEquals, + pathToAbsoluteFileUrl, +} from "./test_util.ts"; unitTest({ perms: { read: true } }, function readFileSyncSuccess(): void { const data = Deno.readFileSync("cli/tests/fixture.json"); @@ -10,6 +15,17 @@ unitTest({ perms: { read: true } }, function readFileSyncSuccess(): void { assertEquals(pkg.name, "deno"); }); +unitTest({ perms: { read: true } }, function readFileSyncUrl(): void { + const data = Deno.readFileSync( + pathToAbsoluteFileUrl("cli/tests/fixture.json") + ); + assert(data.byteLength > 0); + const decoder = new TextDecoder("utf-8"); + const json = decoder.decode(data); + const pkg = JSON.parse(json); + assertEquals(pkg.name, "deno"); +}); + unitTest({ perms: { read: false } }, function readFileSyncPerm(): void { let caughtError = false; try { @@ -34,6 +50,19 @@ unitTest({ perms: { read: true } }, function readFileSyncNotFound(): void { assert(data === undefined); }); +unitTest({ perms: { read: true } }, async function readFileUrl(): Promise< + void +> { + const data = await Deno.readFile( + pathToAbsoluteFileUrl("cli/tests/fixture.json") + ); + assert(data.byteLength > 0); + const decoder = new TextDecoder("utf-8"); + const json = decoder.decode(data); + const pkg = JSON.parse(json); + assertEquals(pkg.name, "deno"); +}); + unitTest({ perms: { read: true } }, async function readFileSuccess(): Promise< void > { diff --git a/cli/tests/unit/read_text_file_test.ts b/cli/tests/unit/read_text_file_test.ts index 3e7493e4a..0f1759c54 100644 --- a/cli/tests/unit/read_text_file_test.ts +++ b/cli/tests/unit/read_text_file_test.ts @@ -1,4 +1,9 @@ -import { unitTest, assert, assertEquals } from "./test_util.ts"; +import { + unitTest, + assert, + assertEquals, + pathToAbsoluteFileUrl, +} from "./test_util.ts"; unitTest({ perms: { read: true } }, function readTextFileSyncSuccess(): void { const data = Deno.readTextFileSync("cli/tests/fixture.json"); @@ -7,6 +12,15 @@ unitTest({ perms: { read: true } }, function readTextFileSyncSuccess(): void { assertEquals(pkg.name, "deno"); }); +unitTest({ perms: { read: true } }, function readTextFileSyncByUrl(): void { + const data = Deno.readTextFileSync( + pathToAbsoluteFileUrl("cli/tests/fixture.json") + ); + assert(data.length > 0); + const pkg = JSON.parse(data); + assertEquals(pkg.name, "deno"); +}); + unitTest({ perms: { read: false } }, function readTextFileSyncPerm(): void { let caughtError = false; try { @@ -41,6 +55,17 @@ unitTest( } ); +unitTest({ perms: { read: true } }, async function readTextFileByUrl(): Promise< + void +> { + const data = await Deno.readTextFile( + pathToAbsoluteFileUrl("cli/tests/fixture.json") + ); + assert(data.length > 0); + const pkg = JSON.parse(data); + assertEquals(pkg.name, "deno"); +}); + unitTest({ perms: { read: false } }, async function readTextFilePerm(): Promise< void > { diff --git a/cli/tests/unit/remove_test.ts b/cli/tests/unit/remove_test.ts index f9095c1c1..94fbba682 100644 --- a/cli/tests/unit/remove_test.ts +++ b/cli/tests/unit/remove_test.ts @@ -53,6 +53,36 @@ unitTest( unitTest( { perms: { write: true, read: true } }, + async function removeFileByUrl(): Promise<void> { + for (const method of REMOVE_METHODS) { + // REMOVE FILE + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + + Deno.writeFileSync(fileUrl, data, { mode: 0o666 }); + const fileInfo = Deno.statSync(fileUrl); + assert(fileInfo.isFile); // check exist first + await Deno[method](fileUrl); // remove + // We then check again after remove + let err; + try { + Deno.statSync(fileUrl); + } catch (e) { + err = e; + } + // File is gone + assert(err instanceof Deno.errors.NotFound); + } + } +); + +unitTest( + { perms: { write: true, read: true } }, async function removeFail(): Promise<void> { for (const method of REMOVE_METHODS) { // NON-EMPTY DIRECTORY diff --git a/cli/tests/unit/stat_test.ts b/cli/tests/unit/stat_test.ts index 7eaf73d58..67598a2d7 100644 --- a/cli/tests/unit/stat_test.ts +++ b/cli/tests/unit/stat_test.ts @@ -1,5 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { unitTest, assert, assertEquals } from "./test_util.ts"; +import { + unitTest, + assert, + assertEquals, + pathToAbsoluteFileUrl, +} from "./test_util.ts"; unitTest( { perms: { read: true, write: true } }, @@ -18,12 +23,47 @@ unitTest( const tempFile = Deno.makeTempFileSync(); const tempInfo = Deno.statSync(tempFile); - const now = Date.now(); + let now = Date.now(); assert(tempInfo.atime !== null && now - tempInfo.atime.valueOf() < 1000); assert(tempInfo.mtime !== null && now - tempInfo.mtime.valueOf() < 1000); assert( tempInfo.birthtime === null || now - tempInfo.birthtime.valueOf() < 1000 ); + + const packageInfoByUrl = Deno.statSync(pathToAbsoluteFileUrl("README.md")); + assert(packageInfoByUrl.isFile); + assert(!packageInfoByUrl.isSymlink); + + const modulesInfoByUrl = Deno.statSync( + pathToAbsoluteFileUrl("cli/tests/symlink_to_subdir") + ); + assert(modulesInfoByUrl.isDirectory); + assert(!modulesInfoByUrl.isSymlink); + + const testsInfoByUrl = Deno.statSync(pathToAbsoluteFileUrl("cli/tests")); + assert(testsInfoByUrl.isDirectory); + assert(!testsInfoByUrl.isSymlink); + + const tempFileForUrl = Deno.makeTempFileSync(); + const tempInfoByUrl = Deno.statSync( + new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempFileForUrl}` + ) + ); + now = Date.now(); + assert( + tempInfoByUrl.atime !== null && now - tempInfoByUrl.atime.valueOf() < 1000 + ); + assert( + tempInfoByUrl.mtime !== null && now - tempInfoByUrl.mtime.valueOf() < 1000 + ); + assert( + tempInfoByUrl.birthtime === null || + now - tempInfoByUrl.birthtime.valueOf() < 1000 + ); + + Deno.removeSync(tempFile, { recursive: true }); + Deno.removeSync(tempFileForUrl, { recursive: true }); } ); @@ -58,13 +98,27 @@ unitTest({ perms: { read: true } }, function lstatSyncSuccess(): void { assert(packageInfo.isFile); assert(!packageInfo.isSymlink); + const packageInfoByUrl = Deno.lstatSync(pathToAbsoluteFileUrl("README.md")); + assert(packageInfoByUrl.isFile); + assert(!packageInfoByUrl.isSymlink); + const modulesInfo = Deno.lstatSync("cli/tests/symlink_to_subdir"); assert(!modulesInfo.isDirectory); assert(modulesInfo.isSymlink); + const modulesInfoByUrl = Deno.lstatSync( + pathToAbsoluteFileUrl("cli/tests/symlink_to_subdir") + ); + assert(!modulesInfoByUrl.isDirectory); + assert(modulesInfoByUrl.isSymlink); + const coreInfo = Deno.lstatSync("core"); assert(coreInfo.isDirectory); assert(!coreInfo.isSymlink); + + const coreInfoByUrl = Deno.lstatSync(pathToAbsoluteFileUrl("core")); + assert(coreInfoByUrl.isDirectory); + assert(!coreInfoByUrl.isSymlink); }); unitTest({ perms: { read: false } }, function lstatSyncPerm(): void { @@ -100,23 +154,60 @@ unitTest( assert(packageInfo.isFile); assert(!packageInfo.isSymlink); + const packageInfoByUrl = await Deno.stat( + pathToAbsoluteFileUrl("README.md") + ); + assert(packageInfoByUrl.isFile); + assert(!packageInfoByUrl.isSymlink); + const modulesInfo = await Deno.stat("cli/tests/symlink_to_subdir"); assert(modulesInfo.isDirectory); assert(!modulesInfo.isSymlink); + const modulesInfoByUrl = await Deno.stat( + pathToAbsoluteFileUrl("cli/tests/symlink_to_subdir") + ); + assert(modulesInfoByUrl.isDirectory); + assert(!modulesInfoByUrl.isSymlink); + const testsInfo = await Deno.stat("cli/tests"); assert(testsInfo.isDirectory); assert(!testsInfo.isSymlink); + const testsInfoByUrl = await Deno.stat(pathToAbsoluteFileUrl("cli/tests")); + assert(testsInfoByUrl.isDirectory); + assert(!testsInfoByUrl.isSymlink); + const tempFile = await Deno.makeTempFile(); const tempInfo = await Deno.stat(tempFile); - const now = Date.now(); + let now = Date.now(); assert(tempInfo.atime !== null && now - tempInfo.atime.valueOf() < 1000); assert(tempInfo.mtime !== null && now - tempInfo.mtime.valueOf() < 1000); assert( tempInfo.birthtime === null || now - tempInfo.birthtime.valueOf() < 1000 ); + + const tempFileForUrl = await Deno.makeTempFile(); + const tempInfoByUrl = await Deno.stat( + new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempFileForUrl}` + ) + ); + now = Date.now(); + assert( + tempInfoByUrl.atime !== null && now - tempInfoByUrl.atime.valueOf() < 1000 + ); + assert( + tempInfoByUrl.mtime !== null && now - tempInfoByUrl.mtime.valueOf() < 1000 + ); + assert( + tempInfoByUrl.birthtime === null || + now - tempInfoByUrl.birthtime.valueOf() < 1000 + ); + + Deno.removeSync(tempFile, { recursive: true }); + Deno.removeSync(tempFileForUrl, { recursive: true }); } ); @@ -155,13 +246,27 @@ unitTest({ perms: { read: true } }, async function lstatSuccess(): Promise< assert(packageInfo.isFile); assert(!packageInfo.isSymlink); + const packageInfoByUrl = await Deno.lstat(pathToAbsoluteFileUrl("README.md")); + assert(packageInfoByUrl.isFile); + assert(!packageInfoByUrl.isSymlink); + const modulesInfo = await Deno.lstat("cli/tests/symlink_to_subdir"); assert(!modulesInfo.isDirectory); assert(modulesInfo.isSymlink); + const modulesInfoByUrl = await Deno.lstat( + pathToAbsoluteFileUrl("cli/tests/symlink_to_subdir") + ); + assert(!modulesInfoByUrl.isDirectory); + assert(modulesInfoByUrl.isSymlink); + const coreInfo = await Deno.lstat("core"); assert(coreInfo.isDirectory); assert(!coreInfo.isSymlink); + + const coreInfoByUrl = await Deno.lstat(pathToAbsoluteFileUrl("core")); + assert(coreInfoByUrl.isDirectory); + assert(!coreInfoByUrl.isSymlink); }); unitTest({ perms: { read: false } }, async function lstatPerm(): Promise<void> { diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts index 8b690505a..660e13511 100644 --- a/cli/tests/unit/test_util.ts +++ b/cli/tests/unit/test_util.ts @@ -1,6 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { assert, assertEquals } from "../../../std/testing/asserts.ts"; +import { resolve } from "../../../std/path/mod.ts"; export { assert, assertThrows, @@ -363,3 +364,9 @@ unitTest( }); } ); + +export function pathToAbsoluteFileUrl(path: string): URL { + path = resolve(path); + + return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`); +} diff --git a/cli/tests/unit/unit_tests.ts b/cli/tests/unit/unit_tests.ts index 7891ea418..b16141bdc 100644 --- a/cli/tests/unit/unit_tests.ts +++ b/cli/tests/unit/unit_tests.ts @@ -40,6 +40,7 @@ import "./mkdir_test.ts"; import "./net_test.ts"; import "./os_test.ts"; import "./permissions_test.ts"; +import "./path_from_url_test.ts"; import "./process_test.ts"; import "./real_path_test.ts"; import "./read_dir_test.ts"; diff --git a/cli/tests/unit/write_file_test.ts b/cli/tests/unit/write_file_test.ts index 80f711dbc..6c263c32c 100644 --- a/cli/tests/unit/write_file_test.ts +++ b/cli/tests/unit/write_file_test.ts @@ -15,6 +15,25 @@ unitTest( } ); +unitTest( + { perms: { read: true, write: true } }, + function writeFileSyncUrl(): void { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + Deno.writeFileSync(fileUrl, data); + const dataRead = Deno.readFileSync(fileUrl); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEquals("Hello", actual); + + Deno.removeSync(tempDir, { recursive: true }); + } +); + unitTest({ perms: { write: true } }, function writeFileSyncFail(): void { const enc = new TextEncoder(); const data = enc.encode("Hello"); @@ -127,6 +146,25 @@ unitTest( unitTest( { perms: { read: true, write: true } }, + async function writeFileUrl(): Promise<void> { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const tempDir = await Deno.makeTempDir(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + await Deno.writeFile(fileUrl, data); + const dataRead = Deno.readFileSync(fileUrl); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEquals("Hello", actual); + + Deno.removeSync(tempDir, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, async function writeFileNotFound(): Promise<void> { const enc = new TextEncoder(); const data = enc.encode("Hello"); diff --git a/cli/tests/unit/write_text_file_test.ts b/cli/tests/unit/write_text_file_test.ts index 321189b0e..d72fa722e 100644 --- a/cli/tests/unit/write_text_file_test.ts +++ b/cli/tests/unit/write_text_file_test.ts @@ -10,6 +10,21 @@ unitTest( } ); +unitTest( + { perms: { read: true, write: true } }, + function writeTextFileSyncByUrl(): void { + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + Deno.writeTextFileSync(fileUrl, "Hello"); + const dataRead = Deno.readTextFileSync(fileUrl); + assertEquals("Hello", dataRead); + + Deno.removeSync(fileUrl, { recursive: true }); + } +); + unitTest({ perms: { write: true } }, function writeTextFileSyncFail(): void { const filename = "/baddir/test.txt"; // The following should fail because /baddir doesn't exist (hopefully). @@ -48,6 +63,21 @@ unitTest( unitTest( { perms: { read: true, write: true } }, + async function writeTextFileByUrl(): Promise<void> { + const tempDir = Deno.makeTempDirSync(); + const fileUrl = new URL( + `file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt` + ); + await Deno.writeTextFile(fileUrl, "Hello"); + const dataRead = Deno.readTextFileSync(fileUrl); + assertEquals("Hello", dataRead); + + Deno.removeSync(fileUrl, { recursive: true }); + } +); + +unitTest( + { perms: { read: true, write: true } }, async function writeTextFileNotFound(): Promise<void> { const filename = "/baddir/test.txt"; // The following should fail because /baddir doesn't exist (hopefully). |