Esempio n. 1
0
    /// <summary>
    /// Generates a dictionary mapping from snippet file name to documentation page listing the snippet.
    /// </summary>
    /// <param name="version">Docs version (e.g. V1, Beta)</param>
    /// <returns>Dictionary holding the mapping from snippet file name to documentation page listing the snippet.</returns>
    public static Dictionary <string, string> GetDocumentationLinks(Versions version, Languages language)
    {
        var documentationLinks     = new Dictionary <string, string>();
        var documentationDirectory = GraphDocsDirectory.GetDocumentationDirectory(version);
        var files              = Directory.GetFiles(documentationDirectory);
        var languageName       = language.AsString();
        var SnippetLinkPattern = @$ "includes\/snippets\/{languageName}\/(.*)\-{languageName}\-snippets\.md";
        var SnippetLinkRegex   = new Regex(SnippetLinkPattern, RegexOptions.Compiled);

        foreach (var file in files)
        {
            var content  = File.ReadAllText(file);
            var fileName = Path.GetFileNameWithoutExtension(file);
            var docsLink = $"https://docs.microsoft.com/en-us/graph/api/{fileName}?view=graph-rest-{new VersionString(version).DocsUrlSegment()}&tabs={languageName}";

            var match = SnippetLinkRegex.Match(content);
            while (match.Success)
            {
                documentationLinks[$"{match.Groups[1].Value}-{languageName}-snippets.md"] = docsLink;
                match = match.NextMatch();
            }
        }

        return(documentationLinks);
    }
    /// <summary>
    /// 1. Fetches snippet from docs repo
    /// 2. Asserts that there is one and only one snippet in the file
    /// 3. Wraps snippet with compilable template
    /// 4. Attempts to compile and reports errors if there is any
    /// </summary>
    /// <param name="testData">Test data containing information such as snippet file name</param>
    public static void Run(LanguageTestData testData)
    {
        if (testData == null)
        {
            throw new ArgumentNullException(nameof(testData));
        }

        var fullPath = Path.Join(GraphDocsDirectory.GetSnippetsDirectory(testData.Version, Languages.Java), testData.FileName);

        Assert.IsTrue(File.Exists(fullPath), "Snippet file referenced in documentation is not found!");

        var fileContent = File.ReadAllText(fullPath);
        var match       = RegExp.Match(fileContent);

        Assert.IsTrue(match.Success, "Java snippet file is not in expected format!");

        var codeSnippetFormatted = match.Groups[1].Value
                                   .Replace("\r\n", "\r\n        ")         // add indentation to match with the template
                                   .Replace("\r\n        \r\n", "\r\n\r\n") // remove indentation added to empty lines
                                   .Replace("\t", "    ")                   // do not use tabs
                                   .Replace("\r\n\r\n\r\n", "\r\n\r\n");    // do not have two consecutive empty lines
        var isCurrentSdk  = string.IsNullOrEmpty(testData.JavaPreviewLibPath);
        var codeToCompile = BaseTestRunner.ConcatBaseTemplateWithSnippet(codeSnippetFormatted, SDKShellTemplate
                                                                         .Replace("--auth--", authProviderCurrent));

        // Compile Code
        var microsoftGraphCSharpCompiler = new MicrosoftGraphJavaCompiler(testData.FileName, testData.JavaPreviewLibPath, testData.JavaLibVersion, testData.JavaCoreVersion);

        var jvmRetryAttmptsLeft = 3;

        while (jvmRetryAttmptsLeft > 0)
        {
            var compilationResultsModel = microsoftGraphCSharpCompiler.CompileSnippet(codeToCompile, testData.Version);

            if (compilationResultsModel.IsSuccess)
            {
                Assert.Pass();
            }
            else if (compilationResultsModel.Diagnostics.Any(x => x.GetMessage().Contains("Starting a Gradle Daemon")))
            {//the JVM takes time to start making the first test to be run to be flaky, this is a workaround
                jvmRetryAttmptsLeft--;
                Thread.Sleep(20000);
                continue;
            }

            var compilationOutputMessage = new CompilationOutputMessage(compilationResultsModel, codeToCompile, testData.DocsLink, testData.KnownIssueMessage, testData.IsCompilationKnownIssue, Languages.Java);

            Assert.Fail($"{compilationOutputMessage}");
            break;
        }
    }
Esempio n. 3
0
    private static IEnumerable <LanguageTestData> GetLanguageTestData(RunSettings runSettings)
    {
        if (runSettings == null)
        {
            throw new ArgumentNullException(nameof(runSettings));
        }

        var language               = runSettings.Language;
        var version                = runSettings.Version;
        var documentationLinks     = GetDocumentationLinks(version, language);
        var compilationKnownIssues = KnownIssues.GetCompilationKnownIssues(language, version);
        var executionKnownIssues   = KnownIssues.GetExecutionKnownIssues(language, version);
        var snippetFileNames       = documentationLinks.Keys.ToList();

        return(from fileName in snippetFileNames                                                                               // e.g. application-addpassword-csharp-snippets.md
               let arbitraryDllPostfix = runSettings.DllPath == null || runSettings.Language != Languages.CSharp ? string.Empty : "arbitraryDll-"
                                         let testNamePostfix = arbitraryDllPostfix + version.ToString() + "-compiles"          // e.g. Beta-compiles or arbitraryDll-Beta-compiles
                                                               let testName = fileName.Replace("snippets.md", testNamePostfix) // e.g. application-addpassword-csharp-Beta-compiles
                                                                              let docsLink = documentationLinks[fileName]
                                                                                             let knownIssueLookupKey = testName.Replace("arbitraryDll-", string.Empty)
                                                                                                                       let executionIssueLookupKey = testName.Replace("-compiles", "-executes")
                                                                                                                                                     let isCompilationKnownIssue = compilationKnownIssues.ContainsKey(knownIssueLookupKey)
                                                                                                                                                                                   let compilationKnownIssue = isCompilationKnownIssue ? compilationKnownIssues[knownIssueLookupKey] : null
                                                                                                                                                                                                               let isExecutionKnownIssue = executionKnownIssues.ContainsKey(executionIssueLookupKey)
                                                                                                                                                                                                                                           let executionKnownIssue = isExecutionKnownIssue ? executionKnownIssues[executionIssueLookupKey] : null
                                                                                                                                                                                                                                                                     let knownIssueMessage = compilationKnownIssue?.Message ?? executionKnownIssue?.Message ?? string.Empty
                                                                                                                                                                                                                                                                                             let knownIssueTestNamePrefix = compilationKnownIssue?.TestNamePrefix ?? executionKnownIssue?.TestNamePrefix ?? string.Empty
                                                                                                                                                                                                                                                                                                                            let owner = compilationKnownIssue?.Owner ?? executionKnownIssue?.Owner ?? string.Empty
                                                                                                                                                                                                                                                                                                                                        let fullPath = Path.Join(GraphDocsDirectory.GetSnippetsDirectory(version, runSettings.Language), fileName)
                                                                                                                                                                                                                                                                                                                                                       let fileContent = File.ReadAllText(fullPath)
                                                                                                                                                                                                                                                                                                                                                                         select new LanguageTestData(
                   version,
                   isCompilationKnownIssue,
                   isExecutionKnownIssue,
                   knownIssueMessage,
                   knownIssueTestNamePrefix,
                   docsLink,
                   fileName,
                   runSettings.DllPath,
                   runSettings.JavaCoreVersion,
                   runSettings.JavaLibVersion,
                   runSettings.JavaPreviewLibPath,
                   testName,
                   owner,
                   fileContent));
    }