@@ -356,4 +356,80 @@ private String getCommandLineAsString(CommandLine commandline) {
356
356
}
357
357
return result ;
358
358
}
359
+
360
+ public void testGetShebang () throws Exception {
361
+ ExecMojo execMojo = new ExecMojo ();
362
+
363
+ // without shebang
364
+ File noShebang = Files .createTempFile ("noShebang" , ".sh" ).toFile ();
365
+ Files .write (noShebang .toPath (), Arrays .asList ("echo hello" ), StandardCharsets .UTF_8 );
366
+ assertNull (execMojo .getShebang (noShebang ));
367
+ noShebang .delete ();
368
+
369
+ // with shebang
370
+ File withShebang = Files .createTempFile ("withShebang" , ".sh" ).toFile ();
371
+ Files .write (withShebang .toPath (), Arrays .asList ("#!/bin/bash -x" , "echo hello" ), StandardCharsets .UTF_8 );
372
+ assertEquals ("/bin/bash -x" , execMojo .getShebang (withShebang ));
373
+ withShebang .delete ();
374
+
375
+ // with shebang but no args
376
+ File withShebangNoArgs =
377
+ Files .createTempFile ("withShebangNoArgs" , ".sh" ).toFile ();
378
+ Files .write (
379
+ withShebangNoArgs .toPath (),
380
+ Arrays .asList ("#!/usr/bin/env python3" , "print('hello')" ),
381
+ StandardCharsets .UTF_8 );
382
+ assertEquals ("/usr/bin/env python3" , execMojo .getShebang (withShebangNoArgs ));
383
+ withShebangNoArgs .delete ();
384
+ }
385
+
386
+ public void testCreateEnvWrapperFile () throws Exception {
387
+ ExecMojo execMojo = new ExecMojo ();
388
+
389
+ // without shebang
390
+ File envScript = Files .createTempFile ("envScript" , ".sh" ).toFile ();
391
+ Files .write (envScript .toPath (), Arrays .asList ("export TEST_VAR=test_value" ), StandardCharsets .UTF_8 );
392
+ File wrapper = execMojo .createEnvWrapperFile (envScript );
393
+ List <String > lines = Files .readAllLines (wrapper .toPath (), StandardCharsets .UTF_8 );
394
+
395
+ if (OS .isFamilyWindows ()) {
396
+ assertEquals ("@echo off" , lines .get (0 ));
397
+ assertTrue (lines .get (1 ).startsWith ("call \" " ));
398
+ assertTrue (lines .get (1 ).endsWith (envScript .getCanonicalPath () + "\" " ));
399
+ assertEquals ("echo " + EnvStreamConsumer .START_PARSING_INDICATOR , lines .get (2 ));
400
+ assertEquals ("set" , lines .get (3 ));
401
+ } else {
402
+ assertEquals ("#!/bin/sh" , lines .get (0 ));
403
+ assertEquals (". " + envScript .getCanonicalPath (), lines .get (1 ));
404
+ assertEquals ("echo " + EnvStreamConsumer .START_PARSING_INDICATOR , lines .get (2 ));
405
+ assertEquals ("env" , lines .get (3 ));
406
+ }
407
+ wrapper .delete ();
408
+ envScript .delete ();
409
+
410
+ // with shebang
411
+ File envScriptWithShebang =
412
+ Files .createTempFile ("envScriptWithShebang" , ".sh" ).toFile ();
413
+ Files .write (
414
+ envScriptWithShebang .toPath (),
415
+ Arrays .asList ("#!/bin/bash" , "export TEST_VAR=test_value" ),
416
+ StandardCharsets .UTF_8 );
417
+ File wrapper2 = execMojo .createEnvWrapperFile (envScriptWithShebang );
418
+ List <String > lines2 = Files .readAllLines (wrapper2 .toPath (), StandardCharsets .UTF_8 );
419
+
420
+ if (OS .isFamilyWindows ()) {
421
+ assertEquals ("@echo off" , lines2 .get (0 ));
422
+ assertTrue (lines2 .get (1 ).startsWith ("call \" " ));
423
+ assertTrue (lines2 .get (1 ).endsWith (envScriptWithShebang .getCanonicalPath () + "\" " ));
424
+ assertEquals ("echo " + EnvStreamConsumer .START_PARSING_INDICATOR , lines2 .get (2 ));
425
+ assertEquals ("set" , lines2 .get (3 ));
426
+ } else {
427
+ assertEquals ("#!/bin/bash" , lines2 .get (0 ));
428
+ assertEquals (". " + envScriptWithShebang .getCanonicalPath (), lines2 .get (1 ));
429
+ assertEquals ("echo " + EnvStreamConsumer .START_PARSING_INDICATOR , lines2 .get (2 ));
430
+ assertEquals ("env" , lines2 .get (3 ));
431
+ }
432
+ wrapper2 .delete ();
433
+ envScriptWithShebang .delete ();
434
+ }
359
435
}
0 commit comments