Example #1
0
        /// <summary>
        /// Performs an internal consistency check on the methods (requests/responses) in the documentation.
        /// Prints the results of the tests to the console.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="docset"></param>
        /// <returns></returns>
        private static async Task <CheckResults> CheckMethodsAsync(BasicCheckOptions options, DocSet docset)
        {
            MethodDefinition[] methods = FindTestMethods(options, docset);
            CheckResults       results = new CheckResults();

            foreach (var method in methods)
            {
                var testName = "check-method-syntax: " + method.Identifier;
                TestReport.StartTest(testName, method.SourceFile.DisplayName);

                if (string.IsNullOrEmpty(method.ExpectedResponse))
                {
                    await TestReport.FinishTestAsync(testName, TestOutcome.Failed, "Null response where one was expected.");

                    results.FailureCount++;
                    continue;
                }

                var parser           = new HttpParser();
                var expectedResponse = parser.ParseHttpResponse(method.ExpectedResponse);

                ValidationError[] errors;
                method.ValidateResponse(expectedResponse, null, null, out errors);

                await WriteOutErrorsAndFinishTestAsync(errors, options.SilenceWarnings, "   ", "No errors.", false, testName, "Warnings detected", "Errors detected");

                results.IncrementResultCount(errors);
            }

            return(results);
        }
Example #2
0
        /// <summary>
        /// Validate that the CSDL metadata defined for a service matches the documentation.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task <bool> CheckServiceMetadataAsync(CheckMetadataOptions options)
        {
            List <Schema> schemas = await TryGetMetadataSchemasAsync(options);

            if (null == schemas)
            {
                return(false);
            }

            FancyConsole.WriteLine(FancyConsole.ConsoleSuccessColor, "  found {0} schema definitions: {1}", schemas.Count, (from s in schemas select s.Namespace).ComponentsJoinedByString(", "));

            var docSet = await GetDocSetAsync(options);

            if (null == docSet)
            {
                return(false);
            }

            const string testname = "validate-service-metadata";

            TestReport.StartTest(testname);

            List <ResourceDefinition> foundResources = ODataParser.GenerateResourcesFromSchemas(schemas);
            CheckResults results = new CheckResults();

            List <ValidationError> collectedErrors = new List <ValidationError>();

            foreach (var resource in foundResources)
            {
                FancyConsole.WriteLine();
                FancyConsole.Write(FancyConsole.ConsoleHeaderColor, "Checking resource: {0}...", resource.Metadata.ResourceType);

                FancyConsole.VerboseWriteLine();
                FancyConsole.VerboseWriteLine(resource.JsonExample);
                FancyConsole.VerboseWriteLine();

                // Verify that this resource matches the documentation
                ValidationError[] errors;
                docSet.ResourceCollection.ValidateJsonExample(resource.Metadata, resource.JsonExample, out errors, new ValidationOptions {
                    RelaxedStringValidation = true
                });
                results.IncrementResultCount(errors);

                collectedErrors.AddRange(errors);

                await WriteOutErrorsAndFinishTestAsync(errors, options.SilenceWarnings, successMessage : " no errors.");
            }

            if (options.IgnoreWarnings)
            {
                results.ConvertWarningsToSuccess();
            }

            var output = (from e in collectedErrors select e.ErrorText).ComponentsJoinedByString("\r\n");

            await TestReport.FinishTestAsync(testname, results.WereFailures?TestOutcome.Failed : TestOutcome.Passed, stdOut : output);

            results.PrintToConsole();
            return(!results.WereFailures);
        }
Example #3
0
        /// <summary>
        /// Perform an internal consistency check on the examples defined in the documentation. Prints
        /// the results of the tests to the console.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="docset"></param>
        /// <returns></returns>
        private static async Task <CheckResults> CheckExamplesAsync(BasicCheckOptions options, DocSet docset)
        {
            var results = new CheckResults();

            foreach (var doc in docset.Files)
            {
                if (doc.Examples.Length == 0)
                {
                    continue;
                }

                FancyConsole.WriteLine(FancyConsole.ConsoleHeaderColor, "Checking examples in \"{0}\"...", doc.DisplayName);

                foreach (var example in doc.Examples)
                {
                    if (example.Metadata == null)
                    {
                        continue;
                    }
                    if (example.Language != CodeLanguage.Json)
                    {
                        continue;
                    }

                    var testName = string.Format("check-example: {0}", example.Metadata.MethodName, example.Metadata.ResourceType);
                    TestReport.StartTest(testName, doc.DisplayName);

                    ValidationError[] errors;
                    docset.ResourceCollection.ValidateJsonExample(example.Metadata, example.SourceExample, out errors);

                    await WriteOutErrorsAndFinishTestAsync(errors, options.SilenceWarnings, "   ", "No errors.", false, testName, "Warnings detected", "Errors detected");

                    results.IncrementResultCount(errors);
                }
            }

            return(results);
        }
Example #4
0
        /// <summary>
        /// Verifies that all markdown links inside the documentation are valid. Prints warnings
        /// to the console for any invalid links. Also checks for orphaned documents.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="docs"></param>
        private static async Task <bool> CheckLinksAsync(DocSetOptions options, DocSet docs = null)
        {
            const string testName = "Check-links";
            var          docset   = docs ?? await GetDocSetAsync(options);

            if (null == docset)
            {
                return(false);
            }


            TestReport.StartTest(testName);

            ValidationError[] errors;
            docset.ValidateLinks(options.EnableVerboseOutput, out errors);

            foreach (var error in errors)
            {
                MessageCategory category;
                if (error.IsWarning)
                {
                    category = MessageCategory.Warning;
                }
                else if (error.IsError)
                {
                    category = MessageCategory.Error;
                }
                else
                {
                    category = MessageCategory.Information;
                }

                string message = string.Format("{1}: {0}", error.Message.FirstLineOnly(), error.Code);
                await TestReport.LogMessageAsync(message, category);
            }

            return(await WriteOutErrorsAndFinishTestAsync(errors, options.SilenceWarnings, successMessage : "No link errors detected.", testName : testName));
        }