/// <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; } }
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)); }