Skip to content

Commit 86ed7af

Browse files
authored
Enable Roslyn Analyzers for Code Analysis (#20)
FxCop StyleCop Integration in Visual Studio Code
2 parents fb66a99 + f3885b4 commit 86ed7af

File tree

12 files changed

+637
-445
lines changed

12 files changed

+637
-445
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"omnisharp.enableRoslynAnalyzers": true,
23
"dotnet-test-explorer.autoExpandTree": true,
34
"dotnet-test-explorer.autoWatch": true,
45
"dotnet-test-explorer.testProjectPath": "**/*Tests.csproj",

FollowingFileStream.ConsoleTestTool/FollowingFileStream.ConsoleTestTool.csproj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
<PropertyGroup>
88
<OutputType>Exe</OutputType>
99
<TargetFrameworks>netcoreapp3.0;netcoreapp2.2</TargetFrameworks>
10-
<LangVersion>7.1</LangVersion>
10+
<LangVersion>8.0</LangVersion>
1111
<IsPackable>false</IsPackable>
12+
<noWarn>SA0001</noWarn>
1213
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.5" PrivateAssets="All"/>
17+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All"/>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Include="../LICENSE"/>
22+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)..\stylecop.json">
23+
<Link>stylecop.json</Link>
24+
</AdditionalFiles>
25+
</ItemGroup>
26+
1327
</Project>
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
using System;
2-
using System.IO;
3-
using System.Threading.Tasks;
1+
// --------------------------------------------------------------------------------------------------
2+
// <copyright file="Program.cs" company="Manandre">
3+
// Copyright (c) Manandre. All rights reserved.
4+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
5+
// </copyright>
6+
// --------------------------------------------------------------------------------------------------
47

