Skip to content

Commit 83baf1a

Browse files
committed
DriveInfo Analyzer
1 parent aa90c73 commit 83baf1a

6 files changed

Lines changed: 139 additions & 2 deletions

File tree

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 DriveInfoAnalyzerTests: CSharpDiagnosticAnalyzerTest<DriveInfoAnalyzer>
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, 4)]
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 = DriveInfoAnalyzer.DiagnosticId,
29+
Message = DriveInfoAnalyzer.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+
DriveInfo info = new DriveInfo("C");
16+
}
17+
}
18+
}

System.IO.Abstractions.Analyzers/Analyzers/FileSystemTypeAnalyzers/DirectoryInfoAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DirectoryInfoAnalyzer: BaseFileSystemNodeObjectCreationAnalyzer
1818
/// <summary>
1919
/// Diagnostic Title
2020
/// </summary>
21-
private const string Title = "Invocation FileStream class shold be replaced with IFileSystem.FileStream";
21+
private const string Title = "Invocation DirectoryInfo class shold be replaced with IFileSystem.DirectoryInfo";
2222

2323
/// <summary>
2424
/// Diagnostic Message Format
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 DriveInfoAnalyzer: BaseFileSystemNodeObjectCreationAnalyzer
11+
{
12+
/// <summary>
13+
/// Diagnostic Identifier
14+
/// </summary>
15+
[UsedImplicitly]
16+
public const string DiagnosticId = "IO0008";
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(DriveInfo);
54+
}
55+
}
56+
}

System.IO.Abstractions.Analyzers/Analyzers/FileSystemTypeAnalyzers/PathAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class PathAnalyzer : BaseFileSystemNodeStaticCallAnalyzer
1818
/// <summary>
1919
/// Diagnostic Title
2020
/// </summary>
21-
private const string Title = "Invocation File class shold be replaced with IFileSystem.File";
21+
private const string Title = "Invocation Path class shold be replaced with IFileSystem.Path";
2222

2323
/// <summary>
2424
/// Diagnostic Message Format

0 commit comments

Comments
 (0)