@@ -4238,6 +4238,70 @@ 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 modules 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+ for pyver in ('2.7' , '3.6' ):
4249+ fake_python_mod = os .path .join (fake_mods_path , 'Python' , pyver )
4250+ if get_module_syntax () == 'Lua' :
4251+ fake_python_mod += '.lua'
4252+ write_file (fake_python_mod , '' )
4253+ else :
4254+ write_file (fake_python_mod , '#%Module' )
4255+ self .modtool .use (fake_mods_path )
4256+
4257+ test_ecs = os .path .join (os .path .dirname (__file__ ), 'easyconfigs' , 'test_ecs' )
4258+ toy_ec = os .path .join (test_ecs , 't' , 'toy' , 'toy-0.0.eb' )
4259+
4260+ test_ec_txt = read_file (toy_ec )
4261+ test_ec_txt += "\n postinstallcmds.append('mkdir -p %(installdir)s/lib/python3.6/site-packages')"
4262+ test_ec_txt += "\n postinstallcmds.append('touch %(installdir)s/lib/python3.6/site-packages/foo.py')"
4263+
4264+ test_ec = os .path .join (self .test_prefix , 'test.eb' )
4265+ write_file (test_ec , test_ec_txt )
4266+ self .run_test_toy_build_with_output (ec_file = test_ec )
4267+
4268+ toy_mod = os .path .join (self .test_installpath , 'modules' , 'all' , 'toy' , '0.0' )
4269+ if get_module_syntax () == 'Lua' :
4270+ toy_mod += '.lua'
4271+ toy_mod_txt = read_file (toy_mod )
4272+
4273+ pythonpath_regex = re .compile ('^prepend.path.*PYTHONPATH.*lib/python3.6/site-packages' , re .M )
4274+
4275+ self .assertTrue (pythonpath_regex .search (toy_mod_txt ),
4276+ f"Pattern '{ pythonpath_regex .pattern } ' found in: { toy_mod_txt } " )
4277+
4278+ # also check when opting in to use $EBPYTHONPREFIXES instead of $PYTHONPATH
4279+ args = ['--prefer-python-search-path=EBPYTHONPREFIXES' ]
4280+ self .run_test_toy_build_with_output (ec_file = test_ec , extra_args = args )
4281+ toy_mod_txt = read_file (toy_mod )
4282+ # if Python is not listed as a runtime dependency then $PYTHONPATH is still used,
4283+ # because the Python dependency used must be aware of $EBPYTHONPREFIXES
4284+ # (see sitecustomize.py installed by Python easyblock)
4285+ self .assertTrue (pythonpath_regex .search (toy_mod_txt ),
4286+ f"Pattern '{ pythonpath_regex .pattern } ' found in: { toy_mod_txt } " )
4287+
4288+ # if Python is listed as runtime dependency, then $EBPYTHONPREFIXES is used if it's preferred
4289+ write_file (test_ec , test_ec_txt + "\n dependencies = [('Python', '3.6', '', SYSTEM)]" )
4290+ self .run_test_toy_build_with_output (ec_file = test_ec , extra_args = args )
4291+ toy_mod_txt = read_file (toy_mod )
4292+
4293+ ebpythonprefixes_regex = re .compile ('^prepend.path.*EBPYTHONPREFIXES.*root' , re .M )
4294+ self .assertTrue (ebpythonprefixes_regex .search (toy_mod_txt ),
4295+ f"Pattern '{ ebpythonprefixes_regex .pattern } ' found in: { toy_mod_txt } " )
4296+
4297+ # if Python is listed in multi_deps, then $EBPYTHONPREFIXES is used, even if it's not explicitely preferred
4298+ write_file (test_ec , test_ec_txt + "\n multi_deps = {'Python': ['2.7', '3.6']}" )
4299+ self .run_test_toy_build_with_output (ec_file = test_ec )
4300+ toy_mod_txt = read_file (toy_mod )
4301+
4302+ self .assertTrue (ebpythonprefixes_regex .search (toy_mod_txt ),
4303+ f"Pattern '{ ebpythonprefixes_regex .pattern } ' found in: { toy_mod_txt } " )
4304+
42414305
42424306def suite ():
42434307 """ return all the tests in this file """
0 commit comments