def test_helpers_python_library(self): mylogger = """ value = "" def append(data): global value value += data """ temp = temp_folder() save(os.path.join(temp, "myconanlogger.py"), mylogger) conanfile = "import myconanlogger" temp1 = temp_folder() save(os.path.join(temp1, "conanfile.py"), conanfile) temp2 = temp_folder() save(os.path.join(temp2, "conanfile.py"), conanfile) try: sys.path.append(temp) loaded1, _ = _parse_conanfile(os.path.join(temp1, "conanfile.py")) loaded2, _ = _parse_conanfile(os.path.join(temp2, "conanfile.py")) self.assertIs(loaded1.myconanlogger, loaded2.myconanlogger) self.assertIs(loaded1.myconanlogger.value, loaded2.myconanlogger.value) finally: sys.path.remove(temp)
def _do_actual_test(self, scm_data, after_scm, after_recipe): target_conanfile = self.conanfile.format(url=scm_data['url'], revision=scm_data['revision'], after_scm=after_scm, after_recipe=after_recipe) save(self.conanfile_path, content=self.conanfile.format(url='auto', revision='auto', after_scm=after_scm, after_recipe=after_recipe)) scm_data = SCMData(conanfile=namedtuple('_', 'scm')(scm=scm_data)) _replace_scm_data_in_conanfile(self.conanfile_path, scm_data) self.assertEqual(load(self.conanfile_path), target_conanfile) _parse_conanfile(conan_file_path=self.conanfile_path) # Check that the resulting file is valid python code.
def _create_and_load(myfunc, value, subdir_name, add_subdir_init): subdir_content = textwrap.dedent(""" def get_value(): return {value} def {myfunc}(): return "{myfunc}" """) side_content = textwrap.dedent(""" def get_side_value(): return {value} def side_{myfunc}(): return "{myfunc}" """) conanfile = textwrap.dedent(""" import pickle from {subdir}.api import get_value, {myfunc} from file import get_side_value, side_{myfunc} from fractions import Fraction def conanfile_func(): return get_value(), {myfunc}(), get_side_value(), side_{myfunc}(), str(Fraction(1,1)) """) expected_return = (value, myfunc, value, myfunc, "1") tmp = temp_folder() with chdir(tmp): save( "conanfile.py", conanfile.format(value=value, myfunc=myfunc, subdir=subdir_name)) save("file.py", side_content.format(value=value, myfunc=myfunc)) save("{}/api.py".format(subdir_name), subdir_content.format(value=value, myfunc=myfunc)) if add_subdir_init: save("__init__.py", "") save("{}/__init__.py".format(subdir_name), "") loaded, module_id = _parse_conanfile(os.path.join(tmp, "conanfile.py")) return loaded, module_id, expected_return