Skip to content

Commit f158491

Browse files
committed
Implement dapper string analyzer
1 parent 3003130 commit f158491

11 files changed

Lines changed: 368 additions & 43 deletions

appveyor.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 1.0.{build}
2+
image: Visual Studio 2019
3+
install:
4+
- dotnet tool install -g Cake.Tool --version 0.33.0
5+
build_script:
6+
- cmd: dotnet cake -Target=CI
7+
test: off
8+
cache:
9+
- '%USERPROFILE%\.sonar\cache'
10+
- '%USERPROFILE%\.nuget\packages -> **\*.csproj'
11+
- 'tools -> build.cake'

build.cake

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#tool nuget:?package=Codecov
2+
#addin nuget:?package=Cake.Codecov
3+
4+
#tool nuget:?package=MSBuild.SonarQube.Runner.Tool
5+
#addin nuget:?package=Cake.Sonar
6+
7+
var target = Argument("target", "Default");
8+
9+
var buildConfiguration = "Release";
10+
11+
var projectName = "Sql.Analyzer";
12+
var testProjectName = "Sql.Analyzer.Test";
13+
14+
var solutionFile = string.Format("./src/{0}.sln", projectName);
15+
var projectFolder = string.Format("./src/{0}/{0}/", projectName);
16+
var vsixProjectFolder = string.Format("./src/{0}/Sql.Analyzer.Vsix/", projectName);
17+
var testProjectFolder = string.Format("./src/{0}/{1}/", projectName, testProjectName);
18+
var testProjectFile = string.Format("{0}{1}.csproj", testProjectFolder, testProjectName);
19+
20+
var vsixFile = string.Format("{0}bin/{1}/{2}.vsix", vsixProjectFolder, buildConfiguration, projectName);
21+
22+
var projectFile = string.Format("{0}{1}.csproj", projectFolder, projectName);
23+
var extensionsVersion = XmlPeek(projectFile, "Project/PropertyGroup/Version/text()");
24+
25+
var nugetPackage = string.Format("{0}bin/{1}/{2}.{3}.nupkg", projectFolder, buildConfiguration, projectName, extensionsVersion);
26+
27+
Information(nugetPackage);
28+
Information(vsixFile);
29+
30+
Task("UpdateBuildVersion")
31+
.WithCriteria(BuildSystem.AppVeyor.IsRunningOnAppVeyor)
32+
.Does(() =>
33+
{
34+
var buildNumber = BuildSystem.AppVeyor.Environment.Build.Number;
35+
36+
BuildSystem.AppVeyor.UpdateBuildVersion(string.Format("{0}.{1}", extensionsVersion, buildNumber));
37+
});
38+
39+
Task("Build")
40+
.Does(() =>
41+
{
42+
var settings = new MSBuildSettings
43+
{
44+
Configuration = buildConfiguration,
45+
ToolVersion = MSBuildToolVersion.VS2019,
46+
MSBuildPlatform = MSBuildPlatform.x86,
47+
Restore = true,
48+
Verbosity = Verbosity.Minimal
49+
};
50+
51+
MSBuild(solutionFile, settings);
52+
});
53+
54+
Task("Test")
55+
.IsDependentOn("Build")
56+
.Does(() =>
57+
{
58+
var settings = new DotNetCoreTestSettings
59+
{
60+
Configuration = buildConfiguration
61+
};
62+
63+
DotNetCoreTest(testProjectFile, settings);
64+
});
65+
66+
Task("CodeCoverage")
67+
.IsDependentOn("Build")
68+
.Does(() =>
69+
{
70+
var settings = new DotNetCoreTestSettings
71+
{
72+
Configuration = buildConfiguration,
73+
ArgumentCustomization = args => args
74+
.Append("/p:CollectCoverage=true")
75+
.Append("/p:CoverletOutputFormat=opencover")
76+
};
77+
78+
DotNetCoreTest(testProjectFile, settings);
79+
80+
Codecov(string.Format("{0}coverage.opencover.xml", testProjectFolder), EnvironmentVariable("codecov:token"));
81+
});
82+
83+
Task("CreateArtifact")
84+
.IsDependentOn("Build")
85+
.WithCriteria(BuildSystem.AppVeyor.IsRunningOnAppVeyor)
86+
.Does(() =>
87+
{
88+
BuildSystem.AppVeyor.UploadArtifact(nugetPackage);
89+
BuildSystem.AppVeyor.UploadArtifact(vsixFile);
90+
});
91+
92+
Task("SonarBegin")
93+
.Does(() => {
94+
SonarBegin(new SonarBeginSettings {
95+
Url = "https://sonarcloud.io",
96+
Login = EnvironmentVariable("sonar:apikey"),
97+
Key = "sql-analyzer",
98+
Name = "Sql.Analyzer",
99+
ArgumentCustomization = args => args
100+
.Append($"/o:olsh-github"),
101+
Version = "1.0.0.0"
102+
});
103+
});
104+
105+
Task("SonarEnd")
106+
.Does(() => {
107+
SonarEnd(new SonarEndSettings {
108+
Login = EnvironmentVariable("sonar:apikey")
109+
});
110+
});
111+
112+
Task("Sonar")
113+
.IsDependentOn("SonarBegin")
114+
.IsDependentOn("Build")
115+
.IsDependentOn("SonarEnd");
116+
117+
Task("Default")
118+
.IsDependentOn("Test");
119+
120+
Task("CI")
121+
.IsDependentOn("UpdateBuildVersion")
122+
.IsDependentOn("Sonar")
123+
.IsDependentOn("CodeCoverage")
124+
.IsDependentOn("CreateArtifact");
125+
126+
RunTarget(target);

