def test_exceptional_rules(self): rule_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_exceptional_rule.rst"), ] config = PackageGeneratorConfig() config.output_dir = self.output_dir config.os = "Linux" config.style_format = "pep8" config.dump = True config.target_version = "2.80" config.mod_version = None config.support_bge = False pkg_generator = PackageGenerator(config) analyzer = BaseAnalyzer() generator = BaseGenerator() rule = PackageGenerationRule("rule", rule_rst_files, analyzer, generator) pkg_generator.add_rule(rule) pkg_generator.generate() expect_files_dir = "{}/package_generator_test_exceptional_rule".format( self.data_dir) actual_files_dir = self.output_dir py_files = [ "module_exceptional.py", ] for file_ in py_files: expect_file_path = "{}/{}".format(expect_files_dir, file_) actual_file_path = "{}/{}".format(actual_files_dir, file_) with open(actual_file_path, "r") as f: self.log( "============= {} =============".format(actual_file_path)) self.log(f.read()) self.assertTrue(filecmp.cmp(expect_file_path, actual_file_path)) json_files = [ "module_exceptional.py-dump.json", ] for file_ in json_files: expect_file_path = "{}/{}".format(expect_files_dir, file_) actual_file_path = "{}/{}".format(actual_files_dir, file_) with open(expect_file_path, "r") as f: expect_json = {"data": json.load(f)} with open(actual_file_path, "r") as f: self.log( "============= {} =============".format(actual_file_path)) data = json.load(f) self.log(str(data)) actual_json = {"data": data} self.assertDictEqual(expect_json, actual_json) self.assertFalse( self.__is_py_typed_exist("{}/py.typed".format(self.output_dir)))
def test_single_rule(self): rule_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_single_rule.rst"), ] config = PackageGeneratorConfig() config.output_dir = self.output_dir config.os = "Linux" config.style_format = "none" config.dump = True config.target_version = "2.80" config.mod_version = "2.80" config.support_bge = False analyzer = BaseAnalyzer() generator = BaseGenerator() rule = PackageGenerationRule("rule", rule_rst_files, analyzer, generator) pkg_analyzer = PackageAnalyzer(config, [rule]) pkg_analyzer.analyze() pkg_struct = pkg_analyzer.package_structure() self.assertDictEqual( pkg_struct.to_dict(), { "name": None, "children": [{ "name": "module_abc", "children": [], }] }) entries = pkg_analyzer.entry_points() actual_entries = set([e.fullname() for e in entries]) expect_entries = { "module_abc.Class123", } self.assertSetEqual(expect_entries, actual_entries) gen_info = pkg_analyzer.generation_info() self.assertEqual(len(gen_info.keys()), 1) actual_rule = None actual_gen_info = None for k in gen_info.keys(): if k.name() == "rule": actual_rule = k actual_gen_info = gen_info[k] self.assertIsNotNone(actual_rule) self.assertIsNotNone(actual_gen_info) self.assertEquals(set(actual_gen_info.targets()), {"module_abc.py"}) target_module_abc = actual_gen_info.get_target("module_abc.py") self.assertEqual(len(target_module_abc.data), 1) self.assertEqual(target_module_abc.data[0].type(), "class") self.assertEqual(target_module_abc.data[0].name(), "Class123") self.assertEqual(len(target_module_abc.child_modules), 0) self.assertEqual(len(target_module_abc.dependencies), 0)
def test_all(self): config = PackageGeneratorConfig() config.output_dir = "./test" config.os = "Windows" config.style_format = "none" config.dump = True config.target_version = "2.80" config.mod_version = "2.80" config.support_bge = True self.assertEqual(config.output_dir, "./test") self.assertEqual(config.os, "Windows") self.assertEqual(config.style_format, "none") self.assertTrue(config.dump) self.assertTrue(config.target_version, "2.80") self.assertEqual(config.mod_version, "2.80") self.assertTrue(config.support_bge)
def test_multiple_rules(self): rule_1_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_multiple_rules_1_a.rst"), "{}/{}".format(self.data_dir, "package_analyzer_test_multiple_rules_1_b.rst"), ] rule_2_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_multiple_rules_2.rst"), ] config = PackageGeneratorConfig() config.output_dir = "{}/output".format(self.output_dir) config.os = "Linux" config.style_format = "none" config.dump = True config.target_version = "2.80" config.mod_version = None config.support_bge = False analyzer = BaseAnalyzer() generator = BaseGenerator() rule_1 = PackageGenerationRule("rule_1", rule_1_rst_files, analyzer, generator) analyzer = BaseAnalyzer() generator = BaseGenerator() rule_2 = PackageGenerationRule("rule_2", rule_2_rst_files, analyzer, generator) pkg_analyzer = PackageAnalyzer(config, [rule_1, rule_2]) pkg_analyzer.analyze() pkg_struct = pkg_analyzer.package_structure() self.assertDictEqual( pkg_struct.to_dict(), { "name": None, "children": [{ "name": "module_1", "children": [{ "name": "submodule_1", "children": [], }] }, { "name": "module_2", "children": [], }] }) entries = pkg_analyzer.entry_points() actual_entries = set([e.fullname() for e in entries]) expect_entries = { "module_1.ClassA", "module_1.submodule_1.BaseClass1", "module_1.submodule_1.function_1", "module_1.submodule_1.DATA_1", "module_2.function_1", } self.assertSetEqual(expect_entries, actual_entries) gen_info = pkg_analyzer.generation_info() self.assertEqual(len(gen_info.keys()), 2) actual_rule_1 = None actual_rule_2 = None actual_gen_info_1 = None actual_gen_info_2 = None for k in gen_info.keys(): if k.name() == "rule_1": actual_rule_1 = k actual_gen_info_1 = gen_info[k] elif k.name() == "rule_2": actual_rule_2 = k actual_gen_info_2 = gen_info[k] self.assertIsNotNone(actual_rule_1) self.assertIsNotNone(actual_rule_2) self.assertIsNotNone(actual_gen_info_1) self.assertIsNotNone(actual_gen_info_2) self.assertEquals(set(actual_gen_info_1.targets()), {"module_1/__init__.py", "module_1/submodule_1.py"}) self.assertEquals(set(actual_gen_info_2.targets()), {"module_2.py"}) target_module_1 = actual_gen_info_1.get_target("module_1/__init__.py") self.assertEqual(len(target_module_1.data), 1) self.assertEqual(target_module_1.data[0].type(), "class") self.assertEqual(target_module_1.data[0].name(), "ClassA") self.assertEquals(target_module_1.child_modules, ["submodule_1"]) self.assertEqual(len(target_module_1.dependencies), 1) self.assertEqual(target_module_1.dependencies[0].mod_name, "module_1.submodule_1") self.assertEquals(target_module_1.dependencies[0].type_lists, ["BaseClass1"]) target_module_1_submodule_1 = actual_gen_info_1.get_target( "module_1/submodule_1.py") self.assertEqual(len(target_module_1_submodule_1.data), 3) self.assertEqual(target_module_1_submodule_1.data[0].type(), "class") self.assertEqual(target_module_1_submodule_1.data[0].name(), "BaseClass1") self.assertEqual(target_module_1_submodule_1.data[1].type(), "function") self.assertEqual(target_module_1_submodule_1.data[1].name(), "function_1") self.assertEqual(target_module_1_submodule_1.data[2].type(), "constant") self.assertEqual(target_module_1_submodule_1.data[2].name(), "DATA_1") self.assertEqual(len(target_module_1_submodule_1.child_modules), 0) self.assertEqual(len(target_module_1_submodule_1.dependencies), 0) target_module_2 = actual_gen_info_2.get_target("module_2.py") self.assertEqual(len(target_module_2.data), 1) self.assertEqual(target_module_2.data[0].type(), "function") self.assertEqual(target_module_2.data[0].name(), "function_1") self.assertEqual(len(target_module_2.child_modules), 0) self.assertEqual(len(target_module_2.dependencies), 2) self.assertEqual(target_module_2.dependencies[0].mod_name, "module_1") self.assertEquals(target_module_2.dependencies[0].type_lists, ["ClassA"]) self.assertEqual(target_module_2.dependencies[1].mod_name, "module_1.submodule_1") self.assertEquals(target_module_2.dependencies[1].type_lists, ["BaseClass1"])