@@ -220,3 +220,67 @@ describe('validateCommandData', () => {
220220 ) . toBe ( true ) ;
221221 } ) ;
222222} ) ;
223+
224+ describe ( 'runScript' , ( ) => {
225+ const originalArgv = process . argv ;
226+ const originalExit = process . exit ;
227+ const originalConsoleError = console . error ;
228+ const originalResourcesPath = process . resourcesPath ;
229+
230+ let exitMock : jest . Mock ;
231+ let consoleErrorMock : jest . Mock ;
232+
233+ beforeEach ( ( ) => {
234+ jest . resetModules ( ) ;
235+ // @ts -ignore this is fine for tests
236+ process . resourcesPath = '/resources' ;
237+ jest . mock ( './plugin-management' , ( ) => ( {
238+ defaultPluginsDir : jest . fn ( ( ) => '/plugins/default' ) ,
239+ defaultUserPluginsDir : jest . fn ( ( ) => '/plugins/user' ) ,
240+ } ) ) ;
241+
242+ exitMock = jest . fn ( ) as any ;
243+ // @ts -expect-error overriding for test
244+ process . exit = exitMock ;
245+ consoleErrorMock = jest . fn ( ) ;
246+ console . error = consoleErrorMock ;
247+ } ) ;
248+
249+ afterEach ( ( ) => {
250+ process . argv = originalArgv ;
251+ process . exit = originalExit ;
252+ console . error = originalConsoleError ;
253+ // @ts -ignore
254+ process . resourcesPath = originalResourcesPath ;
255+ jest . unmock ( './plugin-management' ) ;
256+ jest . restoreAllMocks ( ) ;
257+ } ) ;
258+
259+ const testScriptImport = async ( scriptPath : string ) => {
260+ process . argv = [ 'node' , scriptPath ] ;
261+ jest . doMock ( scriptPath , ( ) => ( { } ) , { virtual : true } ) ;
262+ const runCmdModule = await import ( './runCmd' ) ;
263+ await runCmdModule . runScript ( ) ;
264+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
265+ } ;
266+
267+ it ( 'imports the script when path is inside defaultPluginsDir' , ( ) =>
268+ testScriptImport ( '/plugins/default/my-script.js' ) ) ;
269+
270+ it ( 'imports the script when path is inside defaultUserPluginsDir' , ( ) =>
271+ testScriptImport ( '/plugins/user/my-script.js' ) ) ;
272+
273+ it ( 'imports the script when path is inside static .plugins dir' , ( ) =>
274+ testScriptImport ( '/resources/.plugins/my-script.js' ) ) ;
275+
276+ it ( 'exits with error when script is outside allowed directories' , async ( ) => {
277+ process . argv = [ 'node' , '/not-allowed/my-script.js' ] ;
278+ jest . doMock ( '/not-allowed/my-script.js' , ( ) => ( { } ) , { virtual : true } ) ;
279+
280+ const runCmdModule = await import ( './runCmd' ) ;
281+ await runCmdModule . runScript ( ) ;
282+
283+ expect ( consoleErrorMock ) . toHaveBeenCalledTimes ( 1 ) ;
284+ expect ( exitMock ) . toHaveBeenCalledWith ( 1 ) ;
285+ } ) ;
286+ } ) ;
0 commit comments