src/Sql.Analyzer.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29411.108
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sql.Analyzer", "Sql.Analyzer\Sql.Analyzer\Sql.Analyzer.csproj", "{D0D603A9-CD83-4437-AEAE-E393704BEE67}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sql.Analyzer", "Sql.Analyzer\Sql.Analyzer\Sql.Analyzer.csproj", "{D0D603A9-CD83-4437-AEAE-E393704BEE67}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sql.Analyzer.Test", "Sql.Analyzer\Sql.Analyzer.Test\Sql.Analyzer.Test.csproj", "{400F3C22-A769-416F-82BE-F38ED4A935D9}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sql.Analyzer.Test", "Sql.Analyzer\Sql.Analyzer.Test\Sql.Analyzer.Test.csproj", "{400F3C22-A769-416F-82BE-F38ED4A935D9}"
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sql.Analyzer.Vsix", "Sql.Analyzer\Sql.Analyzer.Vsix\Sql.Analyzer.Vsix.csproj", "{DE372B5D-B2D2-41B1-A88F-A3D7B651FF29}"
1111
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8677C437-092B-4C06-8E9C-DD32EE0F4109}"
13+
ProjectSection(SolutionItems) = preProject
14+
..\appveyor.yml = ..\appveyor.yml
15+
..\build.cake = ..\build.cake
16+
EndProjectSection
17+
EndProject
1218
Global
1319
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1420
Debug|Any CPU = Debug|Any CPU

src/Sql.Analyzer/Sql.Analyzer.Test/DapperStringParameterAnalyzerTests.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using Microsoft.CodeAnalysis;
2-
using Microsoft.CodeAnalysis.CodeFixes;
32
using Microsoft.CodeAnalysis.Diagnostics;
43
using Microsoft.VisualStudio.TestTools.UnitTesting;
5-
using System;
6-
using TestHelper;
7-
using Sql.Analyzer;
4+
85
using Sql.Analyzer.Test.Helpers;
96

7+
using TestHelper;
8+
109
namespace Sql.Analyzer.Test
1110
{
1211
[TestClass]
@@ -21,21 +20,37 @@ public void Empty_NotTriggered()
2120
}
2221

