@@ -13,29 +13,6 @@ namespace Cement.Cli.Tests.ParsersTests;
13
13
[ TestFixture ]
14
14
internal class TestProjectFile
15
15
{
16
- private readonly string defaultCsprojXml =
17
- @"<?xml version=""1.0"" encoding=""utf-8""?>
18
- <Project ToolsVersion=""14.0"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
19
- <PropertyGroup>
20
- <RootNamespace>TestProject</RootNamespace>
21
- <AssemblyName>TestProject</AssemblyName>
22
- <CodeAnalysisRuleSet>Other.ruleset</CodeAnalysisRuleSet>
23
- </PropertyGroup>
24
- <PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "">
25
- <PropertyGroupWithCondition>true</PropertyGroupWithCondition>
26
- <CodeAnalysisRuleSet>Another.ruleset</CodeAnalysisRuleSet>
27
- </PropertyGroup>
28
- <ItemGroup>
29
- <Reference Include=""System"" />
30
- <Reference Include=""LalalaReference"">
31
- <HintPath>../../lalala/LalalaReference.dll</HintPath>
32
- </Reference>
33
- </ItemGroup>
34
- <ItemGroup>
35
- <Analyzer Include=""Other.dll"" />
36
- <Analyzer Include=""dummyDir/Another.dll"" />
37
- </ItemGroup>
38
- </Project>" ;
39
16
private TempDirectory workDirectory = new ( ) ;
40
17
41
18
[ SetUp ]
@@ -169,12 +146,13 @@ public void TestReplaceReferencePath()
169
146
Assert . AreEqual ( "abc/def" , refXml . LastChild . InnerText ) ;
170
147
}
171
148
172
- [ Test ]
173
- public void Costructor_GetXmlFromFile_IfFileExist ( )
149
+ [ TestCase ( DefaultOldCsprojXml , TestName = "OldCsprojFormat" ) ]
150
+ [ TestCase ( DefaultNewCsprojXml , TestName = "NewCsprojFormat" ) ]
151
+ public void Constructor_GetXmlFromFile_IfFileExist ( string csprojXml )
174
152
{
175
- var projectFile = CreateProjectFile ( defaultCsprojXml ) ;
153
+ var projectFile = CreateProjectFile ( csprojXml ) ;
176
154
177
- Assert . AreEqual ( WithoutXmlFormatting ( defaultCsprojXml ) , projectFile . Document . OuterXml ) ;
155
+ Assert . That ( projectFile . Document . OuterXml , Is . EqualTo ( WithoutXmlFormatting ( csprojXml ) ) ) ;
178
156
}
179
157
180
158
[ Test ]
@@ -188,41 +166,43 @@ public void Costructor_ThrowException_IfFileNotExist()
188
166
Assert . Throws < FileNotFoundException > ( ( ) => new ProjectFile ( nullPath ) ) ;
189
167
}
190
168
191
- [ Test ]
192
- public void BindRuleset_NoBindRuleset_IfBindingAlreadyExists ( )
169
+ [ TestCase ( DefaultOldCsprojXml , TestName = "OldCsprojFormat" ) ]
170
+ [ TestCase ( DefaultNewCsprojXml , TestName = "NewCsprojFormat" ) ]
171
+ public void BindRuleset_NoBindRuleset_IfBindingAlreadyExists ( string csprojXml )
193
172
{
194
- var rulesetName = "Other.ruleset" ;
173
+ const string rulesetName = "Other.ruleset" ;
195
174
var rulesetPath = Path . Combine ( workDirectory . Path , rulesetName ) ;
196
175
var rulesetFile = new RulesetFile ( rulesetPath ) ;
197
- var projectFile = CreateProjectFile ( defaultCsprojXml ) ;
176
+ var projectFile = CreateProjectFile ( csprojXml ) ;
198
177
199
178
projectFile . BindRuleset ( rulesetFile ) ;
200
179
201
180
Console . WriteLine ( "ProjectFile should be contains 2 old rulesetBindings:" ) ;
202
181
Console . WriteLine ( projectFile . Document . OuterXml ) ;
203
182
var rulesetBindings = SearchByXpath ( projectFile . Document , "//a:CodeAnalysisRuleSet" ) ;
204
- Assert . AreEqual ( 2 , rulesetBindings . Count ) ;
183
+ Assert . That ( rulesetBindings , Has . Count . EqualTo ( 2 ) ) ;
205
184
}
206
185
207
- [ Test ]
208
- public void BindRuleset_MoveOldRulesetBindings_FromProjectFileToRulesetFile ( )
186
+ [ TestCase ( DefaultOldCsprojXml , TestName = "OldCsprojFormat" ) ]
187
+ [ TestCase ( DefaultNewCsprojXml , TestName = "NewCsprojFormat" ) ]
188
+ public void BindRuleset_MoveOldRulesetBindings_FromProjectFileToRulesetFile ( string csprojXml )
209
189
{
210
190
var rulesetName = $ "{ Guid . NewGuid ( ) } .ruleset";
211
191
var rulesetPath = Path . Combine ( workDirectory . Path , rulesetName ) ;
212
192
var rulesetFile = new RulesetFile ( rulesetPath ) ;
213
- var projectFile = CreateProjectFile ( defaultCsprojXml ) ;
193
+ var projectFile = CreateProjectFile ( csprojXml ) ;
214
194
215
195
projectFile . BindRuleset ( rulesetFile ) ;
216
196
217
197
Console . WriteLine ( $ "ProjectFile should be contains only one CodeAnalysisRuleSet with value '{ rulesetName } ':") ;
218
198
Console . WriteLine ( projectFile . Document . OuterXml ) ;
219
199
var rulesetBinding = SearchByXpath ( projectFile . Document , "//a:CodeAnalysisRuleSet" ) . Single ( ) ;
220
- Assert . AreEqual ( rulesetName , rulesetBinding . InnerText ) ;
200
+ Assert . That ( rulesetBinding . InnerText , Is . EqualTo ( rulesetName ) ) ;
221
201
222
202
Console . WriteLine ( "RulesetFile should be contains oldRulesetBindings:" ) ;
223
203
Console . WriteLine ( rulesetFile . Document . OuterXml ) ;
224
204
var oldRulesetBindings = SearchByXpath ( rulesetFile . Document , "//a:Include" ) ;
225
- Assert . AreEqual ( 2 , oldRulesetBindings . Count ) ;
205
+ Assert . That ( oldRulesetBindings , Has . Count . EqualTo ( 2 ) ) ;
226
206
}
227
207
228
208
[ Test ]
@@ -238,23 +218,37 @@ public void AddAnalyzer_CreateItemGroupForAnalyzers_IfNotExists()
238
218
}
239
219
240
220
[ Test ]
241
- public void AddAnalyzer_AddAnalyzersDll_IfNotExists ( )
221
+ public void AddAnalyzer_CreateItemGroupForAnalyzers_IfNotExists_ForNewCsprojFormat ( )
222
+ {
223
+ var analyzerDllPath = Path . Combine ( workDirectory . Path , $ "{ Guid . NewGuid ( ) } .dll") ;
224
+ const string csprojContent = @"<Project Sdk=""Microsoft.NET.Sdk""><PropertyGroup><TargetFramework>net6.0</TargetFramework></PropertyGroup></Project>" ;
225
+ var projectFile = CreateProjectFile ( csprojContent ) ;
226
+
227
+ projectFile . AddAnalyzer ( analyzerDllPath ) ;
228
+
229
+ Assert . That ( SearchByXpath ( projectFile . Document , "//a:ItemGroup[a:Analyzer]" ) . Single ( ) , Is . Not . Null ) ;
230
+ }
231
+
232
+ [ TestCase ( DefaultOldCsprojXml , TestName = "OldCsprojFormat" ) ]
233
+ [ TestCase ( DefaultNewCsprojXml , TestName = "NewCsprojFormat" ) ]
234
+ public void AddAnalyzer_AddAnalyzersDll_IfNotExists ( string csprojXml )
242
235
{
243
236
var analyzerDllRelpath = $ "{ Guid . NewGuid ( ) } .dll";
244
237
var analyzerDllFullPath = Path . Combine ( workDirectory . Path , analyzerDllRelpath ) ;
245
- var projectFile = CreateProjectFile ( defaultCsprojXml ) ;
238
+ var projectFile = CreateProjectFile ( csprojXml ) ;
246
239
247
240
projectFile . AddAnalyzer ( analyzerDllFullPath ) ;
248
241
249
242
Assert . NotNull ( SearchByXpath ( projectFile . Document , $ "//a:ItemGroup/a:Analyzer[@Include = '{ analyzerDllRelpath } ']") . Single ( ) ) ;
250
243
}
251
244
252
- [ Test ]
253
- public void AddAnalyzer_NotAddAnalyzersDll_IfDllWithSomeNameAlreadyAdded ( )
245
+ [ TestCase ( DefaultOldCsprojXml , TestName = "OldCsprojFormat" ) ]
246
+ [ TestCase ( DefaultNewCsprojXml , TestName = "NewCsprojFormat" ) ]
247
+ public void AddAnalyzer_NotAddAnalyzersDll_IfDllWithSomeNameAlreadyAdded ( string csprojXml )
254
248
{
255
- var analyzerDllRelpath = "Another.dll" ;
256
- var analyzerDllFullPath = Path . Combine ( workDirectory . Path , analyzerDllRelpath ) ;
257
- var projectFile = CreateProjectFile ( defaultCsprojXml ) ;
249
+ const string analyzerDllFileName = "Another.dll" ;
250
+ var analyzerDllFullPath = Path . Combine ( workDirectory . Path , analyzerDllFileName ) ;
251
+ var projectFile = CreateProjectFile ( csprojXml ) ;
258
252
259
253
projectFile . AddAnalyzer ( analyzerDllFullPath ) ;
260
254
@@ -328,4 +322,55 @@ private List<XmlNode> SearchByXpath(XmlDocument xmlDocument, string xpath)
328
322
. ToList ( ) ;
329
323
return result ;
330
324
}
325
+
326
+ private const string DefaultOldCsprojXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
327
+ <Project ToolsVersion=""14.0"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
328
+ <PropertyGroup>
329
+ <RootNamespace>TestProject</RootNamespace>
330
+ <AssemblyName>TestProject</AssemblyName>
331
+ <CodeAnalysisRuleSet>Other.ruleset</CodeAnalysisRuleSet>
332
+ </PropertyGroup>
333
+ <PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "">
334
+ <PropertyGroupWithCondition>true</PropertyGroupWithCondition>
335
+ <CodeAnalysisRuleSet>Another.ruleset</CodeAnalysisRuleSet>
336
+ </PropertyGroup>
337
+ <ItemGroup>
338
+ <Reference Include=""System"" />
339
+ <Reference Include=""LalalaReference"">
340
+ <HintPath>../../lalala/LalalaReference.dll</HintPath>
341
+ </Reference>
342
+ </ItemGroup>
343
+ <ItemGroup>
344
+ <Analyzer Include=""Other.dll"" />
345
+ <Analyzer Include=""dummyDir/Another.dll"" />
346
+ </ItemGroup>
347
+ </Project>" ;
348
+
349
+ private const string DefaultNewCsprojXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
350
+ <Project Sdk=""Microsoft.NET.Sdk"">
351
+ <PropertyGroup>
352
+ <TargetFramework>net6.0</TargetFramework>
353
+ <NoWarn>1701;1702;1591;1573</NoWarn>
354
+ <OutputType>Exe</OutputType>
355
+ <Nullable>enable</Nullable>
356
+ <CodeAnalysisRuleSet>Other.ruleset</CodeAnalysisRuleSet>
357
+ </PropertyGroup>
358
+ <PropertyGroup Condition=""condition"">
359
+ <NoWarn>1</NoWarn>
360
+ <CodeAnalysisRuleSet>Another.ruleset</CodeAnalysisRuleSet>
361
+ </PropertyGroup>
362
+ <ItemGroup>
363
+ <Reference Include=""LalalaReference"">
364
+ <SpecificVersion>False</SpecificVersion>
365
+ <HintPath>..\..\..\lalalal\bin\LalalaReference.dll</HintPath>
366
+ </Reference>
367
+ </ItemGroup>
368
+ <ItemGroup>
369
+ <PackageReference Include=""Newtonsoft.Json"" Version=""13.0.1"" />
370
+ </ItemGroup>
371
+ <ItemGroup>
372
+ <Analyzer Include=""Other.dll"" />
373
+ <Analyzer Include=""dummyDir/Another.dll"" />
374
+ </ItemGroup>
375
+ </Project>" ;
331
376
}
0 commit comments