def test_check_namespace(self): global global_module global_module = None self.assertTrue('global_module' in globals()) with Path(NamedTemporaryFile("w", suffix=".py", delete=False).name) as tf: tf.write_text('export = "anglerfish"') my_module1 = path2import(tf.as_posix(), 'global_module') global_module = my_module1 my_module2 = path2import(tf.as_posix(), 'global_module', check_namespace=True) self.assertEqual(my_module2, global_module) with Path(NamedTemporaryFile("w", suffix=".py", delete=False).name) as tf: tf.write_text('export = "anglerfish"') with self.assertRaises(ImportWarning): my_module3 = path2import(tf.as_posix(), name='os') print(my_module3) # to avoid warning "assigned but never used" my_module4 = path2import( tf.as_posix(), name='os', ignore_exceptions=True) is None print(my_module4) # to avoid warning "assigned but never used" del (global_module)
def test_reimport(): with TempFile('export = "anglerfish"') as tf: my_module1 = path2import(tf.name) assert my_module1.export == 'anglerfish' my_module2 = path2import(tf.name) assert my_module1.export == my_module2.export assert my_module1 == my_module2
def test_not_found(self): with self.assertRaises(ModuleNotFoundError): my_module = path2import('not_existed_module.py') print(my_module) # to avoid warning "assigned but never used" self.assertIsNone( path2import('not_existed_module.py', ignore_exceptions=True))
def test_syntax_error(): with TempFile('export = ') as tf: # invaild python statements with pytest.raises(SyntaxError): my_module = path2import(tf.name) print(my_module) # to avoid warning "assigned but never used" assert path2import(tf.name, ignore_exceptions=True) is None
def test_invaild_module(): with TempFile('export = "anglerfish"', suffix='.txt') as tf: # not a .py module with pytest.raises(ImportError): my_module = path2import(tf.name) print(my_module) # to avoid warning "assigned but never used" assert path2import(tf.name, ignore_exceptions=True) is None
def test_reimport(self): with Path(NamedTemporaryFile("w", suffix=".py", delete=False).name) as tf: tf.write_text('export = "anglerfish"') my_module1 = path2import(tf.as_posix()) self.assertEqual(my_module1.export, 'anglerfish') my_module2 = path2import(tf.as_posix()) self.assertEqual(my_module1.export, my_module2.export) self.assertEqual(my_module1, my_module2)
def test_invaild_module(self): with Path(NamedTemporaryFile("w", suffix=".txt", delete=False).name) as tf: tf.write_text('export = "anglerfish"') # Not a .py module. with self.assertRaises(ImportError): my_module = path2import(tf.as_posix()) print(my_module) # to avoid warning "assigned but never used" self.assertIsNone( path2import(tf.as_posix(), ignore_exceptions=True))
def test_syntax_error(self): with Path(NamedTemporaryFile("w", suffix=".py", delete=False).name) as tf: tf.write_text('export = ') with self.assertRaises(SyntaxError): my_module = path2import(tf.as_posix()) print(my_module) # to avoid warning "assigned but never used" self.assertIsNone( path2import(tf.as_posix(), ignore_exceptions=True))
def test_check_namespace(): global global_module global_module = None assert 'global_module' in globals() with TempFile('export = "anglerfish"') as tf: my_module1 = path2import(tf.name, 'global_module') global_module = my_module1 my_module2 = path2import(tf.name, 'global_module', check_namespace=True) assert my_module2 == global_module with TempFile('export = "anglerfish"') as tf: with pytest.raises(NamespaceConflictError): my_module3 = path2import(tf.name, name='os') print(my_module3) # to avoid warning "assigned but never used" my_module4 = path2import(tf.name, name='os', ignore_exceptions=True) == None print(my_module4) # to avoid warning "assigned but never used" del (global_module)
print(on_battery()) print("Running anglerfish.set_zip_comment()") ZipFile("test.zip", 'w').close() print(set_zip_comment("test.zip", "This is a comment.")) print("Running anglerfish.get_zip_comment()") print(get_zip_comment("test.zip")) print("Running anglerfish.set_display_off()") print(set_display_off()) print("Running anglerfish.path2import()") with open("test.py", "w", encoding="utf-8") as fyle: fyle.write("module_content = 42\n") modulote = path2import("test.py", "modulote") print(modulote) print(modulote.module_content) print("Running anglerfish.get_random_pastelight_color()") print(get_random_pastelight_color()) print("Running anglerfish.get_random_pasteldark_color()") print(get_random_pasteldark_color()) print("Running anglerfish.get_random_pastel_color()") print(get_random_pastel_color()) print("Running anglerfish.get_random_handwriting_font()") print(get_random_handwriting_font())
def test_not_found(): with pytest.raises(not_found_exception): my_module = path2import('not_existed_module.py') print(my_module) # to avoid warning "assigned but never used" assert path2import('not_existed_module.py', ignore_exceptions=True) is None
def test_normal(): with TempFile('export = "anglerfish"') as tf: my_module = path2import(tf.name) assert my_module.export == 'anglerfish'