Skip to content

Commit f0f9aa9

Browse files
committed
refactor: improve argument parsing and enhance test output handling in CoreUtils
1 parent 19af0fd commit f0f9aa9

3 files changed

Lines changed: 58 additions & 42 deletions

File tree

CodeLineCounter.Tests/CoreUtilsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public void ParseArguments_Should_Return_Correct_Values()
1919
{
2020
// Arrange
2121
string[] args = ["-verbose", "-d", "testDirectory", "-output", _testDirectory];
22+
using StringWriter consoleOutput = new();
23+
Console.SetOut(consoleOutput);
2224

2325
// Act
2426
Settings settings = CoreUtils.ParseArguments(args);
@@ -33,6 +35,8 @@ public void ParseArguments_help_Should_Return_Correct_Values()
3335
{
3436
// Arrange
3537
string[] args = ["-help"];
38+
using StringWriter consoleOutput = new();
39+
Console.SetOut(consoleOutput);
3640

3741
// Act
3842
var settings = CoreUtils.ParseArguments(args);
@@ -46,6 +50,8 @@ public void ParseArguments_Should_Return_Default_Values_When_No_Arguments_Passed
4650
{
4751
// Arrange
4852
string[] args = [];
53+
using StringWriter consoleOutput = new();
54+
Console.SetOut(consoleOutput);
4955

5056
// Act
5157
var settings = CoreUtils.ParseArguments(args);
@@ -60,6 +66,8 @@ public void ParseArguments_Should_Ignore_Invalid_Arguments()
6066
{
6167
// Arrange
6268
string[] args = ["-invalid", "-d", "testDirectory", "-f", "json"];
69+
using StringWriter consoleOutput = new();
70+
Console.SetOut(consoleOutput);
6371

6472
// Act
6573
var settings = CoreUtils.ParseArguments(args);
@@ -75,6 +83,8 @@ public void ParseArguments_processes_valid_arguments_with_all_options()
7583
{
7684
// Arrange
7785
string[] args = new[] { "-verbose", "-d", "C:/test", "-format", "JSON", "-help" };
86+
using StringWriter consoleOutput = new();
87+
Console.SetOut(consoleOutput);
7888

7989
// Act
8090
var result = CoreUtils.ParseArguments(args);
@@ -92,6 +102,8 @@ public void ParseArguments_handles_empty_argument_array()
92102
{
93103
// Arrange
94104
string[] emptyArgs = Array.Empty<string>();
105+
using StringWriter consoleOutput = new();
106+
Console.SetOut(consoleOutput);
95107

96108
// Act
97109
var result = CoreUtils.ParseArguments(emptyArgs);

CodeLineCounter/Services/SolutionAnalyzer.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,20 @@ public static void ExportResults(AnalysisResult result, string solutionPath, Cor
6969
{
7070
string baseFileName = Path.GetFileNameWithoutExtension(solutionPath);
7171

72-
// Export des métriques
72+
// Export metrics
7373
string metricsFileName = $"{baseFileName}-CodeMetrics";
7474
string metricsOutputPath = CoreUtils.GetExportFileNameWithExtension(metricsFileName, format, outputPath);
75-
// Export des métriques...
7675

77-
// Export des duplications
76+
// Export duplications
7877
string duplicationsFileName = $"{baseFileName}-CodeDuplication";
7978
string duplicationsOutputPath = CoreUtils.GetExportFileNameWithExtension(duplicationsFileName, format, outputPath);
8079
// Export des duplications...
8180

82-
// Export du graphe de dépendances
81+
// Export dependencies graph
8382
string graphFileName = $"{baseFileName}-Dependencies.dot";
8483
string graphOutputPath = outputPath != null
8584
? Path.Combine(outputPath, graphFileName)
8685
: graphFileName;
87-
// Export du graphe...
8886

8987
try
9088
{

CodeLineCounter/Utils/CoreUtils.cs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,64 +14,70 @@ public static Settings ParseArguments(string[] args)
1414
{
1515
var settings = new Settings();
1616
int argIndex = 0;
17-
17+
1818
while (argIndex < args.Length)
1919
{
2020
switch (args[argIndex])
2121
{
2222
case "-help":
2323
settings.Help = true;
24-
argIndex++;
2524
break;
2625
case "-verbose":
2726
settings.Verbose = true;
28-
argIndex++;
2927
break;
3028
case "-d":
31-
if (argIndex + 1 < args.Length)
32-
{
33-
settings.DirectoryPath = args[argIndex + 1];
34-
argIndex += 2;
35-
}
36-
else
37-
{
38-
argIndex++;
39-
}
29+
HandleDirectory(args, settings, ref argIndex);
4030
break;
4131
case "-format":
42-
if (argIndex + 1 < args.Length)
43-
{
44-
string formatString = args[argIndex + 1];
45-
argIndex += 2;
46-
if (Enum.TryParse<ExportFormat>(formatString, true, out ExportFormat result))
47-
{
48-
settings.Format = result;
49-
}
50-
else
51-
{
52-
Console.WriteLine($"Invalid format: {formatString}. Valid formats are: {string.Join(", ", Enum.GetNames<ExportFormat>())}. Using default format {settings.Format}");
53-
}
54-
}
32+
HandleFormat(args, settings, ref argIndex);
5533
break;
5634
case "-output":
57-
if (argIndex + 1 < args.Length)
58-
{
59-
settings.OutputPath = args[argIndex + 1];
60-
argIndex += 2;
61-
}
62-
else
63-
{
64-
argIndex++;
65-
}
35+
HandleOutput(args, settings, ref argIndex);
6636
break;
6737
default:
68-
argIndex++;
6938
break;
7039
}
40+
argIndex++;
7141
}
7242
return settings;
7343
}
7444

45+
private static void HandleDirectory(string[] args, Settings settings, ref int argIndex)
46+
{
47+
if (argIndex + 1 < args.Length)
48+
{
49+
settings.DirectoryPath = args[argIndex + 1];
50+
argIndex++;
51+
}
52+
53+
}
54+
55+
private static void HandleFormat(string[] args, Settings settings, ref int argIndex)
56+
{
57+
if (argIndex + 1 < args.Length)
58+
{
59+
string formatString = args[argIndex + 1];
60+
argIndex++;
61+
if (Enum.TryParse<ExportFormat>(formatString, true, out ExportFormat result))
62+
{
63+
settings.Format = result;
64+
}
65+
else
66+
{
67+
Console.WriteLine($"Invalid format: {formatString}. Valid formats are: {string.Join(", ", Enum.GetNames<ExportFormat>())}. Using default format {settings.Format}");
68+
}
69+
}
70+
}
71+
72+
private static void HandleOutput(string[] args, Settings settings, ref int argIndex)
73+
{
74+
if (argIndex + 1 < args.Length)
75+
{
76+
settings.OutputPath = args[argIndex + 1];
77+
argIndex++;
78+
}
79+
}
80+
7581
/// <summary>
7682
/// Gets a valid user selection from the available solutions.
7783
/// </summary>
@@ -153,9 +159,9 @@ public static string GetExportFileNameWithExtension(string filePath, CoreUtils.E
153159
}
154160

155161
// If an output directory is specified, combine the path
156-
return outputPath != null
162+
return outputPath != null
157163
? Path.Combine(Path.GetFullPath(outputPath), fileName)
158164
: Path.Combine(Path.GetDirectoryName(filePath) ?? Path.GetFullPath("."), fileName);
159-
}
165+
}
160166
}
161167
}

0 commit comments

Comments
 (0)