PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Compilers/VisualBasic/Test/CommandLine/ErrorLoggerTests.vb

https://gitlab.com/sharadag/Roslyn
Visual Basic | 309 lines | 267 code | 40 blank | 2 comment | 0 complexity | d6d45ef719c2a73a7319310b22de13da MD5 | raw file
  1. ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. Imports System.Globalization
  3. Imports System.IO
  4. Imports Microsoft.CodeAnalysis.Test.Utilities
  5. Imports Microsoft.CodeAnalysis.VisualBasic.UnitTests
  6. Imports Xunit
  7. Imports Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers
  8. Imports Microsoft.CodeAnalysis.DiagnosticExtensions
  9. Imports Roslyn.Test.Utilities.SharedResourceHelpers
  10. Namespace Microsoft.CodeAnalysis.VisualBasic.CommandLine.UnitTests
  11. Public Class ErrorLoggerTests
  12. Inherits BasicTestBase
  13. Private ReadOnly _baseDirectory As String = TempRoot.Root
  14. <Fact>
  15. Public Sub NoDiagnostics()
  16. Dim helloWorldVB As String = <text>
  17. Imports System
  18. Class C
  19. Shared Sub Main(args As String())
  20. Console.WriteLine("Hello, world")
  21. End Sub
  22. End Class
  23. </text>.Value
  24. Dim hello = Temp.CreateFile().WriteAllText(helloWorldVB).Path
  25. Dim errorLogDir = Temp.CreateDirectory()
  26. Dim errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt")
  27. Dim cmd = New MockVisualBasicCompiler(Nothing, _baseDirectory,
  28. {"/nologo",
  29. $"/errorlog:{errorLogFile}",
  30. hello})
  31. Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
  32. Dim exitCode = cmd.Run(outWriter, Nothing)
  33. Assert.Equal("", outWriter.ToString().Trim())
  34. Assert.Equal(0, exitCode)
  35. Dim actualOutput = File.ReadAllText(errorLogFile).Trim()
  36. Dim expectedHeader = GetExpectedErrorLogHeader(actualOutput, cmd)
  37. Dim expectedIssues = "
  38. ""results"": [
  39. ]
  40. }
  41. ]
  42. }"
  43. Dim expectedText = expectedHeader + expectedIssues
  44. Assert.Equal(expectedText, actualOutput)
  45. CleanupAllGeneratedFiles(hello)
  46. CleanupAllGeneratedFiles(errorLogFile)
  47. End Sub
  48. <Fact>
  49. Public Sub SimpleCompilerDiagnostics()
  50. Dim source As String = <text>
  51. Public Class C
  52. Public Sub Method()
  53. Dim x As Integer
  54. End Sub
  55. End Class
  56. </text>.Value
  57. Dim sourceFilePath = Temp.CreateFile().WriteAllText(source).Path
  58. Dim errorLogDir = Temp.CreateDirectory()
  59. Dim errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt")
  60. Dim cmd = New MockVisualBasicCompiler(Nothing, _baseDirectory,
  61. {"/nologo",
  62. "/preferreduilang:en",
  63. $"/errorlog:{errorLogFile}",
  64. sourceFilePath})
  65. Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
  66. Dim exitCode = cmd.Run(outWriter, Nothing)
  67. Dim actualConsoleOutput = outWriter.ToString().Trim()
  68. Assert.Contains("BC42024", actualConsoleOutput)
  69. Assert.Contains("BC30420", actualConsoleOutput)
  70. Assert.NotEqual(0, exitCode)
  71. Dim actualOutput = File.ReadAllText(errorLogFile).Trim()
  72. Dim expectedHeader = GetExpectedErrorLogHeader(actualOutput, cmd)
  73. Dim expectedIssues = String.Format("
  74. ""results"": [
  75. {{
  76. ""ruleId"": ""BC42024"",
  77. ""level"": ""warning"",
  78. ""message"": ""Unused local variable: 'x'."",
  79. ""locations"": [
  80. {{
  81. ""resultFile"": {{
  82. ""uri"": ""{0}"",
  83. ""region"": {{
  84. ""startLine"": 4,
  85. ""startColumn"": 13,
  86. ""endLine"": 4,
  87. ""endColumn"": 14
  88. }}
  89. }}
  90. }}
  91. ],
  92. ""properties"": {{
  93. ""warningLevel"": 1
  94. }}
  95. }},
  96. {{
  97. ""ruleId"": ""BC30420"",
  98. ""level"": ""error"",
  99. ""message"": ""'Sub Main' was not found in '{1}'.""
  100. }}
  101. ],
  102. ""rules"": {{
  103. ""BC30420"": {{
  104. ""id"": ""BC30420"",
  105. ""defaultLevel"": ""error"",
  106. ""properties"": {{
  107. ""category"": ""Compiler"",
  108. ""isEnabledByDefault"": true,
  109. ""tags"": [
  110. ""Compiler"",
  111. ""Telemetry"",
  112. ""NotConfigurable""
  113. ]
  114. }}
  115. }},
  116. ""BC42024"": {{
  117. ""id"": ""BC42024"",
  118. ""shortDescription"": ""Unused local variable"",
  119. ""defaultLevel"": ""warning"",
  120. ""properties"": {{
  121. ""category"": ""Compiler"",
  122. ""isEnabledByDefault"": true,
  123. ""tags"": [
  124. ""Compiler"",
  125. ""Telemetry""
  126. ]
  127. }}
  128. }}
  129. }}
  130. }}
  131. ]
  132. }}", AnalyzerForErrorLogTest.GetUriForPath(sourceFilePath), Path.GetFileNameWithoutExtension(sourceFilePath))
  133. Dim expectedText = expectedHeader + expectedIssues
  134. Assert.Equal(expectedText, actualOutput)
  135. CleanupAllGeneratedFiles(sourceFilePath)
  136. CleanupAllGeneratedFiles(errorLogFile)
  137. End Sub
  138. <Fact>
  139. Public Sub SimpleCompilerDiagnostics_Suppressed()
  140. Dim source As String = <text>
  141. Public Class C
  142. Public Sub Method()
  143. #Disable Warning BC42024
  144. Dim x As Integer
  145. #Enable Warning BC42024
  146. End Sub
  147. End Class
  148. </text>.Value
  149. Dim sourceFilePath = Temp.CreateFile().WriteAllText(source).Path
  150. Dim errorLogDir = Temp.CreateDirectory()
  151. Dim errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt")
  152. Dim cmd = New MockVisualBasicCompiler(Nothing, _baseDirectory,
  153. {"/nologo",
  154. "/preferreduilang:en",
  155. $"/errorlog:{errorLogFile}",
  156. sourceFilePath})
  157. Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
  158. Dim exitCode = cmd.Run(outWriter, Nothing)
  159. Dim actualConsoleOutput = outWriter.ToString().Trim()
  160. ' Suppressed diagnostics are only report in the error log, not the console output.
  161. Assert.DoesNotContain("BC42024", actualConsoleOutput)
  162. Assert.Contains("BC30420", actualConsoleOutput)
  163. Assert.NotEqual(0, exitCode)
  164. Dim actualOutput = File.ReadAllText(errorLogFile).Trim()
  165. Dim expectedHeader = GetExpectedErrorLogHeader(actualOutput, cmd)
  166. Dim expectedIssues = String.Format("
  167. ""results"": [
  168. {{
  169. ""ruleId"": ""BC42024"",
  170. ""level"": ""warning"",
  171. ""message"": ""Unused local variable: 'x'."",
  172. ""suppressionStates"": [
  173. ""suppressedInSource""
  174. ],
  175. ""locations"": [
  176. {{
  177. ""resultFile"": {{
  178. ""uri"": ""{0}"",
  179. ""region"": {{
  180. ""startLine"": 5,
  181. ""startColumn"": 13,
  182. ""endLine"": 5,
  183. ""endColumn"": 14
  184. }}
  185. }}
  186. }}
  187. ],
  188. ""properties"": {{
  189. ""warningLevel"": 1
  190. }}
  191. }},
  192. {{
  193. ""ruleId"": ""BC30420"",
  194. ""level"": ""error"",
  195. ""message"": ""'Sub Main' was not found in '{1}'.""
  196. }}
  197. ],
  198. ""rules"": {{
  199. ""BC30420"": {{
  200. ""id"": ""BC30420"",
  201. ""defaultLevel"": ""error"",
  202. ""properties"": {{
  203. ""category"": ""Compiler"",
  204. ""isEnabledByDefault"": true,
  205. ""tags"": [
  206. ""Compiler"",
  207. ""Telemetry"",
  208. ""NotConfigurable""
  209. ]
  210. }}
  211. }},
  212. ""BC42024"": {{
  213. ""id"": ""BC42024"",
  214. ""shortDescription"": ""Unused local variable"",
  215. ""defaultLevel"": ""warning"",
  216. ""properties"": {{
  217. ""category"": ""Compiler"",
  218. ""isEnabledByDefault"": true,
  219. ""tags"": [
  220. ""Compiler"",
  221. ""Telemetry""
  222. ]
  223. }}
  224. }}
  225. }}
  226. }}
  227. ]
  228. }}", AnalyzerForErrorLogTest.GetUriForPath(sourceFilePath), Path.GetFileNameWithoutExtension(sourceFilePath))
  229. Dim expectedText = expectedHeader + expectedIssues
  230. Assert.Equal(expectedText, actualOutput)
  231. CleanupAllGeneratedFiles(sourceFilePath)
  232. CleanupAllGeneratedFiles(errorLogFile)
  233. End Sub
  234. <Fact>
  235. Public Sub AnalyzerDiagnosticsWithAndWithoutLocation()
  236. Dim source As String = <text>
  237. Imports System
  238. Class C
  239. End Class
  240. </text>.Value
  241. Dim sourceFilePath = Temp.CreateFile().WriteAllText(source).Path
  242. Dim outputDir = Temp.CreateDirectory()
  243. Dim errorLogFile = Path.Combine(outputDir.Path, "ErrorLog.txt")
  244. Dim outputFilePath = Path.Combine(outputDir.Path, "test.dll")
  245. Dim cmd = New MockVisualBasicCompiler(Nothing, _baseDirectory,
  246. {"/nologo",
  247. "/preferreduilang:en",
  248. "/t:library",
  249. $"/out:{outputFilePath}",
  250. $"/errorlog:{errorLogFile}",
  251. sourceFilePath},
  252. analyzer:=New AnalyzerForErrorLogTest())
  253. Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
  254. Dim exitCode = cmd.Run(outWriter, Nothing)
  255. Dim actualConsoleOutput = outWriter.ToString().Trim()
  256. Assert.Contains(AnalyzerForErrorLogTest.Descriptor1.Id, actualConsoleOutput)
  257. Assert.Contains(AnalyzerForErrorLogTest.Descriptor2.Id, actualConsoleOutput)
  258. Assert.NotEqual(0, exitCode)
  259. Dim actualOutput = File.ReadAllText(errorLogFile).Trim()
  260. Dim expectedHeader = GetExpectedErrorLogHeader(actualOutput, cmd)
  261. Dim expectedIssues = AnalyzerForErrorLogTest.GetExpectedErrorLogResultsText(cmd.Compilation)
  262. Dim expectedText = expectedHeader + expectedIssues
  263. Assert.Equal(expectedText, actualOutput)
  264. CleanupAllGeneratedFiles(sourceFilePath)
  265. CleanupAllGeneratedFiles(outputFilePath)
  266. CleanupAllGeneratedFiles(errorLogFile)
  267. End Sub
  268. End Class
  269. End Namespace