Skip to content

Commit 828353a

Browse files
committed
Minor improvements in ProjectContractRemover
1 parent 7785678 commit 828353a

7 files changed

+308
-8
lines changed

src/CodeContractsRemover/ProjectContractRemover.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
namespace CodeContractsRemover
88
{
99
public class ProjectContractRemover
10-
{
10+
{
11+
private const string JbAnnotations = "JETBRAINS_ANNOTATIONS";
1112
private readonly string _filePath;
1213
private readonly AnnotationsMode _mode;
1314
private readonly ProjectRootElement _project;
1415
private bool _changed;
16+
private bool _hadContracts;
1517

1618
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
1719
public ProjectContractRemover(string filePath, AnnotationsMode mode)
@@ -36,7 +38,7 @@ public void Process()
3638

3739
RemoveCodeContracts();
3840

39-
if (_mode == AnnotationsMode.Add || _mode == AnnotationsMode.IncludeIntoBinaries)
41+
if (_hadContracts && (_mode == AnnotationsMode.Add || _mode == AnnotationsMode.IncludeIntoBinaries))
4042
{
4143
AddAnnotations();
4244
}
@@ -74,21 +76,26 @@ private void AddAnnotations()
7476
var addDefaultConst = true;
7577
foreach (var element in consts)
7678
{
77-
element.Value = $"{element.Value};JETBRAINS_ANNOTATIONS";
79+
var currentValues = element.Value.Split(';');
80+
if (!currentValues.Contains(JbAnnotations))
81+
{
82+
element.Value = $"{element.Value};{JbAnnotations}";
83+
_changed = true;
84+
}
85+
7886
if (string.IsNullOrEmpty(element.Parent.Condition))
7987
{
8088
addDefaultConst = false;
8189
}
82-
}
83-
90+
}
91+
8492
if (addDefaultConst)
8593
{
8694
//add const without conditions
8795
var gr = _project.PropertyGroups.Where(g => string.IsNullOrEmpty(g.Condition)).First();
88-
gr.AddProperty("DefineConstants", "JETBRAINS_ANNOTATIONS");
96+
gr.AddProperty("DefineConstants", JbAnnotations);
97+
_changed = true;
8998
}
90-
91-
_changed = true;
9299
}
93100
}
94101

@@ -111,18 +118,21 @@ private void RemoveCodeContracts()
111118
else if (constDefinition.Value.Contains("CONTRACTS_FULL;"))
112119
{
113120
_changed = true;
121+
_hadContracts = true;
114122
constDefinition.Value = constDefinition.Value.Replace("CONTRACTS_FULL;", string.Empty);
115123
}
116124
else if (constDefinition.Value.Contains(";CONTRACTS_FULL"))
117125
{
118126
_changed = true;
127+
_hadContracts = true;
119128
constDefinition.Value = constDefinition.Value.Replace(";CONTRACTS_FULL", string.Empty);
120129
}
121130
}
122131

123132
foreach (var prop in propsToRemove)
124133
{
125134
_changed = true;
135+
_hadContracts = true;
126136
pg.RemoveChild(prop);
127137
}
128138
}

