@@ -5,32 +5,31 @@ const fs = require('fs');
55
66const test = require ( 'supertape' ) ;
77const tryToCatch = require ( 'try-to-catch' ) ;
8- const tryCatch = require ( 'try-catch ' ) ;
9- const { promisify } = require ( 'util' ) ;
8+ const { reRequire } = require ( 'mock-require ' ) ;
9+
1010const trammel = require ( '..' ) ;
11- const trammel_ = promisify ( trammel ) ;
1211
1312const fixturePath = path . join ( __dirname , 'fixture' ) ;
1413
1514test ( 'trammel: size of a file' , async ( t ) => {
1615 const expected = '12b' ;
17- const [ , size ] = await tryToCatch ( trammel_ , `${ fixturePath } /file.txt` ) ;
16+ const [ , size ] = await tryToCatch ( trammel , `${ fixturePath } /file.txt` ) ;
1817
1918 t . equal ( expected , size , 'should equal' ) ;
2019 t . end ( ) ;
2120} ) ;
2221
2322test ( 'trammel: size of a directory' , async ( t ) => {
2423 const expected = '12b' ;
25- const [ , size ] = await tryToCatch ( trammel_ , `${ fixturePath } /dir` ) ;
24+ const [ , size ] = await tryToCatch ( trammel , `${ fixturePath } /dir` ) ;
2625
2726 t . equal ( expected , size , 'should equal' ) ;
2827 t . end ( ) ;
2928} ) ;
3029
3130test ( 'trammel: size of a directory: empty dir: raw' , async ( t ) => {
3231 const expected = 0 ;
33- const [ , size ] = await tryToCatch ( trammel_ , `${ fixturePath } /empty-dir` , {
32+ const [ , size ] = await tryToCatch ( trammel , `${ fixturePath } /empty-dir` , {
3433 type : 'raw' ,
3534 } ) ;
3635
@@ -40,67 +39,78 @@ test('trammel: size of a directory: empty dir: raw', async (t) => {
4039
4140test ( 'trammel: stopOnError: false' , async ( t ) => {
4241 const expected = '0b' ;
43- const [ , size ] = await tryToCatch ( trammel_ , 'abcef' ) ;
42+ const [ , size ] = await tryToCatch ( trammel , 'abcef' ) ;
4443
4544 t . equal ( size , expected , 'should equal' ) ;
4645 t . end ( ) ;
4746} ) ;
4847
4948test ( 'trammel: error' , async ( t ) => {
50- const [ , size ] = await tryToCatch ( trammel_ , 'abcd' ) ;
49+ const [ , size ] = await tryToCatch ( trammel , 'abcd' ) ;
5150
5251 t . equal ( size , '0b' , 'should equal' ) ;
5352 t . end ( ) ;
5453} ) ;
5554
5655test ( 'trammel: stopOnError: true' , async ( t ) => {
57- const [ e ] = await tryToCatch ( trammel_ , 'abcd' , { stopOnError : true } ) ;
56+ const [ e ] = await tryToCatch ( trammel , 'abcd' , { stopOnError : true } ) ;
5857
5958 t . equal ( e . code , 'ENOENT' , 'should equal' ) ;
6059 t . end ( ) ;
6160} ) ;
6261
6362test ( 'trammel: stopOnError: true: can not readdir' , async ( t ) => {
6463 const error = Error ( 'hello' ) ;
65- const { readdir} = fs ;
64+ const { readdir} = fs . promises ;
65+
66+ fs . promises . readdir = async ( ) => {
67+ throw error ;
68+ } ;
6669
67- fs . readdir = ( dir , fn ) => fn ( error ) ;
70+ const trammel = reRequire ( '..' ) ;
6871
69- const [ e ] = await tryToCatch ( trammel_ , fixturePath , {
72+ const [ e ] = await tryToCatch ( trammel , fixturePath , {
7073 stopOnError : true ,
7174 } ) ;
7275
73- fs . readdir = readdir ;
76+ fs . promises . readdir = readdir ;
7477
7578 t . equal ( e . message , 'hello' , 'should equal' ) ;
7679 t . end ( ) ;
7780} ) ;
7881
79- test ( 'trammel: can not readdir' , async ( t ) => {
80- const expected = '0b' ;
81- const { readdir} = fs ;
82+ test ( 'trammel: readdir: empty' , async ( t ) => {
83+ const { readdir} = fs . promises ;
8284
83- fs . readdir = ( dir , fn ) => fn ( Error ( 'hi' ) ) ;
85+ fs . promises . readdir = async ( ) => [ ] ;
8486
85- const [ , size ] = await tryToCatch ( trammel_ , fixturePath ) ;
87+ const trammel = reRequire ( '..' ) ;
8688
87- fs . readdir = readdir ;
89+ const size = await trammel ( fixturePath , {
90+ type : 'raw' ,
91+ } ) ;
8892
89- t . equal ( size , expected , 'should equal' ) ;
90- t . end ( ) ;
91- } ) ;
92-
93- test ( 'trammel: arguments: no' , async ( t ) => {
94- const [ e ] = tryCatch ( trammel ) ;
93+ fs . promises . readdir = readdir ;
9594
96- t . equal ( e . message , 'dir could not be empty!' , 'should equal' ) ;
95+ t . equal ( size , 0 , 'should equal' ) ;
9796 t . end ( ) ;
9897} ) ;
9998
100- test ( 'trammel: arguments: no callback' , async ( t ) => {
101- const [ e ] = tryCatch ( trammel , '/' ) ;
99+ test ( 'trammel: can not readdir' , async ( t ) => {
100+ const expected = '0b' ;
101+ const { readdir} = fs . promises ;
102+
103+ fs . promises . readdir = ( ) => {
104+ throw Error ( 'hi' ) ;
105+ } ;
102106
103- t . equal ( e . message , 'callback could not be empty!' , 'should equal' ) ;
107+ const trammel = reRequire ( '..' ) ;
108+
109+ const [ , size ] = await tryToCatch ( trammel , fixturePath ) ;
110+
111+ fs . promises . readdir = readdir ;
112+
113+ t . equal ( size , expected , 'should equal' ) ;
104114 t . end ( ) ;
105115} ) ;
106116
0 commit comments