4
4
'use strict' ;
5
5
6
6
const fs = require ( 'fs' ) ;
7
+ const net = require ( 'net' ) ;
7
8
const path = require ( 'path' ) ;
8
9
const http = require ( 'http' ) ;
9
- const net = require ( 'net' ) ;
10
10
const assert = require ( 'assert' ) ;
11
- const findit = require ( 'findit' ) ;
12
- const hashish = require ( 'hashish' ) ;
13
11
14
- const common = require ( '../common' ) ;
15
12
const Formidable = require ( '../../src/index' ) ;
16
13
14
+ const PORT = 13532 ;
15
+ const CWD = process . cwd ( ) ;
16
+ const FIXTURES_PATH = path . join ( CWD , 'test' , 'fixture' , 'js' ) ;
17
+ const FIXTURES_HTTP = path . join ( CWD , 'test' , 'fixture' , 'http' ) ;
18
+ const UPLOAD_DIR = path . join ( CWD , 'test' , 'tmp' ) ;
19
+
17
20
const server = http . createServer ( ) ;
18
- server . listen ( common . port , findFixtures ) ;
21
+ server . listen ( PORT , findFixtures ) ;
19
22
20
23
function findFixtures ( ) {
21
- const fixtures = [ ] ;
22
- findit . sync ( path . join ( common . dir . fixture , 'js' ) ) . forEach ( ( jsPath ) => {
23
- if ( ! / \. j s $ / . test ( jsPath ) || / w o r k a r o u n d s / . test ( jsPath ) ) return ;
24
-
25
- const group = path . basename ( jsPath , '.js' ) ;
26
- hashish . forEach ( require ( jsPath ) , ( fixture , name ) => {
27
- fixtures . push ( {
28
- name : `${ group } /${ name } ` ,
29
- fixture,
24
+ const results = fs
25
+ . readdirSync ( FIXTURES_PATH )
26
+ . filter ( ( x ) => / \. j s $ / . test ( x ) && ! / w o r k a r o u n d s / . test ( x ) )
27
+ . reduce ( ( acc , fp ) => {
28
+ const group = path . basename ( fp , '.js' ) ;
29
+ const filepath = path . join ( FIXTURES_PATH , fp ) ;
30
+ const mod = require ( filepath ) ;
31
+
32
+ Object . keys ( mod ) . forEach ( ( k ) => {
33
+ Object . keys ( mod [ k ] ) . forEach ( ( _fixture ) => {
34
+ acc . push ( {
35
+ fixture : mod [ k ] ,
36
+ name : path . join ( group , k ) ,
37
+ } ) ;
38
+ } ) ;
30
39
} ) ;
31
- } ) ;
32
- } ) ;
33
40
34
- testNext ( fixtures ) ;
41
+ return acc ;
42
+ } , [ ] ) ;
43
+
44
+ testNext ( results ) ;
35
45
}
36
46
37
- function testNext ( fixtures ) {
38
- let fixture = fixtures . shift ( ) ;
47
+ function testNext ( results ) {
48
+ let fixture = results . shift ( ) ;
39
49
if ( ! fixture ) {
40
50
server . close ( ) ;
41
51
return ;
42
52
}
43
53
const fixtureName = fixture . name ;
44
-
45
54
fixture = fixture . fixture ;
46
55
47
56
uploadFixture ( fixtureName , ( err , parts ) => {
@@ -62,15 +71,13 @@ function testNext(fixtures) {
62
71
}
63
72
} ) ;
64
73
65
- testNext ( fixtures ) ;
74
+ testNext ( results ) ;
66
75
} ) ;
67
76
}
68
77
69
78
function uploadFixture ( fixtureName , cb ) {
70
79
server . once ( 'request' , ( req , res ) => {
71
- const form = new Formidable ( ) ;
72
- form . uploadDir = common . dir . tmp ;
73
- form . hash = 'sha1' ;
80
+ const form = new Formidable ( { uploadDir : UPLOAD_DIR , hash : 'sha1' } ) ;
74
81
form . parse ( req ) ;
75
82
76
83
function callback ( ...args ) {
@@ -96,8 +103,8 @@ function uploadFixture(fixtureName, cb) {
96
103
} ) ;
97
104
} ) ;
98
105
99
- const socket = net . createConnection ( common . port ) ;
100
- const fixturePath = path . join ( common . dir . fixture , 'http' , fixtureName ) ;
106
+ const socket = net . createConnection ( PORT ) ;
107
+ const fixturePath = path . join ( FIXTURES_HTTP , fixtureName ) ;
101
108
const file = fs . createReadStream ( fixturePath ) ;
102
109
103
110
file . pipe ( socket , { end : false } ) ;
0 commit comments