src/CodeContractsRemoverTests/CodeContractsRemoverTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<Compile Remove="TestCases\TestSubject.cs" />
3232
</ItemGroup>
3333
<ItemGroup>
34+
<Content Include="TestCases\ProjectNoCc.csproj">
35+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
36+
</Content>
3437
<Content Include="TestCases\StringNotNullOrEmptyCase.cs">
3538
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3639
</Content>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net40</TargetFramework>
4+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
6+
<AssemblyTitle>Title</AssemblyTitle>
7+
<Description>Descr</Description>
8+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
9+
<DocumentationFile>bin\$(Configuration)\Project.xml</DocumentationFile>
10+
<OutputPath>bin\$(Configuration)\</OutputPath>
11+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
12+
<RunCodeAnalysis>false</RunCodeAnalysis>
13+
<DefineConstants>JETBRAINS_ANNOTATIONS</DefineConstants>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugType>full</DebugType>
17+
<DefineConstants>TRACE;DEBUG;CODE_ANALYSIS;JETBRAINS_ANNOTATIONS</DefineConstants>
18+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
19+
<NoWarn>1591</NoWarn>
20+
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
21+
</PropertyGroup>
22+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
23+
<DebugType>pdbonly</DebugType>
24+
<DefineConstants>TRACE;INCLUDE_IN_DISTRIB;JETBRAINS_ANNOTATIONS</DefineConstants>
25+
<CodeAnalysisRuleSet>..\..\CodeAnalysis\my.ruleset</CodeAnalysisRuleSet>
26+
</PropertyGroup>
27+
<PropertyGroup>
28+
<SignAssembly>true</SignAssembly>
29+
</PropertyGroup>
30+
<PropertyGroup>
31+
<AssemblyOriginatorKeyFile>KeyPair.snk</AssemblyOriginatorKeyFile>
32+
</PropertyGroup>
33+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
34+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
35+
<PropertyGroup>
36+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
37+
</PropertyGroup>
38+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
39+
</Target>
40+
<ItemGroup>
41+
<Reference Include="System.Configuration" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="..\ProductInfo.cs" Link="Properties\ProductInfo.cs" />
47+
<Compile Update="Properties\Resources.Designer.cs">
48+
<AutoGen>True</AutoGen>
49+
<DesignTime>True</DesignTime>
50+
<DependentUpon>Resources.resx</DependentUpon>
51+
</Compile>
52+
</ItemGroup>
53+
<ItemGroup>
54+
<EmbeddedResource Update="Properties\Resources.resx">
55+
<Generator>ResXFileCodeGenerator</Generator>
56+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
57+
<SubType>Designer</SubType>
58+
</EmbeddedResource>
59+
</ItemGroup>
60+
<ItemGroup>
61+
<None Include="KeyPair.snk" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
65+
</ItemGroup>
66+
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net40</TargetFramework>
4+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
6+
<AssemblyTitle>Title</AssemblyTitle>
7+
<Description>Descr</Description>
8+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
9+
<DocumentationFile>bin\$(Configuration)\Project.xml</DocumentationFile>
10+
<OutputPath>bin\$(Configuration)\</OutputPath>
11+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
12+
<RunCodeAnalysis>false</RunCodeAnalysis>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
<DebugType>full</DebugType>
16+
<DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
17+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
18+
<NoWarn>1591</NoWarn>
19+
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
20+
</PropertyGroup>
21+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22+
<DebugType>pdbonly</DebugType>
23+
<DefineConstants>TRACE;INCLUDE_IN_DISTRIB</DefineConstants>
24+
<CodeAnalysisRuleSet>..\..\CodeAnalysis\my.ruleset</CodeAnalysisRuleSet>
25+
</PropertyGroup>
26+
<PropertyGroup>
27+
<SignAssembly>true</SignAssembly>
28+
</PropertyGroup>
29+
<PropertyGroup>
30+
<AssemblyOriginatorKeyFile>KeyPair.snk</AssemblyOriginatorKeyFile>
31+
</PropertyGroup>
32+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
33+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
34+
<PropertyGroup>
35+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
36+
</PropertyGroup>
37+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
38+
</Target>
39+
<ItemGroup>
40+
<Reference Include="System.Configuration" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="Microsoft.CSharp" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="..\ProductInfo.cs" Link="Properties\ProductInfo.cs" />
46+
<Compile Update="Properties\Resources.Designer.cs">
47+
<AutoGen>True</AutoGen>
48+
<DesignTime>True</DesignTime>
49+
<DependentUpon>Resources.resx</DependentUpon>
50+
</Compile>
51+
</ItemGroup>
52+
<ItemGroup>
53+
<EmbeddedResource Update="Properties\Resources.resx">
54+
<Generator>ResXFileCodeGenerator</Generator>
55+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
56+
<SubType>Designer</SubType>
57+
</EmbeddedResource>
58+
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="KeyPair.snk" />
61+
</ItemGroup>
62+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net40</TargetFramework>
4+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
6+
<AssemblyTitle>Title</AssemblyTitle>
7+
<Description>Descr</Description>
8+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
9+
<DocumentationFile>bin\$(Configuration)\Project.xml</DocumentationFile>
10+
<OutputPath>bin\$(Configuration)\</OutputPath>
11+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
12+
<RunCodeAnalysis>false</RunCodeAnalysis>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
<DebugType>full</DebugType>
16+
<DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
17+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
18+
<NoWarn>1591</NoWarn>
19+
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
20+
</PropertyGroup>
21+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22+
<DebugType>pdbonly</DebugType>
23+
<DefineConstants>TRACE;INCLUDE_IN_DISTRIB</DefineConstants>
24+
<CodeAnalysisRuleSet>..\..\CodeAnalysis\my.ruleset</CodeAnalysisRuleSet>
25+
</PropertyGroup>
26+
<PropertyGroup>
27+
<SignAssembly>true</SignAssembly>
28+
</PropertyGroup>
29+
<PropertyGroup>
30+
<AssemblyOriginatorKeyFile>KeyPair.snk</AssemblyOriginatorKeyFile>
31+
</PropertyGroup>
32+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
33+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
34+
<PropertyGroup>
35+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
36+
</PropertyGroup>
37+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
38+
</Target>
39+
<ItemGroup>
40+
<Reference Include="System.Configuration" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="Microsoft.CSharp" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="..\ProductInfo.cs" Link="Properties\ProductInfo.cs" />
46+
<Compile Update="Properties\Resources.Designer.cs">
47+
<AutoGen>True</AutoGen>
48+
<DesignTime>True</DesignTime>
49+
<DependentUpon>Resources.resx</DependentUpon>
50+
</Compile>
51+
</ItemGroup>
52+
<ItemGroup>
53+
<EmbeddedResource Update="Properties\Resources.resx">
54+
<Generator>ResXFileCodeGenerator</Generator>
55+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
56+
<SubType>Designer</SubType>
57+
</EmbeddedResource>
58+
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="KeyPair.snk" />
61+
</ItemGroup>
62+
</Project>

