Esempio n. 1
0
        /// <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--", isCurrentSdk ? authProviderCurrent: authProvidervNext)
                                                                             .Replace("--imports--", isCurrentSdk ? importsCurrent: importsVNext));

            // 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.IsKnownIssue);

                Assert.Fail($"{compilationOutputMessage}");
                break;
            }
        }
Esempio n. 2
0
        /// <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.CSharp), 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, "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
                                       .Replace("\r\n\r\n\r\n", "\r\n\r\n");    // do not have two consecutive empty lines

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

            // Compile Code
            var microsoftGraphCSharpCompiler = new MicrosoftGraphCSharpCompiler(testData.FileName, testData.DllPath);
            var compilationResultsModel      = microsoftGraphCSharpCompiler.CompileSnippet(codeToCompile, testData.Version);

            if (compilationResultsModel.IsSuccess)
            {
                Assert.Pass();
            }

            var compilationOutputMessage = new CompilationOutputMessage(compilationResultsModel, codeToCompile, testData.DocsLink, testData.KnownIssueMessage, testData.IsKnownIssue);

            // environment variable for sources directory is defined only for cloud runs
            var config = AppSettings.Config();

            if (bool.Parse(config.GetSection("IsLocalRun").Value) &&
                bool.Parse(config.GetSection("GenerateLinqPadOutputInLocalRun").Value))
            {
                WriteLinqFile(testData, codeSnippetFormatted);
            }

            Assert.Fail($"{compilationOutputMessage}");
        }