@@ -2937,6 +2937,7 @@ def test_fix_shebang(self):
29372937 # copy of bin/toy to use in fix_python_shebang_for and fix_perl_shebang_for
29382938 " 'cp -a %(installdir)s/bin/toy %(installdir)s/bin/toy.python'," ,
29392939 " 'cp -a %(installdir)s/bin/toy %(installdir)s/bin/toy.perl'," ,
2940+ " 'cp -a %(installdir)s/bin/toy %(installdir)s/bin/toy.sh'," ,
29402941
29412942 # hardcoded path to bin/python
29422943 " 'echo \" #!/usr/bin/python\\ n# test\" > %(installdir)s/bin/t1.py'," ,
@@ -2973,9 +2974,26 @@ def test_fix_shebang(self):
29732974 # shebang bash
29742975 " 'echo \" #!/usr/bin/env bash\\ n# test\" > %(installdir)s/bin/b2.sh'," ,
29752976
2977+ # tests for bash shebang
2978+ # hardcoded path to bin/bash
2979+ " 'echo \" #!/bin/bash\\ n# test\" > %(installdir)s/bin/t1.sh'," ,
2980+ # hardcoded path to usr/bin/bash
2981+ " 'echo \" #!/usr/bin/bash\\ n# test\" > %(installdir)s/bin/t2.sh'," ,
2982+ # already OK, should remain the same
2983+ " 'echo \" #!/usr/bin/env bash\\ n# test\" > %(installdir)s/bin/t3.sh'," ,
2984+ # shebang with space, should strip the space
2985+ " 'echo \" #! /usr/bin/env bash\\ n# test\" > %(installdir)s/bin/t4.sh'," ,
2986+ # no shebang sh
2987+ " 'echo \" # test\" > %(installdir)s/bin/t5.sh'," ,
2988+ # shebang python
2989+ " 'echo \" #!/usr/bin/env python\\ n# test\" > %(installdir)s/bin/b1.py'," ,
2990+ # shebang perl
2991+ " 'echo \" #!/usr/bin/env perl\\ n# test\" > %(installdir)s/bin/b1.pl'," ,
2992+
29762993 "]" ,
2977- "fix_python_shebang_for = ['bin/t1.py', 'bin/*.py', 'nosuchdir/*.py', 'bin/toy.python', 'bin/b1.sh']" ,
2978- "fix_perl_shebang_for = ['bin/*.pl', 'bin/b2.sh', 'bin/toy.perl']" ,
2994+ "fix_python_shebang_for = ['bin/t1.py', 'bin/t*.py', 'nosuchdir/*.py', 'bin/toy.python', 'bin/b1.sh']" ,
2995+ "fix_perl_shebang_for = ['bin/t*.pl', 'bin/b2.sh', 'bin/toy.perl']" ,
2996+ "fix_bash_shebang_for = ['bin/t*.sh', 'bin/b1.py', 'bin/b1.pl', 'bin/toy.sh']" ,
29792997 ])
29802998 write_file (test_ec , test_ec_txt )
29812999 self .test_toy_build (ec_file = test_ec , raise_error = True )
@@ -2984,36 +3002,32 @@ def test_fix_shebang(self):
29843002
29853003 # bin/toy and bin/toy2 should *not* be patched, since they're binary files
29863004 toy_txt = read_file (os .path .join (toy_bindir , 'toy' ), mode = 'rb' )
2987- for fn in ['toy.perl' , 'toy.python' ]:
3005+ for fn in ['toy.sh' , 'toy. perl' , 'toy.python' ]:
29883006 fn_txt = read_file (os .path .join (toy_bindir , fn ), mode = 'rb' )
29893007 # no shebang added
29903008 self .assertFalse (fn_txt .startswith (b"#!/" ))
29913009 # exact same file as original binary (untouched)
29923010 self .assertEqual (toy_txt , fn_txt )
29933011
3012+ regexes = { }
29943013 # no re.M, this should match at start of file!
2995- py_shebang_regex = re .compile (r'^#!/usr/bin/env python\n# test$' )
2996- for pybin in ['t1.py' , 't2.py' , 't3.py' , 't4.py' , 't5.py' , 't6.py' , 't7.py' ]:
2997- pybin_path = os .path .join (toy_bindir , pybin )
2998- pybin_txt = read_file (pybin_path )
2999- self .assertTrue (py_shebang_regex .match (pybin_txt ),
3000- "Pattern '%s' found in %s: %s" % (py_shebang_regex .pattern , pybin_path , pybin_txt ))
3014+ regexes ['py' ] = re .compile (r'^#!/usr/bin/env python\n# test$' )
3015+ regexes ['pl' ] = re .compile (r'^#!/usr/bin/env perl\n# test$' )
3016+ regexes ['sh' ] = re .compile (r'^#!/usr/bin/env bash\n# test$' )
30013017
3002- # no re.M, this should match at start of file!
3003- perl_shebang_regex = re .compile (r'^#!/usr/bin/env perl\n# test$' )
3004- for perlbin in ['t1.pl' , 't2.pl' , 't3.pl' , 't4.pl' , 't5.pl' , 't6.pl' , 't7.pl' ]:
3005- perlbin_path = os .path .join (toy_bindir , perlbin )
3006- perlbin_txt = read_file (perlbin_path )
3007- self .assertTrue (perl_shebang_regex .match (perlbin_txt ),
3008- "Pattern '%s' found in %s: %s" % (perl_shebang_regex .pattern , perlbin_path , perlbin_txt ))
3009-
3010- # There are 2 bash files which shouldn't be influenced by fix_shebang
3011- bash_shebang_regex = re .compile (r'^#!/usr/bin/env bash\n# test$' )
3012- for bashbin in ['b1.sh' , 'b2.sh' ]:
3013- bashbin_path = os .path .join (toy_bindir , bashbin )
3014- bashbin_txt = read_file (bashbin_path )
3015- self .assertTrue (bash_shebang_regex .match (bashbin_txt ),
3016- "Pattern '%s' found in %s: %s" % (bash_shebang_regex .pattern , bashbin_path , bashbin_txt ))
3018+
3019+ # all scripts should have a shebang that matches their extension
3020+ scripts = { }
3021+ scripts ['py' ] = ['t1.py' , 't2.py' , 't3.py' , 't4.py' , 't5.py' , 't6.py' , 't7.py' , 'b1.py' ]
3022+ scripts ['pl' ] = ['t1.pl' , 't2.pl' , 't3.pl' , 't4.pl' , 't5.pl' , 't6.pl' , 't7.pl' , 'b1.pl' ]
3023+ scripts ['sh' ] = ['t1.sh' , 't2.sh' , 't3.sh' , 't4.sh' , 't5.sh' , 'b1.sh' , 'b2.sh' ]
3024+
3025+ for ext in ['sh' , 'pl' , 'py' ]:
3026+ for script in scripts [ext ]:
3027+ bin_path = os .path .join (toy_bindir , script )
3028+ bin_txt = read_file (bin_path )
3029+ self .assertTrue (regexes [ext ].match (bin_txt ),
3030+ "Pattern '%s' found in %s: %s" % (regexes [ext ].pattern , bin_path , bin_txt ))
30173031
30183032 # now test with a custom env command
30193033 extra_args = ['--env-for-shebang=/usr/bin/env -S' ]
@@ -3023,36 +3037,31 @@ def test_fix_shebang(self):
30233037
30243038 # bin/toy and bin/toy2 should *not* be patched, since they're binary files
30253039 toy_txt = read_file (os .path .join (toy_bindir , 'toy' ), mode = 'rb' )
3026- for fn in ['toy.perl' , 'toy.python' ]:
3040+ for fn in ['toy.sh' , 'toy. perl' , 'toy.python' ]:
30273041 fn_txt = read_file (os .path .join (toy_bindir , fn ), mode = 'rb' )
30283042 # no shebang added
30293043 self .assertFalse (fn_txt .startswith (b"#!/" ))
30303044 # exact same file as original binary (untouched)
30313045 self .assertEqual (toy_txt , fn_txt )
30323046
3047+ regexes_S = { }
30333048 # no re.M, this should match at start of file!
3034- py_shebang_regex = re .compile (r'^#!/usr/bin/env -S python\n# test$' )
3035- for pybin in ['t1.py' , 't2.py' , 't3.py' , 't4.py' , 't5.py' , 't6.py' , 't7.py' ]:
3036- pybin_path = os .path .join (toy_bindir , pybin )
3037- pybin_txt = read_file (pybin_path )
3038- self .assertTrue (py_shebang_regex .match (pybin_txt ),
3039- "Pattern '%s' found in %s: %s" % (py_shebang_regex .pattern , pybin_path , pybin_txt ))
3049+ regexes_S ['py' ] = re .compile (r'^#!/usr/bin/env -S python\n# test$' )
3050+ regexes_S ['pl' ] = re .compile (r'^#!/usr/bin/env -S perl\n# test$' )
3051+ regexes_S ['sh' ] = re .compile (r'^#!/usr/bin/env -S bash\n# test$' )
3052+
3053+ for ext in ['sh' , 'pl' , 'py' ]:
3054+ for script in scripts [ext ]:
3055+ bin_path = os .path .join (toy_bindir , script )
3056+ bin_txt = read_file (bin_path )
3057+ # the scripts b1.py, b1.pl, b1.sh, b2.sh should keep their original shebang
3058+ if script .startswith ('b' ):
3059+ self .assertTrue (regexes [ext ].match (bin_txt ),
3060+ "Pattern '%s' found in %s: %s" % (regexes [ext ].pattern , bin_path , bin_txt ))
3061+ else :
3062+ self .assertTrue (regexes_S [ext ].match (bin_txt ),
3063+ "Pattern '%s' found in %s: %s" % (regexes_S [ext ].pattern , bin_path , bin_txt ))
30403064
3041- # no re.M, this should match at start of file!
3042- perl_shebang_regex = re .compile (r'^#!/usr/bin/env -S perl\n# test$' )
3043- for perlbin in ['t1.pl' , 't2.pl' , 't3.pl' , 't4.pl' , 't5.pl' , 't6.pl' , 't7.pl' ]:
3044- perlbin_path = os .path .join (toy_bindir , perlbin )
3045- perlbin_txt = read_file (perlbin_path )
3046- self .assertTrue (perl_shebang_regex .match (perlbin_txt ),
3047- "Pattern '%s' found in %s: %s" % (perl_shebang_regex .pattern , perlbin_path , perlbin_txt ))
3048-
3049- # There are 2 bash files which shouldn't be influenced by fix_shebang
3050- bash_shebang_regex = re .compile (r'^#!/usr/bin/env bash\n# test$' )
3051- for bashbin in ['b1.sh' , 'b2.sh' ]:
3052- bashbin_path = os .path .join (toy_bindir , bashbin )
3053- bashbin_txt = read_file (bashbin_path )
3054- self .assertTrue (bash_shebang_regex .match (bashbin_txt ),
3055- "Pattern '%s' found in %s: %s" % (bash_shebang_regex .pattern , bashbin_path , bashbin_txt ))
30563065
30573066 def test_toy_system_toolchain_alias (self ):
30583067 """Test use of 'system' toolchain alias."""
0 commit comments