def add_schemas_from_github_url(git_url: String): with tempfile.TemporaryDirectory() as td: try: git.Git(td).clone(git_url, str(uuid.uuid4()), depth=1) except GitCommandError as gce: raise Exception( f"Trying to read from the git repo ({git_url}) and write to the directory ({td}). Full error follows: {str(gce)}" ) MLSchema.append_schema_to_registry(Path(td))
def test_add_schema_to_registry(self): MLSchema.populate_registry() load_path = Path(os.path.dirname(__file__)) / str("external_schema") with self.assertRaises(FileNotFoundError) as context: MLSchema.append_schema_to_registry( "./external_schema" ) # This is a string and not a path, so should error. self.assertTrue("No files ending in" in str(context.exception)) with self.assertRaises(FileNotFoundError) as context: MLSchema.append_schema_to_registry( Path("./external_schema" )) # This is a string and not a path, so should error. self.assertTrue("No files ending in" in str(context.exception)) current_schema_total = len(marshmallow.class_registry._registry) # mymodule.urlprint(protocol, host, domain) # self.assertEqual(fake_out.getvalue(), expected_url) with self.assertRaises(FileNotFoundError): MLSchema.append_schema_to_registry(load_path / str("no_files")) mock_stdout = StringIO() rootLogger = logging.getLogger() rootLogger.addHandler(logging.StreamHandler(mock_stdout)) MLSchema.append_schema_to_registry(load_path / str("bad_yaml")) return_string = mock_stdout.getvalue() assert "bad.yaml" in return_string assert "parsed" in return_string mock_stdout.seek(2) return_string = "" MLSchema.append_schema_to_registry(load_path / str("missing_fields")) return_string = mock_stdout.getvalue() assert "missing_fields.yaml" in return_string assert "mlspec" in return_string mock_stdout.seek(2) return_string = "" temp_dir = tempfile.gettempdir() file_name = f"{str(uuid.uuid4())}.yaml" temp_yaml_file = (Path(temp_dir) / file_name).write_text("") MLSchema.append_schema_to_registry(temp_dir) return_string = mock_stdout.getvalue() assert str(temp_yaml_file) in return_string assert "mlspec" in return_string mock_stdout.seek(2) return_string = "" valid_path = load_path / str("valid") MLSchema.append_schema_to_registry(valid_path) new_schemas = os.listdir(valid_path) self.assertTrue(( current_schema_total + 2 * len(new_schemas)) == len(marshmallow.class_registry._registry))