|
| 1 | +using Xunit; |
| 2 | +using Semmle.BuildAnalyser; |
| 3 | +using Semmle.Util.Logging; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace Semmle.Extraction.Tests |
| 7 | +{ |
| 8 | + |
| 9 | + internal class LoggerStub : ILogger |
| 10 | + { |
| 11 | + public void Log(Severity severity, string message) { } |
| 12 | + |
| 13 | + public void Dispose() { } |
| 14 | + } |
| 15 | + |
| 16 | + internal class UnsafeFileReaderStub : IUnsafeFileReader |
| 17 | + { |
| 18 | + private readonly List<string> lines; |
| 19 | + |
| 20 | + public UnsafeFileReaderStub(List<string> lines) |
| 21 | + { |
| 22 | + this.lines = lines; |
| 23 | + } |
| 24 | + |
| 25 | + public IEnumerable<string> ReadLines(string file) |
| 26 | + { |
| 27 | + foreach (var line in lines) |
| 28 | + { |
| 29 | + yield return line; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + internal class TestFileContent : FileContent |
| 35 | + { |
| 36 | + public TestFileContent(List<string> lines) : base(() => new HashSet<string>(), |
| 37 | + new ProgressMonitor(new LoggerStub()), |
| 38 | + () => new List<string>() { "test1.cs" }, |
| 39 | + new UnsafeFileReaderStub(lines)) |
| 40 | + { } |
| 41 | + } |
| 42 | + |
| 43 | + public class FileContentTests |
| 44 | + { |
| 45 | + [Fact] |
| 46 | + public void TestFileContent1() |
| 47 | + { |
| 48 | + // Setup |
| 49 | + var lines = new List<string>() |
| 50 | + { |
| 51 | + "<Project Sdk=\"Microsoft.NET.Sdk\">", |
| 52 | + "<PackageReference Include=\"DotNetAnalyzers.DocumentationAnalyzers\" Version=\"1.0.0-beta.59\" PrivateAssets=\"all\" />", |
| 53 | + "<PackageReference Version=\"7.0.0\" Include=\"Microsoft.CodeAnalysis.NetAnalyzers\"PrivateAssets=\"all\" />", |
| 54 | + "<PackageReference Include=\"StyleCop.Analyzers\" Version=\"1.2.0-beta.406\">", |
| 55 | + "<FrameworkReference Include=\"My.Framework\"/>" |
| 56 | + }; |
| 57 | + var fileContent = new TestFileContent(lines); |
| 58 | + |
| 59 | + // Execute |
| 60 | + var notYetDownloadedPackages = fileContent.NotYetDownloadedPackages; |
| 61 | + var useAspNetDlls = fileContent.UseAspNetDlls; |
| 62 | + |
| 63 | + // Verify |
| 64 | + Assert.False(useAspNetDlls); |
| 65 | + Assert.Equal(3, notYetDownloadedPackages.Count); |
| 66 | + Assert.Contains("DotNetAnalyzers.DocumentationAnalyzers".ToLowerInvariant(), notYetDownloadedPackages); |
| 67 | + Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), notYetDownloadedPackages); |
| 68 | + Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), notYetDownloadedPackages); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void TestFileContent2() |
| 73 | + { |
| 74 | + // Setup |
| 75 | + var lines = new List<string>() |
| 76 | + { |
| 77 | + "<Project Sdk=\"Microsoft.NET.Sdk.Web\">", |
| 78 | + "<FrameworkReference Include=\"My.Framework\"/>", |
| 79 | + "<PackageReference Version=\"7.0.0\" Include=\"Microsoft.CodeAnalysis.NetAnalyzers\"PrivateAssets=\"all\" />", |
| 80 | + "<PackageReference Include=\"StyleCop.Analyzers\" Version=\"1.2.0-beta.406\">" |
| 81 | + }; |
| 82 | + var fileContent = new TestFileContent(lines); |
| 83 | + |
| 84 | + // Execute |
| 85 | + var useAspNetDlls = fileContent.UseAspNetDlls; |
| 86 | + var notYetDownloadedPackages = fileContent.NotYetDownloadedPackages; |
| 87 | + |
| 88 | + // Verify |
| 89 | + Assert.True(useAspNetDlls); |
| 90 | + Assert.Equal(2, notYetDownloadedPackages.Count); |
| 91 | + Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), notYetDownloadedPackages); |
| 92 | + Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), notYetDownloadedPackages); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments