Skip to content

Commit 206736e

Browse files
committed
add test to verify that $PYTHONPATH or $EBPYTHONPREFIXES are set correctly by generated module file
1 parent 9813bc8 commit 206736e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/framework/toy_build.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4238,6 +4238,69 @@ def test_eb_error(self):
42384238
stderr = stderr.getvalue()
42394239
self.assertTrue(regex.search(stderr), f"Pattern '{regex.pattern}' should be found in {stderr}")
42404240

4241+
def test_toy_python(self):
4242+
"""
4243+
Test whether $PYTHONPATH or $EBPYTHONPREFIXES are set correctly.
4244+
"""
4245+
# generate fake Python module that we can use as runtime dependency for toy
4246+
# (required condition for use of $EBPYTHONPREFIXES)
4247+
fake_mods_path = os.path.join(self.test_prefix, 'modules')
4248+
fake_python_mod = os.path.join(fake_mods_path, 'Python', '3.6')
4249+
if get_module_syntax() == 'Lua':
4250+
fake_python_mod += '.lua'
4251+
write_file(fake_python_mod, '')
4252+
else:
4253+
write_file(fake_python_mod, '#%Module')
4254+
self.modtool.use(fake_mods_path)
4255+
4256+
test_ecs = os.path.join(os.path.dirname(__file__), 'easyconfigs', 'test_ecs')
4257+
toy_ec = os.path.join(test_ecs, 't', 'toy', 'toy-0.0.eb')
4258+
4259+
test_ec_txt = read_file(toy_ec)
4260+
test_ec_txt += "\npostinstallcmds.append('mkdir -p %(installdir)s/lib/python3.6/site-packages')"
4261+
test_ec_txt += "\npostinstallcmds.append('touch %(installdir)s/lib/python3.6/site-packages/foo.py')"
4262+
4263+
test_ec = os.path.join(self.test_prefix, 'test.eb')
4264+
write_file(test_ec, test_ec_txt)
4265+
self.run_test_toy_build_with_output(ec_file=test_ec)
4266+
4267+
toy_mod = os.path.join(self.test_installpath, 'modules', 'all', 'toy', '0.0')
4268+
if get_module_syntax() == 'Lua':
4269+
toy_mod += '.lua'
4270+
toy_mod_txt = read_file(toy_mod)
4271+
4272+
pythonpath_regex = re.compile('^prepend.path.*PYTHONPATH.*lib/python3.6/site-packages', re.M)
4273+
4274+
self.assertTrue(pythonpath_regex.search(toy_mod_txt),
4275+
f"Pattern '{pythonpath_regex.pattern}' found in: {toy_mod_txt}")
4276+
4277+
# also check when opting in to use $EBPYTHONPREFIXES instead of $PYTHONPATH
4278+
args = ['--prefer-python-search-path=EBPYTHONPREFIXES']
4279+
self.run_test_toy_build_with_output(ec_file=test_ec, extra_args=args)
4280+
toy_mod_txt = read_file(toy_mod)
4281+
# if Python is not listed as a runtime dependency then $PYTHONPATH is still used,
4282+
# because the Python dependency used must be aware of $EBPYTHONPREFIXES
4283+
# (see sitecustomize.py installed by Python easyblock)
4284+
self.assertTrue(pythonpath_regex.search(toy_mod_txt),
4285+
f"Pattern '{pythonpath_regex.pattern}' found in: {toy_mod_txt}")
4286+
4287+
test_ec_txt += "\ndependencies = [('Python', '3.6', '', SYSTEM)]"
4288+
write_file(test_ec, test_ec_txt)
4289+
self.run_test_toy_build_with_output(ec_file=test_ec, extra_args=args)
4290+
toy_mod_txt = read_file(toy_mod)
4291+
4292+
ebpythonprefixes_regex = re.compile('^prepend.path.*EBPYTHONPREFIXES.*root', re.M)
4293+
self.assertTrue(ebpythonprefixes_regex.search(toy_mod_txt),
4294+
f"Pattern '{ebpythonprefixes_regex.pattern}' found in: {toy_mod_txt}")
4295+
4296+
test_ec_txt += "\nforce_pythonpath = True"
4297+
write_file(test_ec, test_ec_txt)
4298+
self.run_test_toy_build_with_output(ec_file=test_ec)
4299+
toy_mod_txt = read_file(toy_mod)
4300+
4301+
self.assertTrue(pythonpath_regex.search(toy_mod_txt),
4302+
f"Pattern '{pythonpath_regex.pattern}' found in: {toy_mod_txt}")
4303+
42414304

42424305
def suite():
42434306
""" return all the tests in this file """

0 commit comments

Comments
 (0)