Beispiel #1
0
    /// <summary>
    /// Gets code to be executed
    /// </summary>
    /// <param name="fileContent">snippet file content</param>
    /// <returns>code to be executed</returns>
    private static (string, string) GetCodeToExecute(string fileContent)
    {
        var(codeToCompile, codeSnippetFormatted) = GetCodeToCompile(IdentifierReplacer.Instance.ReplaceIds(fileContent));

        // have another transformation to insert GetRequestMessage method
        codeToCompile = codeToCompile.Replace("GraphServiceClient( authProvider );", "GraphServiceClient( authProvider, httpProvider );");
        codeToCompile = codeToCompile.Replace("return null; //return-request-message", "//insert-code-here");
        codeToCompile = BaseTestRunner.ConcatBaseTemplateWithSnippet(ReturnHttpRequestMessage(codeSnippetFormatted), codeToCompile);
        return(codeToCompile, codeSnippetFormatted);
    }
    /// <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;
        }
    }
Beispiel #3
0
    /// <summary>
    /// Gets code to be compiled
    /// </summary>
    /// <param name="fileContent">snippet file content</param>
    /// <returns>code to be compiled</returns>
    private static (string, string) GetCodeToCompile(string fileContent)
    {
        var match = CSharpSnippetRegex.Match(fileContent);

        Assert.IsTrue(match.Success, "Csharp 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

        while (codeSnippetFormatted.Contains("\r\n\r\n"))
        {
            codeSnippetFormatted = codeSnippetFormatted.Replace("\r\n\r\n", "\r\n"); // do not have empty lines for shorter error messages
        }

        var codeToCompile = BaseTestRunner.ConcatBaseTemplateWithSnippet(codeSnippetFormatted, SDKShellTemplate);

        return(codeToCompile, codeSnippetFormatted);
    }