public void TestSchema_SampleFiles_AllAreValid() { Dictionary <string, List <string> > filesAndErrors = new Dictionary <string, List <string> >(); foreach (var manifest in TestUtils.GetSampleProjectsRoot().EnumerateFiles($"*.*", SearchOption.AllDirectories).Where(x => x.FullName.EndsWith(Strings.ManifestSuffix, StringComparison.OrdinalIgnoreCase))) { string text = File.ReadAllText(manifest.FullName); var xDoc = XDocument.Parse(text); var validator = new SchemaValidator(); var errors = validator.ValidateManifest(xDoc); filesAndErrors.Add(manifest.FullName, errors); } filesAndErrors.Count.Should().BeGreaterOrEqualTo(2); if (filesAndErrors.SelectMany(x => x.Value).Any()) { var sb = new StringBuilder(); foreach (KeyValuePair <string, List <string> > fileAndError in filesAndErrors) { if (fileAndError.Value.Any()) { sb.AppendLine($"File {fileAndError.Key} errors: \r\n\r\n{string.Join("\r\n", fileAndError.Value)}"); } } Assert.Fail(sb.ToString()); } }
public void TestSchema_RandomXml_IsNotValid() { string text = "<Project xmlns=\"https://git.io/RepoCat-ProjectInfo\">hi</Project>"; var xDoc = XDocument.Parse(text); var validator = new SchemaValidator(); var errors = validator.ValidateManifest(xDoc); errors.Should().NotBeNullOrEmpty("There should be errors in this file"); }
public void TestSchema_SampleFiles_AllAreValid() { Dictionary <string, List <string> > filesAndErrors = new Dictionary <string, List <string> >(); IEnumerable <string> manifests = Directory .EnumerateFiles(Path.Combine(TestContext.CurrentContext.TestDirectory, "SampleManifestFiles")) .Concat(Directory.EnumerateFiles( Path.Combine(TestContext.CurrentContext.TestDirectory, "SampleScriptsRepository"), "*.*", SearchOption.AllDirectories).Where(x => x.EndsWith(Strings.ManifestSuffix, StringComparison.OrdinalIgnoreCase))); foreach (string manifestPath in manifests) { string text = File.ReadAllText(manifestPath); XDocument xDoc = XDocument.Parse(text); SchemaValidator validator = new SchemaValidator(); List <string> errors = validator.ValidateManifest(xDoc); try { var _ = ManifestDeserializer.DeserializeProjectInfo(xDoc.Root); } catch (Exception ex) { errors.Add(ex.ToString()); } filesAndErrors.Add(manifestPath, errors); } filesAndErrors.Count.Should().BeGreaterOrEqualTo(8); if (filesAndErrors.SelectMany(x => x.Value).Any()) { StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, List <string> > fileAndError in filesAndErrors) { if (fileAndError.Value.Any()) { sb.AppendLine($"File {fileAndError.Key} errors: \r\n\r\n{string.Join("\r\n", fileAndError.Value)}"); } } Assert.Fail(sb.ToString()); } }
#pragma warning disable 1998 public async Task <IActionResult> AddProject([FromBody] AddProjectModel project) #pragma warning restore 1998 { if (!this.ModelState.IsValid || project == null) { this.TempData["error"] = "Incorrect input."; return(this.Json(this.Url.Action("AddProject"))); } try { var validator = new SchemaValidator(); var errors = validator.ValidateManifest(project.EmptyManifestXml, out XDocument _); if (errors.Count > 0) { this.TempData["error"] = "Schema validation errors:\r\n" + string.Join("\r\n", errors); return(this.Json(this.Url.Action("AddProject"))); } else { ProjectInfo projectInfo = ManifestDeserializer.DeserializeProjectInfo(XElement.Parse(project.EmptyManifestXml)); var upsertedProject = await this.repositoryService.Upsert(projectInfo).ConfigureAwait(false); this.TempData["success"] = $"Added project to catalog. Internal ID: {upsertedProject.Id}"; return(this.Json(this.Url.Action("AddProject"))); } } catch (Exception ex) { this.TempData["error"] = $"Error: {ex.Message}"; return(this.Json(this.Url.Action("AddProject"))); } }