Skip to content

Commit 807491c

Browse files
committed
Implement PathCodeFix
1 parent e27512e commit 807491c

6 files changed

Lines changed: 91 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using System.IO.Abstractions.Analyzers.Analyzers.FileSystemTypeAnalyzers;
3+
using System.IO.Abstractions.Analyzers.CodeFixes;
4+
using Microsoft.CodeAnalysis;
5+
using Roslyn.Testing.CodeFix;
6+
using Xunit;
7+
8+
namespace System.IO.Abstractions.Analyzers.Tests.CodeFixes
9+
{
10+
public class PathCodeFixTests :
11+
CSharpCodeFixProviderTest<PathAnalyzer, PathCodeFix>
12+
{
13+
[Theory]
14+
[InlineData("BeforeFix.txt", "AfterFix.txt")]
15+
public void CodeFix(string sourceBefore, string sourceAfter)
16+
{
17+
var sourceBeforeFix = ReadFile(sourceBefore);
18+
var sourceAfterFix = ReadFile(sourceAfter);
19+
VerifyFix(sourceBeforeFix, sourceAfterFix, 0, true);
20+
}
21+
22+
protected override IEnumerable<MetadataReference> GetAdditionalReferences() => new[]
23+
{
24+
MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
25+
};
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO.Abstractions;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
private readonly IFileSystem _fileSystem;
8+
9+
public WithOutFileSystem(IFileSystem fileSystem)
10+
{
11+
_fileSystem = fileSystem;
12+
}
13+
14+
public void SomeMethod()
15+
{
16+
const string filePath = "C:\\temp.txt";
17+
18+
_fileSystem.Path.Exists(filePath);
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO.Abstractions;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
private readonly IFileSystem _fileSystem;
8+
9+
public WithOutFileSystem(IFileSystem fileSystem)
10+
{
11+
_fileSystem = fileSystem;
12+
}
13+
14+
public void SomeMethod()
15+
{
16+
const string filePath = "C:\\temp.txt";
17+
18+
Path.Exists(filePath);
19+
}
20+
}
21+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PathAnalyzer : BaseFileSystemNodeAnalyzer
1313
/// Diagnostic Identifier
1414
/// </summary>
1515
[UsedImplicitly]
16-
public const string DiagnosticId = "IO0006";
16+
public const string DiagnosticId = Constants.Io0006;
1717

1818
/// <summary>
1919
/// Diagnostic Title
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Composition;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.CodeFixes;
4+
5+
namespace System.IO.Abstractions.Analyzers.CodeFixes
6+
{
7+
[Shared]
8+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PathCodeFix))]
9+
public class PathCodeFix: BaseInvokeCodeFix
10+
{
11+
protected override string DiagnosticId => Constants.Io0006;
12+
13+
protected override string Title => "Use IFileSystem.Path for improved testablity";
14+
}
15+
}

System.IO.Abstractions.Analyzers/Constants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ public static class Constants
1717
public const string Io0001 = "IO0001";
1818

1919
public const string Io0002 = "IO0002";
20+
21+
public const string Io0003 = "IO0003";
22+
23+
public const string Io0004 = "IO0004";
24+
public const string Io0005 = "IO0005";
25+
public const string Io0006 = "IO0006";
2026
}
2127
}

0 commit comments

Comments
 (0)