diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-02-10 13:22:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-10 20:22:13 +0000 |
commit | f5e46c9bf2f50d66a953fa133161fc829cecff06 (patch) | |
tree | 8faf2f5831c1c7b11d842cd9908d141082c869a5 /tests/unit/copy_file_test.ts | |
parent | d2477f780630a812bfd65e3987b70c0d309385bb (diff) |
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
Diffstat (limited to 'tests/unit/copy_file_test.ts')
-rw-r--r-- | tests/unit/copy_file_test.ts | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/tests/unit/copy_file_test.ts b/tests/unit/copy_file_test.ts new file mode 100644 index 000000000..ad467f510 --- /dev/null +++ b/tests/unit/copy_file_test.ts @@ -0,0 +1,249 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +import { assertEquals, assertRejects, assertThrows } from "./test_util.ts"; + +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 | URL, s: string) { + const enc = new TextEncoder(); + const data = enc.encode(s); + Deno.writeFileSync(filename, data, { mode: 0o666 }); +} + +function assertSameContent( + filename1: string | URL, + filename2: string | URL, +) { + const data1 = Deno.readFileSync(filename1); + const data2 = Deno.readFileSync(filename2); + assertEquals(data1, data2); +} + +Deno.test( + { permissions: { read: true, write: true } }, + function copyFileSyncSuccess() { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + writeFileString(fromFilename, "Hello world!"); + Deno.copyFileSync(fromFilename, toFilename); + // No change to original file + assertEquals(readFileString(fromFilename), "Hello world!"); + // Original == Dest + assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + function copyFileSyncByUrl() { + 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 }); + }, +); + +Deno.test( + { permissions: { write: true, read: true } }, + function copyFileSyncFailure() { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + // We skip initial writing here, from.txt does not exist + assertThrows( + () => { + Deno.copyFileSync(fromFilename, toFilename); + }, + Deno.errors.NotFound, + `copy '${fromFilename}' -> '${toFilename}'`, + ); + + Deno.removeSync(tempDir, { recursive: true }); + }, +); + +Deno.test( + { permissions: { write: true, read: false } }, + function copyFileSyncPerm1() { + assertThrows(() => { + Deno.copyFileSync("/from.txt", "/to.txt"); + }, Deno.errors.PermissionDenied); + }, +); + +Deno.test( + { permissions: { write: false, read: true } }, + function copyFileSyncPerm2() { + assertThrows(() => { + Deno.copyFileSync("/from.txt", "/to.txt"); + }, Deno.errors.PermissionDenied); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + function copyFileSyncOverwrite() { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + writeFileString(fromFilename, "Hello world!"); + // Make Dest exist and have different content + writeFileString(toFilename, "Goodbye!"); + Deno.copyFileSync(fromFilename, toFilename); + // No change to original file + assertEquals(readFileString(fromFilename), "Hello world!"); + // Original == Dest + assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function copyFileSuccess() { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + writeFileString(fromFilename, "Hello world!"); + await Deno.copyFile(fromFilename, toFilename); + // No change to original file + assertEquals(readFileString(fromFilename), "Hello world!"); + // Original == Dest + assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function copyFileByUrl() { + 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 }); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function copyFileFailure() { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + // We skip initial writing here, from.txt does not exist + await assertRejects( + async () => { + await Deno.copyFile(fromFilename, toFilename); + }, + Deno.errors.NotFound, + `copy '${fromFilename}' -> '${toFilename}'`, + ); + + Deno.removeSync(tempDir, { recursive: true }); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function copyFileOverwrite() { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + writeFileString(fromFilename, "Hello world!"); + // Make Dest exist and have different content + writeFileString(toFilename, "Goodbye!"); + await Deno.copyFile(fromFilename, toFilename); + // No change to original file + assertEquals(readFileString(fromFilename), "Hello world!"); + // Original == Dest + assertSameContent(fromFilename, toFilename); + + Deno.removeSync(tempDir, { recursive: true }); + }, +); + +Deno.test( + { permissions: { read: false, write: true } }, + async function copyFilePerm1() { + await assertRejects(async () => { + await Deno.copyFile("/from.txt", "/to.txt"); + }, Deno.errors.PermissionDenied); + }, +); + +Deno.test( + { permissions: { read: true, write: false } }, + async function copyFilePerm2() { + await assertRejects(async () => { + await Deno.copyFile("/from.txt", "/to.txt"); + }, Deno.errors.PermissionDenied); + }, +); + +function copyFileSyncMode(content: string): void { + const tempDir = Deno.makeTempDirSync(); + const fromFilename = tempDir + "/from.txt"; + const toFilename = tempDir + "/to.txt"; + Deno.writeTextFileSync(fromFilename, content); + Deno.chmodSync(fromFilename, 0o100755); + + Deno.copyFileSync(fromFilename, toFilename); + const toStat = Deno.statSync(toFilename); + assertEquals(toStat.mode!, 0o100755); +} + +Deno.test( + { + ignore: Deno.build.os === "windows", + permissions: { read: true, write: true }, + }, + function copyFileSyncChmod() { + // this Tests different optimization paths on MacOS: + // + // < 128 KB clonefile() w/ fallback to copyfile() + // > 128 KB + copyFileSyncMode("Hello world!"); + copyFileSyncMode("Hello world!".repeat(128 * 1024)); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function copyFileNulPath() { + const fromFilename = "from.txt\0"; + const toFilename = "to.txt\0"; + await assertRejects(async () => { + await Deno.copyFile(fromFilename, toFilename); + }, TypeError); + }, +); |