summaryrefslogtreecommitdiff
path: root/js/copy_file_test.ts
blob: 695d9b8c94e850e4145eb12c6b559a6a41fbe322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";

function readFileString(filename: string): string {
  const dataRead = deno.readFileSync(filename);
  const dec = new TextDecoder("utf-8");
  return dec.decode(dataRead);
}

function writeFileString(filename: string, s: string) {
  const enc = new TextEncoder();
  const data = enc.encode(s);
  deno.writeFileSync(filename, data, 0o666);
}

function assertSameContent(filename1: string, filename2: string) {
  const data1 = deno.readFileSync(filename1);
  const data2 = deno.readFileSync(filename2);
  assertEqual(data1, data2);
}

testPerm({ 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
  assertEqual(readFileString(fromFilename), "Hello world!");
  // Original == Dest
  assertSameContent(fromFilename, toFilename);
});

testPerm({ write: 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
  let err;
  try {
    deno.copyFileSync(fromFilename, toFilename);
  } catch (e) {
    err = e;
  }
  assert(!!err);
  if (deno.platform.os === "win") {
    assertEqual(err.kind, deno.ErrorKind.NotFound);
    assertEqual(err.name, "NotFound");
  } else {
    // On *nix, Rust deem non-existent path as invalid input
    // See https://github.com/rust-lang/rust/issues/54800
    assertEqual(err.kind, deno.ErrorKind.InvalidInput);
    assertEqual(err.name, "InvalidInput");
  }
});

testPerm({ 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
  assertEqual(readFileString(fromFilename), "Hello world!");
  // Original == Dest
  assertSameContent(fromFilename, toFilename);
});

testPerm({ write: false }, function copyFileSyncPerm() {
  let err;
  try {
    deno.copyFileSync("/from.txt", "/to.txt");
  } catch (e) {
    err = e;
  }
  assert(!!err);
  assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
  assertEqual(err.name, "PermissionDenied");
});

testPerm({ 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
  assertEqual(readFileString(fromFilename), "Hello world!");
  // Original == Dest
  assertSameContent(fromFilename, toFilename);
});

testPerm({ 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
  let err;
  try {
    await deno.copyFile(fromFilename, toFilename);
  } catch (e) {
    err = e;
  }
  assert(!!err);
  if (deno.platform.os === "win") {
    assertEqual(err.kind, deno.ErrorKind.NotFound);
    assertEqual(err.name, "NotFound");
  } else {
    // On *nix, Rust deem non-existent path as invalid input
    // See https://github.com/rust-lang/rust/issues/54800
    assertEqual(err.kind, deno.ErrorKind.InvalidInput);
    assertEqual(err.name, "InvalidInput");
  }
});

testPerm({ 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
  assertEqual(readFileString(fromFilename), "Hello world!");
  // Original == Dest
  assertSameContent(fromFilename, toFilename);
});

testPerm({ write: false }, async function copyFilePerm() {
  let err;
  try {
    await deno.copyFile("/from.txt", "/to.txt");
  } catch (e) {
    err = e;
  }
  assert(!!err);
  assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
  assertEqual(err.name, "PermissionDenied");
});