Skip to content

Commit 300ed6b

Browse files
committed
refactor: update test project dependencies and improve error handling in SolutionAnalyzer
1 parent 0cc20b1 commit 300ed6b

3 files changed

Lines changed: 36 additions & 48 deletions

File tree

CodeLineCounter.Tests/CodeLineCounter.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
<PrivateAssets>all</PrivateAssets>
1616
</PackageReference>
17-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
1818
<PackageReference Include="Moq" Version="4.20.72" />
1919
<PackageReference Include="xunit" Version="2.9.3" />
20-
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
20+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
2121
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2222
<PrivateAssets>all</PrivateAssets>
2323
</PackageReference>

CodeLineCounter.Tests/SolutionAnalyzerTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,21 @@ public void export_results_with_valid_input_exports_all_files()
232232
var solutionPath = Path.Combine(baseSolutionPath, "CodelineCounter.sln");
233233
var format = CoreUtils.ExportFormat.JSON;
234234
Console.SetOut(sw);
235-
SolutionAnalyzer.ExportResults(result, solutionPath, format, baseSolutionPath);
236-
// Assert
237-
Assert.True(File.Exists(Path.Combine(baseSolutionPath, "CodelineCounter.CodeMetrics.json")));
238-
Assert.True(File.Exists(Path.Combine(baseSolutionPath, "CodelineCounter.CodeDuplications.json")));
239-
Assert.True(File.Exists(Path.Combine(baseSolutionPath, "CodelineCounter.Dependencies.dot")));
235+
236+
try
237+
{
238+
SolutionAnalyzer.ExportResults(result, solutionPath, format, baseSolutionPath);
239+
// Assert
240+
Assert.True(File.Exists(Path.Combine(baseSolutionPath, "CodelineCounter.CodeMetrics.json")));
241+
Assert.True(File.Exists(Path.Combine(baseSolutionPath, "CodelineCounter.CodeDuplications.json")));
242+
Assert.True(File.Exists(Path.Combine(baseSolutionPath, "CodelineCounter.Dependencies.dot")));
243+
}
244+
finally
245+
{
246+
File.Delete(Path.Combine(baseSolutionPath, "CodelineCounter.CodeMetrics.json"));
247+
File.Delete(Path.Combine(baseSolutionPath, "CodelineCounter.CodeDuplications.json"));
248+
File.Delete(Path.Combine(baseSolutionPath, "CodelineCounter.Dependencies.dot"));
249+
}
240250
}
241251

242252

CodeLineCounter/Services/SolutionAnalyzer.cs

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ namespace CodeLineCounter.Services
77
{
88
public static partial class SolutionAnalyzer
99
{
10-
1110
public static void AnalyzeAndExportSolution(string solutionPath, bool verbose, CoreUtils.ExportFormat format, string? outputPath = null)
1211
{
1312
try
1413
{
1514
var analysisResult = PerformAnalysis(solutionPath);
1615
OutputAnalysisResults(analysisResult, verbose);
1716
ExportResults(analysisResult, solutionPath, format, outputPath);
18-
17+
}
18+
catch (UnauthorizedAccessException ex)
19+
{
20+
Console.Error.WriteLine($"Access denied: {ex.Message}");
21+
throw;
22+
}
23+
catch (FileNotFoundException ex)
24+
{
25+
Console.Error.WriteLine($"File not found: {ex.Message}");
26+
throw;
1927
}
2028
catch (Exception ex)
2129
{
@@ -56,7 +64,7 @@ public static void OutputAnalysisResults(AnalysisResult result, bool verbose)
5664
}
5765

5866
var percentageDuplication = (result.DuplicatedLines / (double)result.TotalLines) * 100;
59-
NumberFormatInfo nfi = new System.Globalization.CultureInfo( "en-US", false ).NumberFormat;
67+
NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
6068

6169
Console.WriteLine($"Processing completed, number of source files processed: {result.TotalFiles}");
6270
Console.WriteLine($"Total lines of code: {result.TotalLines}");
@@ -68,52 +76,23 @@ public static void OutputAnalysisResults(AnalysisResult result, bool verbose)
6876
public static void ExportResults(AnalysisResult result, string solutionPath, CoreUtils.ExportFormat format, string? outputPath = null)
6977
{
7078
string baseFileName = Path.GetFileNameWithoutExtension(solutionPath);
71-
72-
// Export metrics
7379
string metricsFileName = $"{baseFileName}.CodeMetrics.json";
7480
metricsFileName = CoreUtils.GetExportFileNameWithExtension(metricsFileName, format);
75-
if (string.IsNullOrEmpty(outputPath))
76-
{
77-
outputPath = ".";
78-
}
81+
outputPath ??= ".";
7982

80-
string metricsOutputPath = outputPath != null
81-
? Path.Combine(outputPath, metricsFileName)
82-
: metricsFileName;
83-
84-
// Export duplications
83+
string metricsOutputPath = Path.Combine(outputPath, metricsFileName);
8584
string duplicationsFileName = $"{baseFileName}.CodeDuplications.json";
8685
duplicationsFileName = CoreUtils.GetExportFileNameWithExtension(duplicationsFileName, format);
87-
string duplicationsOutputPath = outputPath != null
88-
? Path.Combine(outputPath, duplicationsFileName)
89-
: duplicationsFileName;
90-
// Export des duplications...
91-
92-
// Export dependencies graph
86+
string duplicationsOutputPath = Path.Combine(outputPath, duplicationsFileName);
9387
string graphFileName = $"{baseFileName}.Dependencies.dot";
94-
string graphOutputPath = outputPath != null
95-
? Path.Combine(outputPath, graphFileName)
96-
: graphFileName;
88+
string graphOutputPath = Path.Combine(outputPath, graphFileName);
9789

9890
try
9991
{
10092
Parallel.Invoke(
101-
() => DataExporter.ExportMetrics(
102-
metricsFileName,
103-
outputPath ??".",
104-
result,
105-
solutionPath,
106-
format),
107-
() => DataExporter.ExportDuplications(
108-
duplicationsFileName,
109-
outputPath ?? ".",
110-
result.DuplicationMap,
111-
format),
112-
async () => await DataExporter.ExportDependencies(
113-
graphFileName,
114-
outputPath ?? ".",
115-
result.DependencyList,
116-
format)
93+
() => DataExporter.ExportMetrics(metricsFileName, outputPath, result, solutionPath, format),
94+
() => DataExporter.ExportDuplications(duplicationsFileName, outputPath, result.DuplicationMap, format),
95+
async () => await DataExporter.ExportDependencies(graphFileName, outputPath, result.DependencyList, format)
11796
);
11897

11998
Console.WriteLine($"The data has been exported to {metricsOutputPath}");
@@ -144,6 +123,5 @@ public static void OutputDetailedMetrics(List<NamespaceMetrics> metrics, Diction
144123
Console.WriteLine($"Project {projectTotal.Key} has {projectTotal.Value} total lines of code.");
145124
}
146125
}
147-
148126
}
149-
}
127+
}

0 commit comments

Comments
 (0)