@@ -50,18 +50,77 @@ export interface HarnessFileMatchers {
50
50
readonly size : jasmine . Matchers < number > ;
51
51
}
52
52
53
+ /**
54
+ * Add a Jasmine expectation filter to an expectation that always fails with a message.
55
+ * @param base The base expectation (`expect(...)`) to use.
56
+ * @param message The message to provide in the expectation failure.
57
+ */
58
+ function createFailureExpectation < T > ( base : T , message : string ) : T {
59
+ // Needed typings are not included in the Jasmine types
60
+ const expectation = base as T & {
61
+ expector : {
62
+ addFilter ( filter : {
63
+ selectComparisonFunc ( ) : ( ) => { pass : boolean ; message : string } ;
64
+ } ) : typeof expectation . expector ;
65
+ } ;
66
+ } ;
67
+ expectation . expector = expectation . expector . addFilter ( {
68
+ selectComparisonFunc ( ) {
69
+ return ( ) => ( {
70
+ pass : false ,
71
+ message,
72
+ } ) ;
73
+ } ,
74
+ } ) ;
75
+
76
+ return expectation ;
77
+ }
78
+
53
79
export function expectFile < T > ( path : string , harness : BuilderHarness < T > ) : HarnessFileMatchers {
54
80
return {
55
- toExist : ( ) => expect ( harness . hasFile ( path ) ) . toBe ( true , 'Expected file to exist: ' + path ) ,
56
- toNotExist : ( ) =>
57
- expect ( harness . hasFile ( path ) ) . toBe ( false , 'Expected file to not exist: ' + path ) ,
81
+ toExist ( ) {
82
+ const exists = harness . hasFile ( path ) ;
83
+ expect ( exists ) . toBe ( true , 'Expected file to exist: ' + path ) ;
84
+
85
+ return exists ;
86
+ } ,
87
+ toNotExist ( ) {
88
+ const exists = harness . hasFile ( path ) ;
89
+ expect ( exists ) . toBe ( false , 'Expected file to not exist: ' + path ) ;
90
+
91
+ return ! exists ;
92
+ } ,
58
93
get content ( ) {
59
- return expect ( harness . readFile ( path ) ) . withContext ( `With file content for '${ path } '` ) ;
94
+ try {
95
+ return expect ( harness . readFile ( path ) ) . withContext ( `With file content for '${ path } '` ) ;
96
+ } catch ( e ) {
97
+ if ( e . code !== 'ENOENT' ) {
98
+ throw e ;
99
+ }
100
+
101
+ // File does not exist so always fail the expectation
102
+ return createFailureExpectation (
103
+ expect ( '' ) ,
104
+ `Expected file content but file does not exist: '${ path } '` ,
105
+ ) ;
106
+ }
60
107
} ,
61
108
get size ( ) {
62
- return expect ( Buffer . byteLength ( harness . readFile ( path ) ) ) . withContext (
63
- `With file size for '${ path } '` ,
64
- ) ;
109
+ try {
110
+ return expect ( Buffer . byteLength ( harness . readFile ( path ) ) ) . withContext (
111
+ `With file size for '${ path } '` ,
112
+ ) ;
113
+ } catch ( e ) {
114
+ if ( e . code !== 'ENOENT' ) {
115
+ throw e ;
116
+ }
117
+
118
+ // File does not exist so always fail the expectation
119
+ return createFailureExpectation (
120
+ expect ( 0 ) ,
121
+ `Expected file size but file does not exist: '${ path } '` ,
122
+ ) ;
123
+ }
65
124
} ,
66
125
} ;
67
126
}
0 commit comments