Skip to content

Commit ae801ed

Browse files
committed
Fixed Stat file type tests
1 parent 02fcf4b commit ae801ed

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

lib/Stat.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,49 +62,49 @@ class Stat {
6262
* Checks if file.
6363
*/
6464
isFile (): bool {
65-
return !!(this.mode & constants.S_IFREG);
65+
return (this.mode & constants.S_IFMT) == constants.S_IFREG;
6666
}
6767

6868
/**
6969
* Checks if directory.
7070
*/
7171
isDirectory (): bool {
72-
return !!(this.mode & constants.S_IFDIR);
72+
return (this.mode & constants.S_IFMT) == constants.S_IFDIR;
7373
}
7474

7575
/**
7676
* Checks if block device.
7777
*/
7878
isBlockDevice (): bool {
79-
return !!(this.mode & constants.S_IFBLK);
79+
return (this.mode & constants.S_IFMT) == constants.S_IFBLK;
8080
}
8181

8282
/**
8383
* Checks if character device.
8484
*/
8585
isCharacterDevice (): bool {
86-
return !!(this.mode & constants.S_IFCHR);
86+
return (this.mode & constants.S_IFMT) == constants.S_IFCHR;
8787
}
8888

8989
/**
9090
* Checks if symbolic link.
9191
*/
9292
isSymbolicLink (): bool {
93-
return !!(this.mode & constants.S_IFLNK);
93+
return (this.mode & constants.S_IFMT) == constants.S_IFLNK;
9494
}
9595

9696
/**
9797
* Checks if FIFO.
9898
*/
9999
isFIFO (): bool {
100-
return !!(this.mode & constants.S_IFIFO);
100+
return (this.mode & constants.S_IFMT) == constants.S_IFIFO;
101101
}
102102

103103
/**
104104
* Checks if socket.
105105
*/
106106
isSocket (): bool {
107-
return !!(this.mode & constants.S_IFSOCK);
107+
return (this.mode & constants.S_IFMT) == constants.S_IFSOCK;
108108
}
109109

110110
}

test/VirtualFS.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,50 @@ test.cb('asynchronous errors are passed to callbacks - async', t => {
114114
});
115115
});
116116

117+
///////////////
118+
// stat type //
119+
///////////////
120+
121+
test('file stat makes sense - sync', t => {
122+
const fs = new VirtualFS;
123+
fs.writeFileSync('/test', 'test data');
124+
const stat = fs.statSync('/test');
125+
t.true(stat.isFile());
126+
t.false(stat.isDirectory());
127+
t.false(stat.isBlockDevice());
128+
t.false(stat.isCharacterDevice());
129+
t.false(stat.isSocket());
130+
t.false(stat.isSymbolicLink());
131+
t.false(stat.isFIFO());
132+
});
133+
134+
test('dir stat makes sense - sync', t => {
135+
const fs = new VirtualFS;
136+
fs.mkdirSync('/dir');
137+
const stat = fs.statSync('/dir');
138+
t.false(stat.isFile());
139+
t.true(stat.isDirectory());
140+
t.false(stat.isBlockDevice());
141+
t.false(stat.isCharacterDevice());
142+
t.false(stat.isSocket());
143+
t.false(stat.isSymbolicLink());
144+
t.false(stat.isFIFO());
145+
});
146+
147+
test('symlink stat makes sense - sync', t => {
148+
const fs = new VirtualFS;
149+
fs.writeFileSync('/a', 'data');
150+
fs.symlinkSync('/a', '/link-to-a');
151+
const stat = fs.lstatSync('/link-to-a');
152+
t.false(stat.isFile());
153+
t.false(stat.isDirectory());
154+
t.false(stat.isBlockDevice());
155+
t.false(stat.isCharacterDevice());
156+
t.false(stat.isSocket());
157+
t.true(stat.isSymbolicLink());
158+
t.false(stat.isFIFO());
159+
});
160+
117161
///////////
118162
// files //
119163
///////////

0 commit comments

Comments
 (0)