Skip to content

Commit e36b44c

Browse files
committed
FileSystemWatcher Analyzer
1 parent 83baf1a commit e36b44c

5 files changed

Lines changed: 138 additions & 1 deletion

File tree

System.IO.Abstractions.Analyzers.Tests/Analyzers/DriveInfoAnalyzerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Analyzer_is_not_triggered(string filename)
1818
}
1919

2020
[Theory]
21-
[InlineData("WithOutFileSystem.txt", 15, 4)]
21+
[InlineData("WithOutFileSystem.txt", 15, 21)]
2222
public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagnosticColumn)
2323
{
2424
var source = ReadFile(filename);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Collections.Generic;
2+
using System.IO.Abstractions.Analyzers.Analyzers.FileSystemTypeAnalyzers;
3+
using Microsoft.CodeAnalysis;
4+
using Roslyn.Testing.Analyzer;
5+
using Roslyn.Testing.Model;
6+
using Xunit;
7+
8+
namespace System.IO.Abstractions.Analyzers.Tests.Analyzers
9+
{
10+
public class FileSystemWatcherAnalyzerTests : CSharpDiagnosticAnalyzerTest<FileSystemWatcherAnalyzer>
11+
{
12+
[Theory]
13+
[InlineData("Valid.txt")]
14+
public void Analyzer_is_not_triggered(string filename)
15+
{
16+
var source = ReadFile(filename);
17+
VerifyNoDiagnosticTriggered(source);
18+
}
19+
20+
[Theory]
21+
[InlineData("WithOutFileSystem.txt", 15, 42)]
22+
public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagnosticColumn)
23+
{
24+
var source = ReadFile(filename);
25+
26+
var expectedDiagnostic = new DiagnosticResult
27+
{
28+
Id = FileSystemWatcherAnalyzer.DiagnosticId,
29+
Message = FileSystemWatcherAnalyzer.MessageFormat,
30+
Severity = DiagnosticSeverity.Warning,
31+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", diagnosticLine, diagnosticColumn) }
32+
};
33+
34+
VerifyDiagnostic(source, expectedDiagnostic);
35+
}
36+
37+
[Fact]
38+
public void Empty_source_code_does_not_trigger_analyzer()
39+
{
40+
var source = string.Empty;
41+
VerifyNoDiagnosticTriggered(source);
42+
}
43+
44+
protected override IEnumerable<MetadataReference> GetAdditionalReferences() => new[]
45+
{
46+
MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
47+
};
48+
}
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.IO.Abstractions;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class Valid
6+
{
7+
private readonly IFileSystem _fileSystem;
8+
9+
public Valid(IFileSystem fileSystem)
10+
{
11+
_fileSystem = fileSystem;
12+
}
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
public WithOutFileSystem()
8+
{
9+
}
10+
11+
public void SomeMethod()
12+
{
13+
const string path = "C:\\temp.txt";
14+
15+
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
16+
}
17+
}
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Immutable;
2+
using JetBrains.Annotations;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Microsoft.CodeAnalysis.Diagnostics;
6+
7+
namespace System.IO.Abstractions.Analyzers.Analyzers.FileSystemTypeAnalyzers
8+
{
9+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
10+
public class FileSystemWatcherAnalyzer: BaseFileSystemNodeObjectCreationAnalyzer
11+
{
12+
/// <summary>
13+
/// Diagnostic Identifier
14+
/// </summary>
15+
[UsedImplicitly]
16+
public const string DiagnosticId = "IO0009";
17+
18+
/// <summary>
19+
/// Diagnostic Title
20+
/// </summary>
21+
private const string Title = "Invocation DriveInfo class shold be replaced with IFileSystem.DriveInfo";
22+
23+
/// <summary>
24+
/// Diagnostic Message Format
25+
/// </summary>
26+
public const string MessageFormat = Title;
27+
28+
/// <summary>
29+
/// Diagnostic Description
30+
/// </summary>
31+
private const string Description = Title;
32+
33+
/// <summary>
34+
/// Правило
35+
/// </summary>
36+
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId,
37+
Title,
38+
MessageFormat,
39+
Category,
40+
DiagnosticSeverity.Warning,
41+
true,
42+
Description);
43+
44+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
45+
46+
protected override void Analyze(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax syntax)
47+
{
48+
context.ReportDiagnostic(Diagnostic.Create(Rule, syntax.GetLocation()));
49+
}
50+
51+
protected override Type GetFileSystemType()
52+
{
53+
return typeof(FileSystemWatcher);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)