@@ -129,6 +129,74 @@ public async void InvokeFromStreamAsync_InvokesJavascript()
129
129
Assert . Equal ( dummyResultString , result . Result ) ;
130
130
}
131
131
132
+ [ Fact ( Timeout = _timeoutMS ) ]
133
+ public async void InvokeFromStreamAsync_LoadsRequiredModulesFromNodeModulesInProjectDirectory ( )
134
+ {
135
+ // Arrange
136
+ const string dummyCode = @"public string ExampleFunction(string arg)
137
+ {
138
+ // Example comment
139
+ return arg + ""dummyString"";
140
+ }" ;
141
+ HttpNodeJSService testSubject = CreateHttpNodeJSService ( projectPath : Directory . GetCurrentDirectory ( ) + "/../../../Javascript" ) ; // Current directory is <test project path>/bin/debug/<framework>
142
+
143
+ // Act
144
+ string result ;
145
+ using ( var memoryStream = new MemoryStream ( ) )
146
+ using ( var streamWriter = new StreamWriter ( memoryStream ) )
147
+ {
148
+ streamWriter . Write ( @"const prismjs = require('prismjs');
149
+ require('prismjs/components/prism-csharp');
150
+
151
+ module.exports = (callback, code) => {
152
+ var result = prismjs.highlight(code, prismjs.languages.csharp, 'csharp');
153
+
154
+ callback(null, result);
155
+ };" ) ;
156
+ streamWriter . Flush ( ) ;
157
+ memoryStream . Position = 0 ;
158
+
159
+ // Act
160
+ result = await testSubject . InvokeFromStreamAsync < string > ( memoryStream , args : new [ ] { dummyCode } ) . ConfigureAwait ( false ) ;
161
+ }
162
+
163
+ // Assert
164
+ const string expectedResult = @"<span class=""token keyword"">public</span> <span class=""token keyword"">string</span> <span class=""token function"">ExampleFunction</span><span class=""token punctuation"">(</span><span class=""token keyword"">string</span> arg<span class=""token punctuation"">)</span>
165
+ <span class=""token punctuation"">{</span>
166
+ <span class=""token comment"">// Example comment</span>
167
+ <span class=""token keyword"">return</span> arg <span class=""token operator"">+</span> <span class=""token string"">""dummyString""</span><span class=""token punctuation"">;</span>
168
+ <span class=""token punctuation"">}</span>" ;
169
+ Assert . Equal ( expectedResult , result ) ;
170
+ }
171
+
172
+ [ Fact ( Timeout = _timeoutMS ) ]
173
+ public async void InvokeFromStreamAsync_LoadsRequiredModuleFromFileInProjectDirectory ( )
174
+ {
175
+ // Arrange
176
+ HttpNodeJSService testSubject = CreateHttpNodeJSService ( projectPath : Directory . GetCurrentDirectory ( ) + "/../../../Javascript" ) ; // Current directory is <test project path>/bin/debug/<framework>
177
+
178
+ // Act
179
+ int result ;
180
+ using ( var memoryStream = new MemoryStream ( ) )
181
+ using ( var streamWriter = new StreamWriter ( memoryStream ) )
182
+ {
183
+ streamWriter . Write ( @"const value = require('./dummyReturnsValueModule.js');
184
+
185
+ module.exports = (callback) => {
186
+
187
+ callback(null, value);
188
+ };" ) ;
189
+ streamWriter . Flush ( ) ;
190
+ memoryStream . Position = 0 ;
191
+
192
+ // Act
193
+ result = await testSubject . InvokeFromStreamAsync < int > ( memoryStream ) . ConfigureAwait ( false ) ;
194
+ }
195
+
196
+ // Assert
197
+ Assert . Equal ( 10 , result ) ; // dummyReturnsValueModule.js just exports 10
198
+ }
199
+
132
200
[ Fact ( Timeout = _timeoutMS ) ]
133
201
public void InvokeFromStreamAsync_IsThreadSafe ( )
134
202
{
@@ -185,6 +253,54 @@ public async void InvokeFromStringAsync_InvokesJavascript()
185
253
Assert . Equal ( dummyResultString , result . Result ) ;
186
254
}
187
255
256
+ [ Fact ( Timeout = _timeoutMS ) ]
257
+ public async void InvokeFromStringAsync_LoadsRequiredModulesFromNodeModulesInProjectDirectory ( )
258
+ {
259
+ // Arrange
260
+ const string dummyCode = @"public string ExampleFunction(string arg)
261
+ {
262
+ // Example comment
263
+ return arg + ""dummyString"";
264
+ }" ;
265
+ HttpNodeJSService testSubject = CreateHttpNodeJSService ( projectPath : Directory . GetCurrentDirectory ( ) + "/../../../Javascript" ) ; // Current directory is <test project path>/bin/debug/<framework>
266
+
267
+ // Act
268
+ string result = await testSubject . InvokeFromStringAsync < string > ( @"const prismjs = require('prismjs');
269
+ require('prismjs/components/prism-csharp');
270
+
271
+ module.exports = (callback, code) => {
272
+ var result = prismjs.highlight(code, prismjs.languages.csharp, 'csharp');
273
+
274
+ callback(null, result);
275
+ };" , args : new [ ] { dummyCode } ) . ConfigureAwait ( false ) ;
276
+
277
+ // Assert
278
+ const string expectedResult = @"<span class=""token keyword"">public</span> <span class=""token keyword"">string</span> <span class=""token function"">ExampleFunction</span><span class=""token punctuation"">(</span><span class=""token keyword"">string</span> arg<span class=""token punctuation"">)</span>
279
+ <span class=""token punctuation"">{</span>
280
+ <span class=""token comment"">// Example comment</span>
281
+ <span class=""token keyword"">return</span> arg <span class=""token operator"">+</span> <span class=""token string"">""dummyString""</span><span class=""token punctuation"">;</span>
282
+ <span class=""token punctuation"">}</span>" ;
283
+ Assert . Equal ( expectedResult , result ) ;
284
+ }
285
+
286
+ [ Fact ( Timeout = _timeoutMS ) ]
287
+ public async void InvokeFromStringAsync_LoadsRequiredModuleFromFileInProjectDirectory ( )
288
+ {
289
+ // Arrange
290
+ HttpNodeJSService testSubject = CreateHttpNodeJSService ( projectPath : Directory . GetCurrentDirectory ( ) + "/../../../Javascript" ) ; // Current directory is <test project path>/bin/debug/<framework>
291
+
292
+ // Act
293
+ int result = await testSubject . InvokeFromStringAsync < int > ( @"const value = require('./dummyReturnsValueModule.js');
294
+
295
+ module.exports = (callback) => {
296
+
297
+ callback(null, value);
298
+ };" ) . ConfigureAwait ( false ) ;
299
+
300
+ // Assert
301
+ Assert . Equal ( 10 , result ) ; // dummyReturnsValueModule.js just exports 10
302
+ }
303
+
188
304
[ Fact ( Timeout = _timeoutMS ) ]
189
305
public void InvokeFromStringAsync_IsThreadSafe ( )
190
306
{
@@ -220,7 +336,7 @@ public void InvokeFromStringAsync_IsThreadSafe()
220
336
public async void InvokeFromFileAsync_InvokesJavascript ( )
221
337
{
222
338
const string dummyResultString = "success" ;
223
- HttpNodeJSService testSubject = CreateHttpNodeJSService ( ) ;
339
+ HttpNodeJSService testSubject = CreateHttpNodeJSService ( projectPath : Directory . GetCurrentDirectory ( ) + "/Javascript" ) ;
224
340
225
341
// Act
226
342
DummyResult result = await testSubject .
@@ -236,7 +352,7 @@ public void InvokeFromFileAsync_IsThreadSafe()
236
352
// Arrange
237
353
const string dummyModule = "dummyModule.js" ;
238
354
const string dummyResultString = "success" ;
239
- HttpNodeJSService testSubject = CreateHttpNodeJSService ( ) ;
355
+ HttpNodeJSService testSubject = CreateHttpNodeJSService ( projectPath : Directory . GetCurrentDirectory ( ) + "/Javascript" ) ;
240
356
241
357
// Act
242
358
var results = new ConcurrentQueue < DummyResult > ( ) ;
@@ -489,10 +605,15 @@ public static IEnumerable<object[]> AllInvokeMethods_ReceiveAndLogStderrOutput_D
489
605
/// <summary>
490
606
/// Specify <paramref name="loggerStringBuilder"/> for access to all logging output.
491
607
/// </summary>
492
- private HttpNodeJSService CreateHttpNodeJSService ( StringBuilder loggerStringBuilder = null )
608
+ private HttpNodeJSService CreateHttpNodeJSService ( StringBuilder loggerStringBuilder = null ,
609
+ string projectPath = null )
493
610
{
494
611
var services = new ServiceCollection ( ) ;
495
612
services . AddNodeJS ( ) ; // Default INodeService is HttpNodeService
613
+ if ( projectPath != null )
614
+ {
615
+ services . Configure < NodeJSProcessOptions > ( options => options . ProjectPath = projectPath ) ;
616
+ }
496
617
services . AddLogging ( lb =>
497
618
{
498
619
lb .
0 commit comments