1+ var fs = require ( 'fs' ) ;
2+ var path = require ( 'path' ) ;
3+
14var assert = require ( 'chai' ) . assert ;
25var rewire = require ( 'rewire' ) ;
36
@@ -6,44 +9,117 @@ var Plugin = rewire('../lib/plugin');
69var h = rewire ( '../lib/helper' ) ;
710
811describe ( 'plugin' , function ( ) {
9- var leetcode = new Plugin ( 0 , 'Leetcode' , '2.0' , '' ) ;
10- var cache = new Plugin ( 1 , 'Cache' , '1.0' , '' ) ;
11- var retry = new Plugin ( 2 , 'Retry' , '3.0' , '' ) ;
12- var core = new Plugin ( 3 , 'Core' , '4.0' , '' ) ;
13-
1412 before ( function ( ) {
1513 log . init ( ) ;
16-
17- var noop = function ( ) { } ;
18- cache . init = noop ;
19- leetcode . init = noop ;
20- retry . init = noop ;
21- core . init = noop ;
22-
23- h . getCodeDirData = function ( ) {
24- return [
25- { name : 'cache' , data : cache } ,
26- { name : 'leetcode' , data : leetcode } ,
27- { name : 'retry' , data : retry } ,
28- { name : 'bad' , data : null }
29- ] ;
30- } ;
31- Plugin . __set__ ( 'h' , h ) ;
3214 } ) ;
3315
34- it ( 'should init ok' , function ( ) {
35- assert . deepEqual ( Plugin . plugins , [ ] ) ;
36- Plugin . init ( core ) ;
37- assert . deepEqual ( Plugin . plugins . length , 3 ) ;
16+ describe ( '#init' , function ( ) {
17+ var leetcode = new Plugin ( 0 , 'Leetcode' , '2.0' , '' ) ;
18+ var cache = new Plugin ( 1 , 'Cache' , '1.0' , '' ) ;
19+ var retry = new Plugin ( 2 , 'Retry' , '3.0' , '' ) ;
20+ var core = new Plugin ( 3 , 'Core' , '4.0' , '' ) ;
21+
22+ before ( function ( ) {
23+ var noop = function ( ) { } ;
24+ cache . init = noop ;
25+ leetcode . init = noop ;
26+ retry . init = noop ;
27+ core . init = noop ;
28+
29+ h . getCodeDirData = function ( ) {
30+ return [
31+ { name : 'cache' , data : cache } ,
32+ { name : '.leetcode' , data : leetcode } , // disabled
33+ { name : 'retry' , data : retry } ,
34+ { name : 'bad' , data : null }
35+ ] ;
36+ } ;
37+ Plugin . __set__ ( 'h' , h ) ;
38+ } ) ;
39+
40+ it ( 'should init ok' , function ( ) {
41+ assert . deepEqual ( Plugin . plugins , [ ] ) ;
42+ Plugin . init ( core ) ;
43+ assert . deepEqual ( Plugin . plugins . length , 3 ) ;
44+
45+ var names = Plugin . plugins . map ( function ( p ) { return p . name ; } ) ;
46+ assert . deepEqual ( names , [ 'Retry' , 'Cache' , 'Leetcode' ] ) ;
3847
39- var names = Plugin . plugins . map ( function ( p ) {
40- return p . name ;
48+ assert . equal ( core . next , retry ) ;
49+ assert . equal ( retry . next , cache ) ;
50+ assert . equal ( cache . next , null ) ;
51+ assert . equal ( leetcode . next , null ) ;
4152 } ) ;
42- assert . deepEqual ( names , [ 'Retry' , 'Cache' , 'Leetcode' ] ) ;
53+ } ) ; // #init
4354
44- assert . equal ( core . next , retry ) ;
45- assert . equal ( retry . next , cache ) ;
46- assert . equal ( cache . next , leetcode ) ;
47- assert . equal ( leetcode . next , null ) ;
55+ describe ( '#install' , function ( ) {
56+ var expect ;
57+ before ( function ( ) {
58+ var cp = {
59+ exec : function ( cmd , opts , cb ) {
60+ expect = cmd ;
61+ return cb ( ) ;
62+ }
63+ } ;
64+ Plugin . __set__ ( 'cp' , cp ) ;
65+ } ) ;
66+
67+ it ( 'should install no deps ok' , function ( done ) {
68+ expect = '' ;
69+ var plugin = new Plugin ( 100 , 'test' , '2017.12.26' , 'desc' , [ ] ) ;
70+ plugin . install ( function ( ) {
71+ assert . equal ( expect , '' ) ;
72+ done ( ) ;
73+ } ) ;
74+ } ) ;
75+
76+ it ( 'should install deps ok' , function ( done ) {
77+ var deps = [ 'a' , 'b:linux' , 'b:darwin' , 'b:win32' , 'c:bad' , 'd' ] ;
78+ var plugin = new Plugin ( 100 , 'test' , '2017.12.26' , 'desc' , deps ) ;
79+ plugin . install ( function ( ) {
80+ assert . equal ( expect , 'npm install --save a b d' ) ;
81+ done ( ) ;
82+ } ) ;
83+ } ) ;
84+ } ) ; // #install
85+
86+ describe ( '#copy' , function ( ) {
87+ var src = path . resolve ( './tmp/copy.src.js' ) ;
88+ var dst = path . resolve ( './tmp/copy.test.js' ) ;
89+
90+ function clean ( ) {
91+ if ( fs . existsSync ( src ) ) fs . unlinkSync ( src ) ;
92+ if ( fs . existsSync ( dst ) ) fs . unlinkSync ( dst ) ;
93+ h . getPluginFile = function ( ) { return dst ; } ;
94+ }
95+
96+ beforeEach ( clean ) ;
97+ after ( clean ) ;
98+
99+ it ( 'should copy from http error' , function ( done ) {
100+ this . timeout ( 5000 ) ;
101+ Plugin . copy ( 'non-exists' , function ( e , fullpath ) {
102+ assert . equal ( e , 'HTTP Error: 404' ) ;
103+ assert . equal ( fs . existsSync ( dst ) , false ) ;
104+ done ( ) ;
105+ } ) ;
106+ } ) ;
107+
108+ it ( 'should copy from local ok' , function ( done ) {
109+ var data = [
110+ 'module.exports = {' ,
111+ ' x: 123,' ,
112+ ' install: function(cb) { cb(); }' ,
113+ '};'
114+ ] ;
115+ fs . writeFileSync ( src , data . join ( '\n' ) ) ;
116+
117+ Plugin . install ( src , function ( e , plugin ) {
118+ assert . notExists ( e ) ;
119+ assert . equal ( plugin . x , 123 ) ;
120+ assert . equal ( fs . existsSync ( dst ) , true ) ;
121+ done ( ) ;
122+ } ) ;
123+ } ) ;
48124 } ) ;
49125} ) ;
0 commit comments