1
1
/* eslint-disable no-octal */
2
2
3
3
var
4
- vows = require ( 'vows' ) ,
5
4
assert = require ( 'assert' ) ,
6
5
7
6
path = require ( 'path' ) ,
8
7
fs = require ( 'fs' ) ,
9
8
existsSync = fs . existsSync || path . existsSync ,
10
-
11
- tmp = require ( '../lib/tmp.js' ) ,
12
- Test = require ( './base.js' ) ;
9
+ tmp = require ( '../lib/tmp.js' ) ;
13
10
14
11
15
12
function _testDir ( mode ) {
@@ -23,98 +20,124 @@ function _testDir(mode) {
23
20
} ;
24
21
}
25
22
26
- vows . describe ( 'Synchronous directory creation' ) . addBatch ( {
27
- 'when using without parameters' : {
28
- topic : function ( ) {
29
- return tmp . dirSync ( ) ;
30
- } ,
31
23
32
- 'should return with a name' : Test . assertNameSync ,
33
- 'should be a directory' : _testDir ( 040700 ) ,
34
- 'should have the default prefix' : Test . testPrefixSync ( 'tmp-' )
35
- } ,
24
+ function assertName ( name , expected ) {
25
+ assert . ok ( typeof name == 'string' ) ;
26
+ assert . ok ( name . length > 0 , 'an empty string is not a valid name' ) ;
27
+ if ( expected ) {
28
+ assert . equal ( path . basename ( name ) , expected , 'should be the expected name' ) ;
29
+ }
30
+ }
36
31
37
- 'when using with prefix' : {
38
- topic : function ( ) {
39
- return tmp . dirSync ( { prefix : 'something' } ) ;
40
- } ,
41
32
42
- 'should return with a name' : Test . assertNameSync ,
43
- 'should be a directory' : _testDir ( 040700 ) ,
44
- 'should have the provided prefix' : Test . testPrefixSync ( 'something' )
45
- } ,
33
+ function assertStat ( stat , mode ) {
34
+ // getuid() and getgid() do not exist on Windows.
35
+ if ( process . getuid ) {
36
+ assert . equal ( stat . uid , process . getuid ( ) , 'should have the same UID' ) ;
37
+ }
38
+ if ( process . getgid ) {
39
+ // FIXME does not always work as expected
40
+ assert . equal ( stat . gid , process . getgid ( ) , 'should have the same GUID' ) ;
41
+ }
42
+ // mode values do not work properly on Windows. Ignore “group” and
43
+ // “other” bits then. Ignore execute bit on that platform because it
44
+ // doesn’t exist—even for directories.
45
+ if ( process . platform == 'win32' ) {
46
+ assert . equal ( stat . mode & 0666600 , mode & 0666600 ) ;
47
+ } else {
48
+ assert . equal ( stat . mode , mode ) ;
49
+ }
50
+ }
46
51
47
- 'when using with postfix' : {
48
- topic : function ( ) {
49
- return tmp . dirSync ( { postfix : '.txt' } ) ;
50
- } ,
51
52
52
- 'should return with a name' : Test . assertNameSync ,
53
- 'should be a directory' : _testDir ( 040700 ) ,
54
- 'should have the provided postfix' : Test . testPostfixSync ( '.txt' )
55
- } ,
53
+ function assertPrefix ( name , prefix ) {
54
+ assert . equal ( path . basename ( name ) . slice ( 0 , prefix . length ) , prefix , 'should have the provided prefix' ) ;
55
+ }
56
56
57
- 'when using template' : {
58
- topic : function ( ) {
59
- return tmp . dirSync ( { template : path . join ( tmp . tmpdir , 'clike-XXXXXX-postfix' ) } ) ;
60
- } ,
61
57
62
- 'should return with a name' : Test . assertNameSync ,
63
- 'should be a directory' : _testDir ( 040700 ) ,
64
- 'should have the provided prefix' : Test . testPrefixSync ( 'clike-' ) ,
65
- 'should have the provided postfix' : Test . testPostfixSync ( '-postfix' )
66
- } ,
58
+ function assertPostfix ( name , postfix ) {
59
+ assert . equal ( name . slice ( name . length - postfix . length , name . length ) , postfix , 'should have the provided postfix' ) ;
60
+ }
67
61
68
- 'when using name' : {
69
- topic : function ( ) {
70
- return tmp . dirSync ( { name : 'using-name' } ) ;
71
- } ,
72
62
73
- 'should return with a name' : Test . assertNameSync ,
74
- 'should have the provided name' : Test . testNameSync ( path . join ( tmp . tmpdir , 'using-name' ) ) ,
75
- 'should be a directory' : function ( result ) {
76
- _testDir ( 040700 ) ( result ) ;
77
- result . removeCallback ( ) ;
78
- assert . ok ( ! existsSync ( result . name ) , 'Directory should be removed' ) ;
79
- }
80
- } ,
81
63
82
- 'when using multiple options' : {
83
- topic : function ( ) {
84
- return tmp . dirSync ( { prefix : 'foo' , postfix : 'bar' , mode : 0750 } ) ;
85
- } ,
64
+ function _prepareInbandTests ( testOpts , opts ) {
65
+ var opts = opts || { } ;
66
+ var testOpts = testOpts || { } ;
86
67
87
- 'should return with a name' : Test . assertNameSync ,
88
- 'should be a directory' : _testDir ( 040750 ) ,
89
- 'should have the provided prefix' : Test . testPrefixSync ( 'foo' ) ,
90
- 'should have the provided postfix' : Test . testPostfixSync ( 'bar' )
91
- } ,
68
+ return function ( ) {
69
+ var result = null ;
92
70
93
- 'when using multiple options and mode' : {
94
- topic : function ( ) {
95
- return tmp . dirSync ( { prefix : 'complicated' , postfix : 'options' , mode : 0755 } ) ;
96
- } ,
71
+ before ( function ( ) {
72
+ result = tmp . dirSync ( opts ) ;
73
+ } ) ;
97
74
98
- 'should return with a name' : Test . assertNameSync ,
99
- 'should be a directory' : _testDir ( 040755 ) ,
100
- 'should have the provided prefix' : Test . testPrefixSync ( 'complicated' ) ,
101
- 'should have the provided postfix' : Test . testPostfixSync ( 'options' )
102
- } ,
75
+ it ( 'should return a proper result' , function ( ) {
76
+ assert . ok ( result != undefined ) ;
77
+ assert . ok ( result . name != undefined ) ;
78
+ assert . ok ( result . fd == undefined ) ;
79
+ assert . ok ( typeof result . removeCallback == 'function' ) ;
80
+ } ) ;
103
81
104
- 'no tries' : {
105
- topic : function ( ) {
106
- try {
107
- return tmp . dirSync ( { tries : - 1 } ) ;
108
- }
109
- catch ( e ) {
110
- return e ;
111
- }
112
- } ,
82
+ it ( 'temporary directory should exist' , function ( ) {
83
+ var stat = fs . statSync ( result . name ) ;
84
+ assert . ok ( stat . isDirectory ( ) ) ;
85
+ } ) ;
86
+
87
+ it ( 'temporary directory should have the expected stats and mode' , function ( ) {
88
+ var stat = fs . statSync ( result . name ) ;
89
+ assertStat ( stat , testOpts . mode || opts . mode ) ;
90
+ } ) ;
113
91
114
- 'should return with an error' : function ( topic ) {
115
- assert . instanceOf ( topic , Error ) ;
92
+ if ( opts . prefix || opts . testPrefix ) {
93
+ it ( 'should have the expected prefix' , function ( ) {
94
+ assertPrefix ( result . name , testOpts . prefix || opts . prefix ) ;
95
+ } ) ;
116
96
}
117
- } ,
97
+
98
+ if ( opts . postfix || opts . testPostfix ) {
99
+ it ( 'should have the expected postfix' , function ( ) {
100
+ assertPostfix ( result . name , testOpts . postfix || opts . postfix ) ;
101
+ } ) ;
102
+ }
103
+
104
+ it ( 'should have a valid name' , function ( ) {
105
+ assertName ( result . name , opts . name ) ;
106
+ } ) ;
107
+
108
+ it ( 'should have a working removeCallback' , function ( ) {
109
+ result . removeCallback ( ) ;
110
+ assert . ok ( ! existsSync ( result . name ) ) ;
111
+ } ) ;
112
+ } ;
113
+ }
114
+
115
+
116
+ describe ( 'tmp' , function ( ) {
117
+ describe ( '#dirSync()' , function ( ) {
118
+ describe ( 'without any parameters' , _prepareInbandTests ( { mode : 040700 , prefix : 'tmp-' } ) ) ;
119
+ describe ( 'with prefix' , _prepareInbandTests ( { mode : 040700 } , { prefix : 'something' } ) ) ;
120
+ describe ( 'with postfix' , _prepareInbandTests ( { mode : 040700 } , { postfix : '.txt' } ) ) ;
121
+ describe ( 'with template' , _prepareInbandTests ( { mode : 040700 , prefix : 'clike-' , postfix : '.txt' } , { template : 'clike-XXXXXX-postfix' } ) ) ;
122
+ describe ( 'with name' , _prepareInbandTests ( { mode : 040700 } , { name : 'using-name' } ) ) ;
123
+ describe ( 'with mode' , _prepareInbandTests ( null , { mode : 0755 } ) ) ;
124
+ describe ( 'with multiple options' , _prepareInbandTests ( null , { prefix : 'foo' , postfix : 'bar' , mode : 0750 } ) ) ;
125
+ describe ( 'with invalid tries' , function ( ) {
126
+ it ( 'should result in an error' , function ( ) {
127
+ try {
128
+ tmp . dirSync ( { tries : - 1 } ) ;
129
+ assert . fail ( 'should have failed' ) ;
130
+ } catch ( e ) {
131
+ assert . ok ( e instanceof Error ) ;
132
+ }
133
+ } ) ;
134
+ } ) ;
135
+ } ) ;
136
+ } ) ;
137
+
138
+
139
+ /*
140
+ vows.describe('Synchronous directory creation').addBatch({
118
141
119
142
'keep testing': {
120
143
topic: function () {
@@ -217,16 +240,5 @@ vows.describe('Synchronous directory creation').addBatch({
217
240
fs.rmdirSync(name);
218
241
}
219
242
},
220
-
221
- 'remove callback' : {
222
- topic : function ( ) {
223
- return tmp . dirSync ( ) ;
224
- } ,
225
-
226
- 'should return with a name' : Test . assertNameSync ,
227
- 'removeCallback should remove directory' : function ( result ) {
228
- result . removeCallback ( ) ;
229
- assert . ok ( ! existsSync ( result . name ) , 'Directory should be removed' ) ;
230
- }
231
- }
232
243
}).exportTo(module);
244
+ */
0 commit comments