Skip to content

Commit 69077a1

Browse files
radelmannaddaleax
authored andcommitted
test: refactor test-fs-append-file.js
- Replaced var with either const or let. - Replaced assert.equal() with assert.strictEqual(). - Replaced all error checks with assert.ifError(e). PR-URL: #10110 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent baa1acc commit 69077a1

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

test/parallel/test-fs-append-file.js

+44-43
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,130 @@
11
'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;
66

7-
var filename = join(common.tmpDir, 'append.txt');
7+
const filename = join(common.tmpDir, 'append.txt');
88

9-
var currentFileData = 'ABCD';
9+
const currentFileData = 'ABCD';
1010

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';
1919

20-
var ncallbacks = 0;
20+
let ncallbacks = 0;
2121

2222
common.refreshTmpDir();
2323

2424
// test that empty file will be created and have content added
2525
fs.appendFile(filename, s, function(e) {
26-
if (e) throw e;
26+
assert.ifError(e);
2727

2828
ncallbacks++;
2929

3030
fs.readFile(filename, function(e, buffer) {
31-
if (e) throw e;
31+
assert.ifError(e);
3232
ncallbacks++;
33-
assert.equal(Buffer.byteLength(s), buffer.length);
33+
assert.strictEqual(Buffer.byteLength(s), buffer.length);
3434
});
3535
});
3636

3737
// 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');
3939
fs.writeFileSync(filename2, currentFileData);
4040

4141
fs.appendFile(filename2, s, function(e) {
42-
if (e) throw e;
42+
assert.ifError(e);
4343

4444
ncallbacks++;
4545

4646
fs.readFile(filename2, function(e, buffer) {
47-
if (e) throw e;
47+
assert.ifError(e);
4848
ncallbacks++;
49-
assert.equal(Buffer.byteLength(s) + currentFileData.length, buffer.length);
49+
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
50+
buffer.length);
5051
});
5152
});
5253

5354
// test that appendFile accepts buffers
54-
var filename3 = join(common.tmpDir, 'append3.txt');
55+
const filename3 = join(common.tmpDir, 'append3.txt');
5556
fs.writeFileSync(filename3, currentFileData);
5657

57-
var buf = Buffer.from(s, 'utf8');
58+
const buf = Buffer.from(s, 'utf8');
5859

5960
fs.appendFile(filename3, buf, function(e) {
60-
if (e) throw e;
61+
assert.ifError(e);
6162

6263
ncallbacks++;
6364

6465
fs.readFile(filename3, function(e, buffer) {
65-
if (e) throw e;
66+
assert.ifError(e);
6667
ncallbacks++;
67-
assert.equal(buf.length + currentFileData.length, buffer.length);
68+
assert.strictEqual(buf.length + currentFileData.length, buffer.length);
6869
});
6970
});
7071

7172
// test that appendFile accepts numbers.
72-
var filename4 = join(common.tmpDir, 'append4.txt');
73+
const filename4 = join(common.tmpDir, 'append4.txt');
7374
fs.writeFileSync(filename4, currentFileData);
7475

75-
var m = 0o600;
76+
const m = 0o600;
7677
fs.appendFile(filename4, n, { mode: m }, function(e) {
77-
if (e) throw e;
78+
assert.ifError(e);
7879

7980
ncallbacks++;
8081

8182
// windows permissions aren't unix
8283
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);
8586
}
8687

8788
fs.readFile(filename4, function(e, buffer) {
88-
if (e) throw e;
89+
assert.ifError(e);
8990
ncallbacks++;
90-
assert.equal(Buffer.byteLength('' + n) + currentFileData.length,
91-
buffer.length);
91+
assert.strictEqual(Buffer.byteLength('' + n) + currentFileData.length,
92+
buffer.length);
9293
});
9394
});
9495

9596
// test that appendFile accepts file descriptors
96-
var filename5 = join(common.tmpDir, 'append5.txt');
97+
const filename5 = join(common.tmpDir, 'append5.txt');
9798
fs.writeFileSync(filename5, currentFileData);
9899

99100
fs.open(filename5, 'a+', function(e, fd) {
100-
if (e) throw e;
101+
assert.ifError(e);
101102

102103
ncallbacks++;
103104

104105
fs.appendFile(fd, s, function(e) {
105-
if (e) throw e;
106+
assert.ifError(e);
106107

107108
ncallbacks++;
108109

109110
fs.close(fd, function(e) {
110-
if (e) throw e;
111+
assert.ifError(e);
111112

112113
ncallbacks++;
113114

114115
fs.readFile(filename5, function(e, buffer) {
115-
if (e) throw e;
116+
assert.ifError(e);
116117

117118
ncallbacks++;
118-
assert.equal(Buffer.byteLength(s) + currentFileData.length,
119-
buffer.length);
119+
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
120+
buffer.length);
120121
});
121122
});
122123
});
123124
});
124125

125126
process.on('exit', function() {
126-
assert.equal(12, ncallbacks);
127+
assert.strictEqual(12, ncallbacks);
127128

128129
fs.unlinkSync(filename);
129130
fs.unlinkSync(filename2);

0 commit comments

Comments
 (0)