• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5// The following tests validate base functionality for the fs.promises
6// FileHandle.readFile method.
7
8const fs = require('fs');
9const { open } = fs.promises;
10const path = require('path');
11const tmpdir = require('../common/tmpdir');
12const assert = require('assert');
13const tmpDir = tmpdir.path;
14
15tmpdir.refresh();
16
17async function validateReadFile() {
18  const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
19  const fileHandle = await open(filePath, 'w+');
20  const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
21
22  const fd = fs.openSync(filePath, 'w+');
23  fs.writeSync(fd, buffer, 0, buffer.length);
24  fs.closeSync(fd);
25
26  const readFileData = await fileHandle.readFile();
27  assert.deepStrictEqual(buffer, readFileData);
28}
29
30async function validateReadFileProc() {
31  // Test to make sure reading a file under the /proc directory works. Adapted
32  // from test-fs-read-file-sync-hostname.js.
33  // Refs:
34  // - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
35  // - https://github.com/nodejs/node/issues/21331
36
37  // Test is Linux-specific.
38  if (!common.isLinux)
39    return;
40
41  const fileHandle = await open('/proc/sys/kernel/hostname', 'r');
42  const hostname = await fileHandle.readFile();
43  assert.ok(hostname.length > 0);
44}
45
46validateReadFile()
47  .then(() => validateReadFileProc())
48  .then(common.mustCall());
49