|
1 | 1 | 'use strict';
|
2 |
| -var common = require('../common'); |
3 |
| -var assert = require('assert'); |
4 |
| -var fs = require('fs'); |
5 |
| -var join = require('path').join; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const fs = require('fs'); |
| 5 | +const join = require('path').join; |
6 | 6 |
|
7 |
| -var filename = join(common.tmpDir, 'append.txt'); |
| 7 | +const filename = join(common.tmpDir, 'append.txt'); |
8 | 8 |
|
9 |
| -var currentFileData = 'ABCD'; |
| 9 | +const currentFileData = 'ABCD'; |
10 | 10 |
|
11 |
| -var n = 220; |
12 |
| -var s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + |
13 |
| - '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + |
14 |
| - '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + |
15 |
| - '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + |
16 |
| - '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + |
17 |
| - '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + |
18 |
| - '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; |
| 11 | +const n = 220; |
| 12 | +const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + |
| 13 | + '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + |
| 14 | + '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + |
| 15 | + '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + |
| 16 | + '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + |
| 17 | + '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + |
| 18 | + '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; |
19 | 19 |
|
20 |
| -var ncallbacks = 0; |
| 20 | +let ncallbacks = 0; |
21 | 21 |
|
22 | 22 | common.refreshTmpDir();
|
23 | 23 |
|
24 | 24 | // test that empty file will be created and have content added
|
25 | 25 | fs.appendFile(filename, s, function(e) {
|
26 |
| - if (e) throw e; |
| 26 | + assert.ifError(e); |
27 | 27 |
|
28 | 28 | ncallbacks++;
|
29 | 29 |
|
30 | 30 | fs.readFile(filename, function(e, buffer) {
|
31 |
| - if (e) throw e; |
| 31 | + assert.ifError(e); |
32 | 32 | ncallbacks++;
|
33 |
| - assert.equal(Buffer.byteLength(s), buffer.length); |
| 33 | + assert.strictEqual(Buffer.byteLength(s), buffer.length); |
34 | 34 | });
|
35 | 35 | });
|
36 | 36 |
|
37 | 37 | // test that appends data to a non empty file
|
38 |
| -var filename2 = join(common.tmpDir, 'append2.txt'); |
| 38 | +const filename2 = join(common.tmpDir, 'append2.txt'); |
39 | 39 | fs.writeFileSync(filename2, currentFileData);
|
40 | 40 |
|
41 | 41 | fs.appendFile(filename2, s, function(e) {
|
42 |
| - if (e) throw e; |
| 42 | + assert.ifError(e); |
43 | 43 |
|
44 | 44 | ncallbacks++;
|
45 | 45 |
|
46 | 46 | fs.readFile(filename2, function(e, buffer) {
|
47 |
| - if (e) throw e; |
| 47 | + assert.ifError(e); |
48 | 48 | ncallbacks++;
|
49 |
| - assert.equal(Buffer.byteLength(s) + currentFileData.length, buffer.length); |
| 49 | + assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, |
| 50 | + buffer.length); |
50 | 51 | });
|
51 | 52 | });
|
52 | 53 |
|
53 | 54 | // test that appendFile accepts buffers
|
54 |
| -var filename3 = join(common.tmpDir, 'append3.txt'); |
| 55 | +const filename3 = join(common.tmpDir, 'append3.txt'); |
55 | 56 | fs.writeFileSync(filename3, currentFileData);
|
56 | 57 |
|
57 |
| -var buf = Buffer.from(s, 'utf8'); |
| 58 | +const buf = Buffer.from(s, 'utf8'); |
58 | 59 |
|
59 | 60 | fs.appendFile(filename3, buf, function(e) {
|
60 |
| - if (e) throw e; |
| 61 | + assert.ifError(e); |
61 | 62 |
|
62 | 63 | ncallbacks++;
|
63 | 64 |
|
64 | 65 | fs.readFile(filename3, function(e, buffer) {
|
65 |
| - if (e) throw e; |
| 66 | + assert.ifError(e); |
66 | 67 | ncallbacks++;
|
67 |
| - assert.equal(buf.length + currentFileData.length, buffer.length); |
| 68 | + assert.strictEqual(buf.length + currentFileData.length, buffer.length); |
68 | 69 | });
|
69 | 70 | });
|
70 | 71 |
|
71 | 72 | // test that appendFile accepts numbers.
|
72 |
| -var filename4 = join(common.tmpDir, 'append4.txt'); |
| 73 | +const filename4 = join(common.tmpDir, 'append4.txt'); |
73 | 74 | fs.writeFileSync(filename4, currentFileData);
|
74 | 75 |
|
75 |
| -var m = 0o600; |
| 76 | +const m = 0o600; |
76 | 77 | fs.appendFile(filename4, n, { mode: m }, function(e) {
|
77 |
| - if (e) throw e; |
| 78 | + assert.ifError(e); |
78 | 79 |
|
79 | 80 | ncallbacks++;
|
80 | 81 |
|
81 | 82 | // windows permissions aren't unix
|
82 | 83 | if (!common.isWindows) {
|
83 |
| - var st = fs.statSync(filename4); |
84 |
| - assert.equal(st.mode & 0o700, m); |
| 84 | + const st = fs.statSync(filename4); |
| 85 | + assert.strictEqual(st.mode & 0o700, m); |
85 | 86 | }
|
86 | 87 |
|
87 | 88 | fs.readFile(filename4, function(e, buffer) {
|
88 |
| - if (e) throw e; |
| 89 | + assert.ifError(e); |
89 | 90 | ncallbacks++;
|
90 |
| - assert.equal(Buffer.byteLength('' + n) + currentFileData.length, |
91 |
| - buffer.length); |
| 91 | + assert.strictEqual(Buffer.byteLength('' + n) + currentFileData.length, |
| 92 | + buffer.length); |
92 | 93 | });
|
93 | 94 | });
|
94 | 95 |
|
95 | 96 | // test that appendFile accepts file descriptors
|
96 |
| -var filename5 = join(common.tmpDir, 'append5.txt'); |
| 97 | +const filename5 = join(common.tmpDir, 'append5.txt'); |
97 | 98 | fs.writeFileSync(filename5, currentFileData);
|
98 | 99 |
|
99 | 100 | fs.open(filename5, 'a+', function(e, fd) {
|
100 |
| - if (e) throw e; |
| 101 | + assert.ifError(e); |
101 | 102 |
|
102 | 103 | ncallbacks++;
|
103 | 104 |
|
104 | 105 | fs.appendFile(fd, s, function(e) {
|
105 |
| - if (e) throw e; |
| 106 | + assert.ifError(e); |
106 | 107 |
|
107 | 108 | ncallbacks++;
|
108 | 109 |
|
109 | 110 | fs.close(fd, function(e) {
|
110 |
| - if (e) throw e; |
| 111 | + assert.ifError(e); |
111 | 112 |
|
112 | 113 | ncallbacks++;
|
113 | 114 |
|
114 | 115 | fs.readFile(filename5, function(e, buffer) {
|
115 |
| - if (e) throw e; |
| 116 | + assert.ifError(e); |
116 | 117 |
|
117 | 118 | ncallbacks++;
|
118 |
| - assert.equal(Buffer.byteLength(s) + currentFileData.length, |
119 |
| - buffer.length); |
| 119 | + assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, |
| 120 | + buffer.length); |
120 | 121 | });
|
121 | 122 | });
|
122 | 123 | });
|
123 | 124 | });
|
124 | 125 |
|
125 | 126 | process.on('exit', function() {
|
126 |
| - assert.equal(12, ncallbacks); |
| 127 | + assert.strictEqual(12, ncallbacks); |
127 | 128 |
|
128 | 129 | fs.unlinkSync(filename);
|
129 | 130 | fs.unlinkSync(filename2);
|
|
0 commit comments