add test for lua and node patching in sbang (#5169)

* add test for lua and node handling in sbang patching (cf #5086)
This commit is contained in:
healther 2017-08-23 10:05:36 +02:00 committed by Todd Gamblin
parent 40e9171390
commit 15186d4ae8

View file

@ -30,6 +30,7 @@
import pytest import pytest
import tempfile import tempfile
import shutil import shutil
import filecmp
from llnl.util.filesystem import * from llnl.util.filesystem import *
@ -38,12 +39,16 @@
from spack.util.executable import which from spack.util.executable import which
short_line = "#!/this/is/short/bin/bash\n" short_line = "#!/this/is/short/bin/bash\n"
long_line = "#!/this/" + ('x' * 200) + "/is/long\n" long_line = "#!/this/" + ('x' * 200) + "/is/long\n"
lua_line = "#!/this/" + ('x' * 200) + "/is/lua\n" lua_line = "#!/this/" + ('x' * 200) + "/is/lua\n"
lua_line_patched = "--!/this/" + ('x' * 200) + "/is/lua\n" lua_in_text = ("line\n") * 100 + "lua\n" + ("line\n" * 100)
sbang_line = '#!/bin/bash %s/bin/sbang\n' % spack.spack_root lua_line_patched = "--!/this/" + ('x' * 200) + "/is/lua\n"
last_line = "last!\n" node_line = "#!/this/" + ('x' * 200) + "/is/node\n"
node_in_text = ("line\n") * 100 + "lua\n" + ("line\n" * 100)
node_line_patched = "//!/this/" + ('x' * 200) + "/is/node\n"
sbang_line = '#!/bin/bash %s/bin/sbang\n' % spack.spack_root
last_line = "last!\n"
class ScriptDirectory(object): class ScriptDirectory(object):
@ -72,6 +77,26 @@ def __init__(self):
f.write(lua_line) f.write(lua_line)
f.write(last_line) f.write(last_line)
# Lua script with long shebang
self.lua_textbang = os.path.join(self.tempdir, 'lua_in_text')
with open(self.lua_textbang, 'w') as f:
f.write(short_line)
f.write(lua_in_text)
f.write(last_line)
# Node script with long shebang
self.node_shebang = os.path.join(self.tempdir, 'node')
with open(self.node_shebang, 'w') as f:
f.write(node_line)
f.write(last_line)
# Node script with long shebang
self.node_textbang = os.path.join(self.tempdir, 'node_in_text')
with open(self.node_textbang, 'w') as f:
f.write(short_line)
f.write(node_in_text)
f.write(last_line)
# Script already using sbang. # Script already using sbang.
self.has_sbang = os.path.join(self.tempdir, 'shebang') self.has_sbang = os.path.join(self.tempdir, 'shebang')
with open(self.has_sbang, 'w') as f: with open(self.has_sbang, 'w') as f:
@ -123,6 +148,17 @@ def test_shebang_handling(script_dir):
assert f.readline() == lua_line_patched assert f.readline() == lua_line_patched
assert f.readline() == last_line assert f.readline() == last_line
# Make sure this got patched.
with open(script_dir.node_shebang, 'r') as f:
assert f.readline() == sbang_line
assert f.readline() == node_line_patched
assert f.readline() == last_line
assert filecmp.cmp(script_dir.lua_textbang,
os.path.join(script_dir.tempdir, 'lua_in_text'))
assert filecmp.cmp(script_dir.node_textbang,
os.path.join(script_dir.tempdir, 'node_in_text'))
# Make sure this is untouched # Make sure this is untouched
with open(script_dir.has_sbang, 'r') as f: with open(script_dir.has_sbang, 'r') as f:
assert f.readline() == sbang_line assert f.readline() == sbang_line