Skip to content

Commit c28cbe8

Browse files
Upgrade to include .net7.0 and netcore 3.1, and cleanup (#32)
* Upgrade to include .net7.0 and netcore 3.1, and cleanup * Switch away from multi framework targeting
1 parent 76fc267 commit c28cbe8

6 files changed

Lines changed: 36 additions & 44 deletions

File tree

ListingManager.Tests/ListingInformationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ListingInformationTests
1414
public void Constructor_GivenValidListings_PropertiesPopulatedSuccessfully(string listing,
1515
int chapterNumber, int listingNumber, string suffix, string description)
1616
{
17-
ListingInformation listingInformation = new ListingInformation(listing);
17+
ListingInformation listingInformation = new(listing);
1818

1919
Assert.AreEqual(chapterNumber, listingInformation.ChapterNumber);
2020
Assert.AreEqual(listingNumber, listingInformation.ListingNumber);

ListingManager.Tests/ListingManager.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net7.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77

@@ -12,7 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="IntelliTect.TestTools.Console" Version="1.0.0-CI-20181030-214503" />
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
1616
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
1717
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
1818
<PackageReference Include="Polly" Version="7.2.3" />

ListingManager.Tests/ListingManagerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,23 @@ public void GetAllExtraListings_ExtraListingsReturned()
6565
[Ignore]
6666
public void UpdateChapterListingNumbers_ListingsWithinListMissing_ListingsRenumbered()
6767
{
68-
List<string> filesToMake = new List<string>
68+
List<string> filesToMake = new()
6969
{
7070
"Listing01.01.SpecifyingLiteralValues.cs",
7171
"Listing01.02.cs",
7272
"Listing01.04.cs",
7373
"Listing01.06.Something.cs"
7474
};
7575

76-
List<string> expectedFiles = new List<string>
76+
List<string> expectedFiles = new()
7777
{
7878
"Listing01.01.SpecifyingLiteralValues.cs",
7979
"Listing01.02.cs",
8080
"Listing01.03.cs",
8181
"Listing01.04.Something.cs"
8282
};
8383

84-
List<string> toWrite = new List<string>
84+
List<string> toWrite = new()
8585
{
8686
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01",
8787
"{",
@@ -559,7 +559,7 @@ public void GenerateUnitTests_TestsGenerated()
559559
{
560560
string chapter = "Chapter01";
561561

562-
List<string> filesToCreate = new List<string>
562+
List<string> filesToCreate = new()
563563
{
564564
@"Listing01.01.Something.cs",
565565
@"Listing01.02A.cs",
@@ -609,7 +609,7 @@ private List<FileInfo> WriteFiles(DirectoryInfo targetDirectory, IEnumerable<str
609609
IEnumerable<string>? toWrite)
610610
{
611611
List<string> filesToWrite = toWrite?.ToList() ?? new List<string>();
612-
List<FileInfo> ret = new List<FileInfo>();
612+
List<FileInfo> ret = new();
613613
foreach (string file in fileNames)
614614
{
615615
ret.Add(WriteFile(targetDirectory, file, filesToWrite));

ListingManager/ListingInformation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ListingInformation
1515

1616
public ListingInformation(string listingPath)
1717
{
18-
Regex regex = new Regex(@"Listing(\d{2}).(\d{2})([A-Za-z]*)(\.{1}(.*))?.cs$");
18+
Regex regex = new(@"Listing(\d{2}).(\d{2})([A-Za-z]*)(\.{1}(.*))?.cs$");
1919

2020
var matches = regex.Match(listingPath);
2121

@@ -31,7 +31,7 @@ public ListingInformation(string listingPath)
3131
}
3232
else
3333
{
34-
throw new ArgumentException(nameof(listingPath));
34+
throw new ArgumentException("Listing information not successfully able to be parsed from listing path.", nameof(listingPath));
3535
}
3636
}
3737
}

ListingManager/ListingManager.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
104104
if (curListingData is null || !chapterOnly && !byFolder && listingNumber == curListingData.ListingNumber)
105105
{
106106
File.Copy(curListingData?.TemporaryPath, curListingData?.Path, true);
107-
if (testListingData.Where(x => x?.ListingNumber == curListingData.ListingNumber && x.ListingSuffix == curListingData.ListingSuffix).FirstOrDefault() is ListingInformation currentTestListingData)
107+
if (testListingData.FirstOrDefault(x => x?.ListingNumber == curListingData.ListingNumber && x.ListingSuffix == curListingData.ListingSuffix) is ListingInformation currentTestListingData)
108108
{
109109
File.Copy(currentTestListingData?.TemporaryPath, currentTestListingData?.Path, true);
110110
}
@@ -127,14 +127,14 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
127127

128128
UpdateListingNamespace(cur, listingChapterNumber,
129129
completeListingNumber,
130-
curListingData.ListingDescription, curListingData, verbose, preview);
130+
curListingData.ListingDescription, verbose, preview);
131131

132132
if (testListingData.Where(x => x?.ListingNumber == curListingData.ListingNumber && x.ListingSuffix == curListingData.ListingSuffix).FirstOrDefault() is ListingInformation curTestListingData)
133133
{
134134
Console.Write("Updating test. ");
135135
UpdateTestListingNamespace(curTestListingData.TemporaryPath, listingChapterNumber,
136136
completeListingNumber,
137-
curListingData.ListingDescription, curListingData, verbose, preview);
137+
curListingData.ListingDescription, verbose, preview);
138138
}
139139

140140
}
@@ -151,7 +151,7 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
151151
public static bool IsExtraListing(string path,
152152
string regexNamespace = @".*Listing\d{2}\.\d{2}(A|B|C|D).*\.cs$")
153153
{
154-
Regex fileNameRegex = new Regex(regexNamespace);
154+
Regex fileNameRegex = new(regexNamespace);
155155

156156
string directoryNameFull = Path.GetDirectoryName(path) ?? string.Empty;
157157
string directoryName = Path.GetFileName(directoryNameFull);
@@ -166,16 +166,15 @@ public static bool IsExtraListing(string path,
166166
/// <param name="chapterNumber">The chapter the listing belongs to</param>
167167
/// <param name="listingNumber">The updated listing number</param>
168168
/// <param name="listingData">The name of the listing to be included in the namespace/path</param>
169-
/// <param name="curListingData"></param>
170169
/// <param name="verbose">When true, enables verbose console output</param>
171170
/// <param name="preview">When true, leaves files in place and only print console output</param>
172171
private static void UpdateTestListingNamespace(string path, int chapterNumber, string listingNumber,
173-
string listingData, ListingInformation curListingData, bool verbose = false, bool preview = false)
172+
string listingData, bool verbose = false, bool preview = false)
174173
{
175174
string paddedChapterNumber = chapterNumber.ToString("00");
176175

177176
string regexSingleDigitListingWithSuffix = @"\d{1}[A-Za-z]";
178-
string paddedListingNumber = "";
177+
string paddedListingNumber;
179178
if (Regex.IsMatch(listingNumber, regexSingleDigitListingWithSuffix))
180179
{ //allows for keeping the original listing number with a suffix. e.g. "01A"
181180
paddedListingNumber = listingNumber.PadLeft(3, '0');
@@ -217,9 +216,8 @@ private static void UpdateTestListingNamespace(string path, int chapterNumber, s
217216
/// <param name="listingData">The name of the listing to be included in the namespace/path</param>
218217
/// <param name="verbose">When true, enables verbose console output</param>
219218
/// <param name="preview">When true, leaves files in place and only print console output</param>
220-
/// <param name="curListingData">An instance of ListingInformation for the current listing.</param>
221219
private static void UpdateListingNamespace(string path, int chapterNumber, string listingNumber,
222-
string listingData, ListingInformation curListingData, bool verbose = false, bool preview = false)
220+
string listingData, bool verbose = false, bool preview = false)
223221
{
224222
string paddedChapterNumber = chapterNumber.ToString("00");
225223

@@ -265,41 +263,39 @@ private static void UpdateNamespaceOfPath(string path, string newNamespace, stri
265263

266264
string targetPath = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, newFileName) ?? path;
267265

268-
using (TextWriter textWriter = new StreamWriter(targetPath, true))
266+
using TextWriter textWriter = new StreamWriter(targetPath, true);
267+
foreach (string line in allLinesInFile)
269268
{
270-
foreach (string line in allLinesInFile)
269+
if (line.StartsWith("namespace"))
271270
{
272-
if (line.StartsWith("namespace"))
271+
if (line.TrimEnd().EndsWith(";"))
273272
{
274-
if (line.TrimEnd().EndsWith(";"))
275-
{
276-
textWriter.WriteLine("namespace " + newNamespace + ";");
277-
}
278-
else
279-
{
280-
textWriter.WriteLine("namespace " + newNamespace);
281-
}
273+
textWriter.WriteLine("namespace " + newNamespace + ";");
282274
}
283275
else
284276
{
285-
textWriter.WriteLine(line);
277+
textWriter.WriteLine("namespace " + newNamespace);
286278
}
287279
}
280+
else
281+
{
282+
textWriter.WriteLine(line);
283+
}
288284
}
289285
}
290286

291287
public static bool GetPathToAccompanyingUnitTest(string listingPath, out string pathToTest)
292288
{
293289
string testDirectory = $"{Path.GetDirectoryName(listingPath)}.Tests";
294290

295-
Regex regex = new Regex(@"((Listing\d{2}\.\d{2})([A-Z]?)((\.Tests)?)).*\.cs.tmp$");
291+
Regex regex = new(@"((Listing\d{2}\.\d{2})([A-Z]?)((\.Tests)?)).*\.cs.tmp$");
296292

297293
Match fileNameMatch = regex.Match(listingPath);
298294

299295
string testFileName = fileNameMatch.Success ? regex.Match(listingPath).Groups[1].Value : "";
300296

301297
Regex pathToTestRegex =
302-
new Regex(Regex.Escape($"{testDirectory}{Path.DirectorySeparatorChar}{testFileName}")
298+
new(Regex.Escape($"{testDirectory}{Path.DirectorySeparatorChar}{testFileName}")
303299
+ @".*\.cs");
304300

305301
if (Directory.Exists(testDirectory))
@@ -326,12 +322,12 @@ private static string GetTestLayout(string chapterNumber, string listingNumber)
326322
listingNumber.PadLeft(2, '0')) + TestBodyLayout;
327323
}
328324

329-
private static string TestHeaderLayout =
325+
private static readonly string TestHeaderLayout =
330326
@"using Microsoft.VisualStudio.TestTools.UnitTesting;
331327
332328
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter{0}.Listing{0}_{1}.Tests";
333329

334-
private static string TestBodyLayout =
330+
private static readonly string TestBodyLayout =
335331
@"
336332
{
337333
[TestClass]
@@ -393,7 +389,7 @@ private static bool GenerateUnitTest(string pathToTest)
393389
return false;
394390
}
395391

396-
Regex getListingData = new Regex(@"Listing(\d{2})\.(\d{2})");
392+
Regex getListingData = new(@"Listing(\d{2})\.(\d{2})");
397393

398394
var match = getListingData.Match(pathToTest);
399395

ListingManager/ListingManager.csproj

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net7.0</TargetFramework>
66
<PackAsTool>true</PackAsTool>
77
<ToolCommandName>ListingManager</ToolCommandName>
88
<RepositoryType>git</RepositoryType>
@@ -32,19 +32,15 @@
3232
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<None Include="../README.md" Pack="true" PackagePath="\"/>
36-
<SourceRoot Include="$(MSBuildThisFileDirectory)/"/>
35+
<None Include="../README.md" Pack="true" PackagePath="\" />
36+
<SourceRoot Include="$(MSBuildThisFileDirectory)/" />
3737
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
3838
<PrivateAssets>all</PrivateAssets>
3939
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
4040
</PackageReference>
4141
</ItemGroup>
4242
<ItemGroup>
4343
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
44-
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
45-
<PrivateAssets>all</PrivateAssets>
46-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
47-
</PackageReference>
4844
</ItemGroup>
4945

5046
</Project>

0 commit comments

Comments
 (0)