summaryrefslogtreecommitdiff
path: root/js/files_test.ts
blob: d46a46906047a17c8dc8b3fb374aa6c21af62dd9 (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
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as deno from "deno";
import { test, testPerm, assert, assertEqual } from "./test_util.ts";

test(function filesStdioFileDescriptors() {
  assertEqual(deno.stdin.rid, 0);
  assertEqual(deno.stdout.rid, 1);
  assertEqual(deno.stderr.rid, 2);
});

test(async function filesCopyToStdout() {
  const filename = "package.json";
  const file = await deno.open(filename);
  assert(file.rid > 2);
  const bytesWritten = await deno.copy(deno.stdout, file);
  const fileSize = deno.statSync(filename).len;
  assertEqual(bytesWritten, fileSize);
  console.log("bytes written", bytesWritten);
});

test(async function filesToAsyncIterator() {
  const filename = "tests/hello.txt";
  const file = await deno.open(filename);

  let totalSize = 0;
  for await (const buf of deno.toAsyncIterator(file)) {
    totalSize += buf.byteLength;
  }

  assertEqual(totalSize, 12);
});

testPerm({ write: true }, async function createFile() {
  const tempDir = await deno.makeTempDir();
  const filename = tempDir + "/test.txt";
  let f = await deno.open(filename, "w");
  let fileInfo = deno.statSync(filename);
  assert(fileInfo.isFile());
  assert(fileInfo.len === 0);
  const enc = new TextEncoder();
  const data = enc.encode("Hello");
  await f.write(data);
  fileInfo = deno.statSync(filename);
  assert(fileInfo.len === 5);
  f.close();

  // TODO: test different modes
  await deno.removeAll(tempDir);
});

testPerm({ write: true }, async function openModeWrite() {
  const tempDir = deno.makeTempDirSync();
  const encoder = new TextEncoder();
  const filename = tempDir + "hello.txt";
  const data = encoder.encode("Hello world!\n");

  let file = await deno.open(filename, "w");
  // assert file was created
  let fileInfo = deno.statSync(filename);
  assert(fileInfo.isFile());
  assertEqual(fileInfo.len, 0);
  // write some data
  await file.write(data);
  fileInfo = deno.statSync(filename);
  assertEqual(fileInfo.len, 13);
  // assert we can't read from file
  let thrown = false;
  try {
    const buf = new Uint8Array(20);
    await file.read(buf);
  } catch (e) {
    thrown = true;
  } finally {
    assert(thrown, "'w' mode shouldn't allow to read file");
  }
  file.close();
  // assert that existing file is truncated on open
  file = await deno.open(filename, "w");
  file.close();
  const fileSize = deno.statSync(filename).len;
  assertEqual(fileSize, 0);
  await deno.removeAll(tempDir);
});

testPerm({ write: true }, async function openModeWriteRead() {
  const tempDir = deno.makeTempDirSync();
  const encoder = new TextEncoder();
  const filename = tempDir + "hello.txt";
  const data = encoder.encode("Hello world!\n");

  let file = await deno.open(filename, "w+");
  // assert file was created
  let fileInfo = deno.statSync(filename);
  assert(fileInfo.isFile());
  assertEqual(fileInfo.len, 0);
  // write some data
  await file.write(data);
  fileInfo = deno.statSync(filename);
  assertEqual(fileInfo.len, 13);

  // TODO: this test is not working, I expect because
  //  file handle points to the end of file, but ATM
  //  deno has no seek implementation on Rust side
  // assert file can be read
  // const buf = new Uint8Array(20);
  // const result = await file.read(buf);
  // console.log(result.eof, result.nread);
  // assertEqual(result.nread, 13);
  // file.close();

  await deno.removeAll(tempDir);
});