src/CodeContractsRemoverTests/ProjectContractRemoverTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IO;
22
using System.Text;
33
using ApprovalTests;
4+
using ApprovalTests.Namers;
45
using ApprovalTests.Reporters;
56
using CodeContractsRemover;
67
using NUnit.Framework;
@@ -51,5 +52,39 @@ public void Process_IncludeAnnotationsIntoBinaries_ResultIsCorrect()
5152
//Assert
5253
Approvals.VerifyFile(file);
5354
}
55+
56+
[Test]
57+
public void Process_ProcessManyTimes_ResultIsCorrect()
58+
{
59+
//Arrange
60+
var file = $"{nameof(Process_ProcessManyTimes_ResultIsCorrect)}.received.csproj";
61+
File.Copy(TestsHelper.GetInitialFileName(".\\TestCases\\Project.csproj"), file);
62+
63+
new ProjectContractRemover(file, AnnotationsMode.IncludeIntoBinaries).Process();
64+
65+
//Act
66+
//Process second time
67+
new ProjectContractRemover(file, AnnotationsMode.IncludeIntoBinaries).Process();
68+
69+
//Assert
70+
Approvals.VerifyFile(file);
71+
}
72+
73+
[Test]
74+
[TestCase(AnnotationsMode.Add)]
75+
[TestCase(AnnotationsMode.IncludeIntoBinaries)]
76+
public void Process_ProjectWithoutCc_CsprojNotModified(AnnotationsMode mode)
77+
{
78+
//Arrange
79+
var file = $"{nameof(Process_ProjectWithoutCc_CsprojNotModified)}.{mode}.received.csproj";
80+
File.Copy(TestsHelper.GetInitialFileName(".\\TestCases\\ProjectNoCc.csproj"), file);
81+
82+
//Act
83+
new ProjectContractRemover(file, mode).Process();
84+
85+
//Assert
86+
NamerFactory.AdditionalInformation = mode.ToString();
87+
Approvals.VerifyFile(file);
88+
}
5489
}
5590
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net40</TargetFramework>
4+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
6+
<AssemblyTitle>Title</AssemblyTitle>
7+
<Description>Descr</Description>
8+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
9+
<DocumentationFile>bin\$(Configuration)\Project.xml</DocumentationFile>
10+
<OutputPath>bin\$(Configuration)\</OutputPath>
11+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
12+
<RunCodeAnalysis>false</RunCodeAnalysis>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
<DebugType>full</DebugType>
16+
<DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
17+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
18+
<NoWarn>1591</NoWarn>
19+
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
20+
</PropertyGroup>
21+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22+
<DebugType>pdbonly</DebugType>
23+
<DefineConstants>TRACE;INCLUDE_IN_DISTRIB</DefineConstants>
24+
<CodeAnalysisRuleSet>..\..\CodeAnalysis\my.ruleset</CodeAnalysisRuleSet>
25+
</PropertyGroup>
26+
<PropertyGroup>
27+
<SignAssembly>true</SignAssembly>
28+
</PropertyGroup>
29+
<PropertyGroup>
30+
<AssemblyOriginatorKeyFile>KeyPair.snk</AssemblyOriginatorKeyFile>
31+
</PropertyGroup>
32+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
33+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
34+
<PropertyGroup>
35+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
36+
</PropertyGroup>
37+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
38+
</Target>
39+
<ItemGroup>
40+
<Reference Include="System.Configuration" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="Microsoft.CSharp" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="..\ProductInfo.cs" Link="Properties\ProductInfo.cs" />
46+
<Compile Update="Properties\Resources.Designer.cs">
47+
<AutoGen>True</AutoGen>
48+
<DesignTime>True</DesignTime>
49+
<DependentUpon>Resources.resx</DependentUpon>
50+
</Compile>
51+
</ItemGroup>
52+
<ItemGroup>
53+
<EmbeddedResource Update="Properties\Resources.resx">
54+
<Generator>ResXFileCodeGenerator</Generator>
55+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
56+
<SubType>Designer</SubType>
57+
</EmbeddedResource>
58+
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="KeyPair.snk" />
61+
</ItemGroup>
62+
</Project>

0 commit comments

Comments
 (0)