Skip to content

Commit a5fad2e

Browse files
committed
improve dirAction test
- augment testing
1 parent 1759858 commit a5fad2e

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

action-walk.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ async function walk (dir, options = {}) {
4444
if (stat) {
4545
ctx.stat = await fsp[stat](entry);
4646
}
47-
if (dirent.isDirectory() && await dirAction(entry, ctx) !== 'skip') {
48-
await walker(entry);
47+
if (dirent.isDirectory()) {
48+
if (await dirAction(entry, ctx) !== 'skip') {
49+
await walker(entry);
50+
}
4951
} else if (dirent.isFile()) {
5052
fileAction && await fileAction(entry, ctx);
5153
} else if (dirent.isSymbolicLink()) {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "action-walk",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "walk a directory tree performing actions",
55
"main": "action-walk.js",
66
"directories": {
@@ -28,8 +28,9 @@
2828
"action-walk": "bin/walk"
2929
},
3030
"devDependencies": {
31-
"mocha": "^8.1.3",
3231
"chai": "^4.2.0",
32+
"chai-as-promised": "^7.1.1",
33+
"mocha": "^8.1.3",
3334
"nyc": "^15.1.0"
3435
}
3536
}

test/basics.test.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ const fsp = require('fs').promises;
22
const p = require('path');
33
const {execCommandLine} = require('./utilities/exec');
44
const walk = require('../action-walk');
5-
const {expect} = require('chai');
5+
const chai = require('chai');
6+
const expect = chai.expect;
7+
chai.use(require('chai-as-promised'));
68

9+
// calculate expected results using stat, du, and find.
710
const testdir = '.';
811
let testdirStat;
912
const duOutput = {
@@ -59,7 +62,26 @@ describe('verify that action-walk works as expected', function () {
5962
})
6063
});
6164

62-
it.only('should count the correct number of directories, files, and links', function () {
65+
it('should work with no arguments other than a directory', function () {
66+
return walk('/dev');
67+
});
68+
69+
it('should reject if the argument is not a directory', function () {
70+
return expect(walk('./package.json')).eventually.rejected;
71+
})
72+
73+
it('should work with non-file, non-directory, non-link file types', function () {
74+
const options = {
75+
otherAction: () => options.own.other += 1,
76+
own: {other: 0},
77+
};
78+
return walk('/dev', options)
79+
.then(() => {
80+
expect(options.own.other).not.equal(0);
81+
});
82+
});
83+
84+
it('should count the correct number of directories, files, and links', function () {
6385
let dirCount = 0;
6486
let fileCount = 0;
6587
let linkCount = 0;
@@ -80,7 +102,7 @@ describe('verify that action-walk works as expected', function () {
80102
});
81103
});
82104

83-
it.only('du -ab totals should differ by targetsize - linksize using stat', function () {
105+
it('du -ab totals should differ by targetsize - linksize using stat', function () {
84106
let delta = 0;
85107
const options = {
86108
dirAction: (path, ctx) => ctx.own.total += ctx.stat.size,
@@ -111,7 +133,7 @@ describe('verify that action-walk works as expected', function () {
111133
})
112134
});
113135

114-
it.only('should match du -ab output using lstat without a linkAction', function () {
136+
it('should match du -ab output using lstat without a linkAction', function () {
115137
const options = {
116138
dirAction: (path, ctx) => ctx.own.total += ctx.stat.size,
117139
fileAction: (path, ctx) => ctx.own.total += ctx.stat.size,
@@ -128,7 +150,7 @@ describe('verify that action-walk works as expected', function () {
128150
})
129151
});
130152

131-
it.only('should match du -ab --exclude=node_modules', function () {
153+
it('should match du -ab --exclude=node_modules', function () {
132154
const options = {
133155
dirAction: (path, {dirent, stat, own}) => {
134156
if (own.skipDirs && own.skipDirs.indexOf(dirent.name) >= 0) {
@@ -150,10 +172,10 @@ describe('verify that action-walk works as expected', function () {
150172
})
151173
});
152174

153-
it.only('should execute recursively matching du -b', function () {
175+
it('should execute recursively matching du -b', function () {
154176
const own = {total: 0, linkCount: 0, dirTotals: {}, skipDirs: []};
155177
const options = {
156-
dirAction: daDirOnly,
178+
dirAction: daDirsOnly,
157179
fileAction: (path, ctx) => ctx.own.total += ctx.stat.size,
158180
own,
159181
stat: 'lstat',
@@ -171,17 +193,17 @@ describe('verify that action-walk works as expected', function () {
171193
it('should execute recursively matching du -b --exclude=node_modules', function () {
172194
const own = {total: 0, linkCount: 0, dirTotals: {}, skipDirs: ['node_modules']};
173195
const options = {
174-
dirAction: daDirOnly,
196+
dirAction: daDirsOnly,
175197
fileAction: (path, ctx) => ctx.own.total += ctx.stat.size,
176198
own,
177199
stat: 'lstat',
178200
};
179201

180202
return walk(testdir, options)
181203
.then(() => {
182-
expect(own.total + testdirStat.size).equal(duOutput.w_node[testdir]);
204+
expect(own.total + testdirStat.size).equal(duOutput.wo_node[testdir]);
183205
for (const dir in own.dirTotals) {
184-
expect(own.dirTotals[dir]).equal(duOutput.w_node[`${dir}`]);
206+
expect(own.dirTotals[dir]).equal(duOutput.wo_node[`${dir}`]);
185207
}
186208
});
187209
});
@@ -193,15 +215,15 @@ describe('verify that action-walk works as expected', function () {
193215
// utilities
194216
//
195217

196-
async function daDirOnly (path, ctx) {
218+
async function daDirsOnly (path, ctx) {
197219
const {dirent, stat, own} = ctx;
198220
if (own.skipDirs && own.skipDirs.indexOf(dirent.name) >= 0) {
199221
return 'skip';
200222
}
201223
own.dirTotals[path] = 0;
202224
const newown = {total: 0, dirTotals: own.dirTotals};
203225
const options = {
204-
dirAction: daDirOnly,
226+
dirAction: daDirsOnly,
205227
fileAction: (path, ctx) => ctx.own.total += ctx.stat.size,
206228
own: newown,
207229
stat: 'lstat',

0 commit comments

Comments
 (0)