2322
[TestMethod]
24-
public void AwaitStringGetAsync_AnalyzerTriggered()
23+
public void InlineSqlWithStringArgument_AnalyzerTriggered()
2524
{
26-
var code = EmbeddedResourceHelper.ReadTestData("InlineSqlWithStringParameterTestData.cs");
25+
var code = EmbeddedResourceHelper.ReadTestData("InlineSqlWithStringArgumentTestData.cs");
2726

2827
var expected = new DiagnosticResult
2928
{
3029
Id = DapperStringParameterAnalyzer.DiagnosticId,
31-
Message = string.Format(DapperStringParameterAnalyzer.MessageFormat, "StringGetAsync"),
30+
Message = DapperStringParameterAnalyzer.MessageFormat,
3231
Severity = DiagnosticSeverity.Warning,
33-
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 12, 25) }
32+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 13, 46) }
3433
};
3534

3635
VerifyCSharpDiagnostic(code, expected);
3736
}
3837

38+
[TestMethod]
39+
public void InlineSqlWithoutStringArgument_NotTriggered()
40+
{
41+
var code = EmbeddedResourceHelper.ReadTestData("InlineSqlWithDbStringArgumentTestData.cs");
42+
43+
VerifyCSharpDiagnostic(code);
44+
}
45+
46+
[TestMethod]
47+
public void StoredProcedureWithStringArgument_NotTriggered()
48+
{
49+
var code = EmbeddedResourceHelper.ReadTestData("StoredProcedureWithStringArgumentTestData.cs");
50+
51+
VerifyCSharpDiagnostic(code);
52+
}
53+
3954
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
4055
{
4156
return new DapperStringParameterAnalyzer();

src/Sql.Analyzer/Sql.Analyzer.Test/Sql.Analyzer.Test.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Compile Remove="TestData\InlineSqlWithStringParameterTestData.cs" />
8+
<Compile Remove="TestData\InlineSqlWithDbStringArgumentTestData.cs" />
9+
<Compile Remove="TestData\InlineSqlWithStringArgumentTestData.cs" />
10+
<Compile Remove="TestData\StoredProcedureWithStringArgumentTestData.cs" />
911
</ItemGroup>
1012

1113
<ItemGroup>
12-
<EmbeddedResource Include="TestData\InlineSqlWithStringParameterTestData.cs" />
14+
<EmbeddedResource Include="TestData\InlineSqlWithDbStringArgumentTestData.cs" />
15+
<EmbeddedResource Include="TestData\InlineSqlWithStringArgumentTestData.cs" />
16+
<EmbeddedResource Include="TestData\StoredProcedureWithStringArgumentTestData.cs" />
1317
</ItemGroup>
1418

1519
<ItemGroup>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Data.SqlClient;
2+
using System.Threading.Tasks;
3+
4+
using Dapper;
5+
6+
namespace Sql.Analyzer.Test.TestData
7+
{
8+
public class InlineSqlWithDbStringArgumentTestData
9+
{
10+
private static async Task Main(string[] args)
11+
{
12+
var sql = new SqlConnection();
13+
sql.Execute("inline sql @param", new { param = new DbString() { IsAnsi = true, Value = "some_string" } });
14+
}
15+
}
16+
}

src/Sql.Analyzer/Sql.Analyzer.Test/TestData/InlineSqlWithStringParameterTestData.cs renamed to src/Sql.Analyzer/Sql.Analyzer.Test/TestData/InlineSqlWithStringArgumentTestData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class InlineSqlWithStringParameterTestData
1010
private static async Task Main(string[] args)
1111
{
1212
var sql = new SqlConnection();
13-
sql.Execute("inline sql", new { param = "some_string" });
13+
sql.Execute("inline sql @param", new { param = "some_string" });
1414
}
1515
}
1616
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Data;
2+
using System.Data.SqlClient;
3+
using System.Threading.Tasks;
4+
5+
using Dapper;
6+
7+
namespace Sql.Analyzer.Test.TestData
8+
{
9+
public class InlineSqlWithStringParameterTestData
10+
{
11+
private static async Task Main(string[] args)
12+
{
13+
var sql = new SqlConnection();
14+
sql.Execute("inline sql @param", new { param = "some_string" }, commandType: CommandType.StoredProcedure);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)