58
namespace Manandre.IO
69
{
7-
static class Program
10+
using System;
11+
using System.IO;
12+
using System.Threading.Tasks;
13+
14+
/// <summary>
15+
/// Console test tool.
16+
/// </summary>
17+
internal static class Program
818
{
919
private const string InputPath = "source.txt";
1020
private const string OutputPath = "destination.txt";
1121

12-
static async Task Main(string[] args)
22+
/// <summary>
23+
/// Main routine.
24+
/// </summary>
25+
/// <returns>A task representing the main operation.</returns>
26+
public static async Task Main()
1327
{
14-
Console.WriteLine("Hello World!");
15-
var input = WriteInput();
16-
await CopyToOutput();
28+
var input = WriteInput().ConfigureAwait(false);
29+
await CopyToOutput().ConfigureAwait(false);
1730
await input;
1831
}
1932

2033
private static async Task WriteInput()
2134
{
22-
using (var sw = new StreamWriter(new FileStream(InputPath, FileMode.Create, FileAccess.Write, FileShare.Read)))
23-
using (var sr = new StreamReader(Console.OpenStandardInput()))
24-
{
25-
await sr.CopyToAsync(sw, stopOn:"quit");
26-
}
27-
}
28-
private static async Task CopyToOutput()
29-
{
30-
using (var source = new FollowingFileStream(InputPath))
31-
using (var destination = new FileStream(OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
32-
{
33-
await source.CopyToAsync(destination);
34-
}
35+
using var sw = new StreamWriter(new FileStream(InputPath, FileMode.Create, FileAccess.Write, FileShare.Read));
36+
using var sr = new StreamReader(Console.OpenStandardInput());
37+
await sr.CopyToAsync(sw, stopOn: "quit").ConfigureAwait(false);
3538
}
36-
}
3739

38-
static public class TextReaderExtensions
39-
{
40-
public static async Task CopyToAsync(this TextReader reader, TextWriter writer, string stopOn)
40+
private static async Task CopyToOutput()
4141
{
42-
string line = string.Empty;
43-
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != stopOn)
44-
{
45-
await writer.WriteLineAsync(line).ConfigureAwait(false);
46-
await writer.FlushAsync().ConfigureAwait(false);
47-
}
42+
using var source = new FollowingFileStream(InputPath);
43+
using var destination = new FileStream(OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read);
44+
await source.CopyToAsync(destination).ConfigureAwait(false);
4845
}
4946
}
50-
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// --------------------------------------------------------------------------------------------------
2+
// <copyright file="TextReaderExtensions.cs" company="Manandre">
3+
// Copyright (c) Manandre. All rights reserved.
4+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
5+
// </copyright>
6+
// --------------------------------------------------------------------------------------------------
7+
8+
namespace Manandre.IO
9+
{
10+
using System.IO;
11+
using System.Threading.Tasks;
12+
13+
/// <summary>
14+
/// Extension methods for the <see cref="TextReader"/> class.
15+
/// </summary>
16+
internal static class TextReaderExtensions
17+
{
18+
/// <summary>
19+
/// Copy lines from reader to writer until a specific string is reached.
20+
/// </summary>
21+
/// <param name="reader">A text reader to read from.</param>
22+
/// <param name="writer">A text writer to write to.</param>
23+
/// <param name="stopOn">A string on which to stop copying.</param>
24+
/// <returns>A task representing the copy operation.</returns>
25+
public static async Task CopyToAsync(this TextReader reader, TextWriter writer, string stopOn)
26+
{
27+
string line;
28+
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != stopOn)
29+
{
30+
await writer.WriteLineAsync(line).ConfigureAwait(false);
31+
await writer.FlushAsync().ConfigureAwait(false);
32+
}
33+
}
34+
}
35+
}

FollowingFileStream.Tests/AsyncStreamTest.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Moq;
3-
using System;
4-
using System.IO;
5-
using System.Linq.Expressions;
6-
using System.Threading;
1+
// --------------------------------------------------------------------------------------------------
2+
// <copyright file="AsyncStreamTest.cs" company="Manandre">
3+
// Copyright (c) Manandre. All rights reserved.
4+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
5+
// </copyright>
6+
// --------------------------------------------------------------------------------------------------
77

88
namespace Manandre.IO
99
{
10+
using System;
11+
using System.IO;
12+
using System.Linq.Expressions;
13+
using System.Threading;
14+
using Microsoft.VisualStudio.TestTools.UnitTesting;
15+
using Moq;
16+
17+
/// <summary>
18+
/// Test class for AsyncStream.
19+
/// </summary>
1020
[TestClass]
1121
public class AsyncStreamTest
1222
{
23+
/// <summary>
24+
/// Test AsyncStream read operations.
25+
/// </summary>
1326
[TestMethod]
14-
public void AS_Read()
27+
public void ASRead()
1528
{
1629
var sut = new Mock<AsyncStream>() { CallBase = true };
1730
var expected = 42;
@@ -24,8 +37,11 @@ public void AS_Read()
2437
Assert.AreEqual(expected, read);
2538
}
2639

40+
/// <summary>
41+
/// Test AsyncStream write operations.
42+
/// </summary>
2743
[TestMethod]
28-
public void AS_Write()
44+
public void ASWrite()
2945
{
3046
var sut = new Mock<AsyncStream>() { CallBase = true };
3147
sut.Setup(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
@@ -37,25 +53,34 @@ public void AS_Write()
3753
sut.Verify(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
3854
}
3955

56+
/// <summary>
57+
/// Test AsyncStream flush operations.
58+
/// </summary>
4059
[TestMethod]
41-
public void AS_Flush()
60+
public void ASFlush()
4261
{
4362
var sut = new Mock<AsyncStream>() { CallBase = true };
4463
sut.Setup(x => x.FlushAsync(It.IsAny<CancellationToken>())).Verifiable();
4564
sut.Object.Flush();
4665
sut.Verify(x => x.FlushAsync(It.IsAny<CancellationToken>()), Times.Once);
4766
}
4867

68+
/// <summary>
69+
/// Test AsyncStream dispose operations.
70+
/// </summary>
4971
[TestMethod]
50-
public void AS_Dispose()
72+
public void ASDispose()
5173
{
5274
var sut = new Mock<AsyncStream>() { CallBase = true };
5375
sut.Object.Dispose();
5476
sut.Object.Dispose();
5577
}
5678

79+
/// <summary>
80+
/// Test AsyncStream synchronized operations.
81+
/// </summary>
5782
[TestMethod]
58-
public void AS_Synchronized()
83+
public void ASSynchronized()
5984
{
6085
Assert.ThrowsException<ArgumentNullException>(() => AsyncStream.Synchronized(null));
6186
var sut = new Mock<AsyncStream>() { CallBase = true };
@@ -69,7 +94,7 @@ public void AS_Synchronized()
6994
x => x.CanRead,
7095
x => x.CanWrite,
7196
x => x.CanSeek,
72-
x => x.CanTimeout
97+
x => x.CanTimeout,
7398
};
7499
foreach (var func in funcs)
75100
{

FollowingFileStream.Tests/FollowingFileStream.Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netcoreapp3.0;netcoreapp2.2</TargetFrameworks>
4+
<LangVersion>8.0</LangVersion>
45
<IsPackable>false</IsPackable>
6+
<noWarn>SA0001</noWarn>
57
</PropertyGroup>
68
<ItemGroup>
79
<PackageReference Include="coverlet.msbuild" Version="2.6.3">
@@ -12,8 +14,16 @@
1214
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0"/>
1315
<PackageReference Include="MSTest.TestFramework" Version="1.4.0"/>
1416
<PackageReference Include="Moq" Version="4.12.0"/>
17+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.5" PrivateAssets="All"/>
18+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All"/>
1519
</ItemGroup>
1620
<ItemGroup>
1721
<ProjectReference Include="..\FollowingFileStream\FollowingFileStream.csproj"/>
1822
</ItemGroup>
23+
<ItemGroup>
24+
<None Include="../LICENSE"/>
25+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)..\stylecop.json">
26+
<Link>stylecop.json</Link>
27+
</AdditionalFiles>
28+
</ItemGroup>
1929
</Project>

0 commit comments

Comments
 (0)