1
+ import * as chai from 'chai' ;
2
+ import * as chaiAspromised from 'chai-as-promised' ;
3
+ chai . use ( chaiAspromised ) ;
4
+
1
5
import { assert , expect } from 'chai' ;
2
6
import * as fs from 'fs' ;
3
7
import 'mocha' ;
4
8
import * as mockfs from 'mock-fs' ;
5
9
import * as path from 'path' ;
10
+ import Logger from '../util/logger' ;
6
11
import PackageJsonConfigReader , { ERRORS } from './package-json-config-reader' ;
7
12
8
13
describe ( 'PackageJsonConfigReader' , ( ) => {
9
14
const testDir = '/tmp/tests/' ;
10
15
const priorConfiguredFile = 'priorConfiguredFile.json' ;
11
16
const noConfigFile = 'noConfigFile.json' ;
12
17
const noFrontvueConfigFile = 'noFrontVueConfigFile.json' ;
18
+ const nonExistingFile = 'nonExistingConfigFile.json' ;
19
+ const readonlyConfigFile = 'readonlyConfigFile.json' ;
13
20
21
+ // Restore FileSystem
22
+ before ( mockfs . restore ) ;
23
+ // Mock FileSystem with dummy files
14
24
beforeEach ( ( ) => {
25
+ mockfs . restore ( ) ;
26
+ const noConfigContent = '{}' ;
27
+ const noFrontvueConfigContent = `{ "config": { "somePlugin": {} } }` ;
28
+ const withConfigContent = `{ "config": { "frontvue": { "key": "value" } } }` ;
15
29
mockfs ( {
16
30
[ testDir ] : {
17
- [ noConfigFile ] : `{}` ,
18
- [ noFrontvueConfigFile ] : `{
19
- "config": {
20
- "somePlugin": {}
21
- }
22
- }` ,
23
- [ priorConfiguredFile ] : `{
24
- "config": {
25
- "frontvue": {
26
- "key": "value"
27
- }
28
- }
29
- }` ,
31
+ [ noConfigFile ] : noConfigContent ,
32
+ [ noFrontvueConfigFile ] : noFrontvueConfigContent ,
33
+ [ priorConfiguredFile ] : withConfigContent ,
34
+ [ readonlyConfigFile ] : mockfs . file ( { content : withConfigContent , mode : 0o440 } ) ,
30
35
} ,
31
36
} ) ;
32
37
} ) ;
38
+ // Clean up
39
+ afterEach ( mockfs . restore ) ;
40
+ after ( async ( ) => {
41
+ return PackageJsonConfigReader ( 'frontvue' )
42
+ . then ( configReader => configReader . destroy ( ) ) ;
43
+ } ) ;
33
44
34
-
35
- it ( 'throws if no namespace is passed' , async ( ) => {
36
- await PackageJsonConfigReader ( undefined ) . catch ( ( error : Error ) => {
37
- expect ( error . message , ERRORS . NO_NAMESPACE ) ;
38
- } ) ;
45
+ it ( 'instantiates with no namespace' , ( ) => {
46
+ // Reading actual package.json from current folder
47
+ mockfs . restore ( ) ;
48
+ return expect ( PackageJsonConfigReader ( ) )
49
+ . to . eventually . have . all . keys ( 'destroy' , 'fetch' , 'update' ) ;
39
50
} ) ;
40
51
41
52
42
- it ( 'throws if namespace is not a string' , async ( ) => {
43
- await PackageJsonConfigReader ( 1 ) . catch ( ( error : Error ) => {
44
- expect ( error . message , ERRORS . NO_NAMESPACE ) ;
45
- } ) ;
53
+ it ( 'throws if namespace is not a string' , ( ) => {
54
+ // Reading actual package.json from current folder
55
+ mockfs . restore ( ) ;
56
+ return expect ( PackageJsonConfigReader ( 1 ) )
57
+ . to . be . rejectedWith ( ERRORS . INVALID_NAMESPACE ) ;
46
58
} ) ;
47
59
48
60
49
- it ( 'instantiates with no custom filepath' , async ( ) => {
61
+ it ( 'instantiates with no custom filepath' , ( ) => {
62
+ // Reading actual package.json from current folder
50
63
mockfs . restore ( ) ;
51
- expect ( await PackageJsonConfigReader ( 'frontvue' ) )
52
- . to . be . an ( 'object' )
53
- . to . have . all . keys ( 'destroy' , 'fetch' , 'update' ) ;
64
+ return expect ( PackageJsonConfigReader ( 'frontvue' ) )
65
+ . to . eventually . be . an ( 'object' )
66
+ . to . eventually . have . all . keys ( 'destroy' , 'fetch' , 'update' ) ;
54
67
} ) ;
55
68
56
69
57
70
it ( 'instantiates without any config object' , async ( ) => {
58
71
const filepath = path . join ( testDir , noConfigFile ) ;
59
72
const configReader = await PackageJsonConfigReader ( 'frontvue' , filepath ) ;
60
73
const config = await configReader . fetch ( ) ;
61
- const fileContents = await fs . readFile ( filepath , { encoding : 'utf-8' } , ( error , data ) => {
74
+ const fileContents = fs . readFile ( filepath , { encoding : 'utf-8' } , ( error , data ) => {
62
75
expect ( JSON . parse ( data ) ) . to . eql ( { config : { frontvue : { } } } ) ;
63
76
} ) ;
64
- } ) ;
77
+ } ) . timeout ( 12000 ) ;
65
78
66
79
67
80
it ( 'instantiates without Frontvue config object' , async ( ) => {
@@ -71,21 +84,21 @@ describe('PackageJsonConfigReader', () => {
71
84
const fileContents = await fs . readFile ( filepath , { encoding : 'utf-8' } , ( error , data ) => {
72
85
expect ( JSON . parse ( data ) ) . to . eql ( { config : { somePlugin : { } , frontvue : { } } } ) ;
73
86
} ) ;
74
- } ) ;
87
+ } ) . timeout ( 12000 ) ;
75
88
76
89
77
90
it ( 'instantiates with prior Frontvue config object' , async ( ) => {
78
91
const filepath = path . join ( testDir , priorConfiguredFile ) ;
79
92
const configReader = await PackageJsonConfigReader ( 'frontvue' , filepath ) ;
80
- expect ( await configReader . fetch ( ) ) . to . eql ( { key : 'value' } ) ;
93
+ return expect ( configReader . fetch ( ) ) . to . eventually . eql ( { key : 'value' } ) ;
81
94
} ) ;
82
95
83
96
84
97
it ( 'updates config object' , async ( ) => {
85
98
const filepath = path . join ( testDir , noConfigFile ) ;
86
99
const configReader = await PackageJsonConfigReader ( 'frontvue' , filepath ) ;
87
100
const updated = await configReader . update ( { key : 'value' } ) ;
88
- expect ( await configReader . fetch ( ) ) . to . eql ( { key : 'value' } ) ;
101
+ return expect ( configReader . fetch ( ) ) . to . eventually . eql ( { key : 'value' } ) ;
89
102
} ) ;
90
103
91
104
@@ -97,5 +110,55 @@ describe('PackageJsonConfigReader', () => {
97
110
} ) ;
98
111
99
112
100
- afterEach ( mockfs . restore ) ;
113
+ it ( 'accepts custom logger' , ( ) => {
114
+ const filepath = path . join ( testDir , priorConfiguredFile ) ;
115
+ return expect (
116
+ PackageJsonConfigReader ( 'frontvue' , filepath , Logger ( 'frontvue' ) ( 'customLogger' ) ) ,
117
+ ) . to . eventually . not . throw ;
118
+ } ) . timeout ( 12000 ) ;
119
+
120
+
121
+ it ( 'rejects promise if filepath config file can\'t be read' , ( ) => {
122
+ return expect ( PackageJsonConfigReader ( 'frontvue' , path . join ( testDir , nonExistingFile ) ) )
123
+ . to . be . rejectedWith ( RegExp ( ERRORS . RW_ERROR ) ) ;
124
+ } ) . timeout ( 12000 ) ;
125
+
126
+
127
+ it ( 'rejects promise if refetching config from file fails' , async ( ) => {
128
+ const filepath = path . join ( testDir , priorConfiguredFile ) ;
129
+ const configReader = await PackageJsonConfigReader ( 'frontvue' , filepath ) ;
130
+ // We destroy the mocked fs and replace the file with something that can't be read
131
+ mockfs . restore ( ) ;
132
+ mockfs ( {
133
+ [ testDir ] : {
134
+ [ priorConfiguredFile ] : '' ,
135
+ } ,
136
+ } ) ;
137
+ return expect ( configReader . fetch ( ) ) . to . be . rejectedWith ( RegExp ( ERRORS . RW_ERROR ) ) ;
138
+ } ) . timeout ( 12000 ) ;
139
+
140
+
141
+ it ( 'rejects promise if destroy fails after initial config file fetch' , async ( ) => {
142
+ const filepath = path . join ( testDir , priorConfiguredFile ) ;
143
+ const configReader = await PackageJsonConfigReader ( 'frontvue' , filepath ) ;
144
+ // We destroy the mocked fs and recreate the file with something that can't be read
145
+ mockfs . restore ( ) ;
146
+ mockfs ( {
147
+ [ testDir ] : {
148
+ [ priorConfiguredFile ] : mockfs . file ( {
149
+ content : 'File content' ,
150
+ mode : 0o330 ,
151
+ } ) ,
152
+ } ,
153
+ } ) ;
154
+
155
+ return expect ( configReader . destroy ( ) ) . to . be . rejectedWith ( RegExp ( ERRORS . RW_ERROR ) ) ;
156
+ } ) ;
157
+
158
+
159
+ it ( 'rejects promise if trying to destroy from readonly file' , async ( ) => {
160
+ const filepath = path . join ( testDir , readonlyConfigFile ) ;
161
+ const configReader = await PackageJsonConfigReader ( 'frontvue' , filepath ) ;
162
+ return expect ( configReader . destroy ( ) ) . to . be . rejectedWith ( Error ) ;
163
+ } ) . timeout ( 12000 ) ;
101
164
} ) ;
0 commit comments