Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/publish
/*.zip
/*.csv
/*.json
/*.xlsx
/output.txt
/test.cmd
Expand Down
31 changes: 23 additions & 8 deletions CodeLineCounter.Tests/CoreUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void ParseArguments_Should_Return_Correct_Values()
string[] args = ["-verbose", "-d", "testDirectory"];

// Act
var (Verbose, DirectoryPath, _) = CoreUtils.ParseArguments(args);
var (Verbose, DirectoryPath, _, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.True(Verbose);
Expand All @@ -25,7 +25,7 @@ public void ParseArguments_help_Should_Return_Correct_Values()
string[] args = ["-help"];

// Act
var (_, _, Help) = CoreUtils.ParseArguments(args);
var (_, _, Help, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.True(Help);
Expand All @@ -38,7 +38,7 @@ public void ParseArguments_Should_Return_Default_Values_When_No_Arguments_Passed
string[] args = [];

// Act
var (Verbose, DirectoryPath, _) = CoreUtils.ParseArguments(args);
var (Verbose, DirectoryPath, _, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.False(Verbose);
Expand All @@ -49,10 +49,10 @@ public void ParseArguments_Should_Return_Default_Values_When_No_Arguments_Passed
public void ParseArguments_Should_Ignore_Invalid_Arguments()
{
// Arrange
string[] args = ["-invalid", "-d", "testDirectory"];
string[] args = ["-invalid", "-d", "testDirectory", "-f", "json"];

// Act
var (Verbose, DirectoryPath, _) = CoreUtils.ParseArguments(args);
var (Verbose, DirectoryPath, _, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.False(Verbose);
Expand Down Expand Up @@ -149,7 +149,7 @@ public void GetFilenamesList_Should_Return_List_Of_Filenames()
public void CheckSettings_WhenHelpIsTrue_ReturnsFalse()
{
// Arrange
(bool Verbose, string? DirectoryPath, bool Help) settings = (true, null, true);
(bool Verbose, string? DirectoryPath, bool Help, CoreUtils.ExportFormat format) settings = (true, null, true, CoreUtils.ExportFormat.JSON);
using var sw = new StringWriter();
Console.SetOut(sw);

Expand All @@ -165,7 +165,7 @@ public void CheckSettings_WhenHelpIsTrue_ReturnsFalse()
public void CheckSettings_WhenDirectoryPathIsNull_ReturnsFalse()
{
// Arrange
(bool Verbose, string? DirectoryPath, bool Help) settings = (Verbose: false, DirectoryPath: null, Help: false);
(bool Verbose, string? DirectoryPath, bool Help, CoreUtils.ExportFormat format) settings = (Verbose: false, DirectoryPath: null, Help: false, format: CoreUtils.ExportFormat.CSV);
using var sw = new StringWriter();
Console.SetOut(sw);

Expand All @@ -181,13 +181,28 @@ public void CheckSettings_WhenDirectoryPathIsNull_ReturnsFalse()
public void CheckSettings_WhenSettingsAreValid_ReturnsTrue()
{
// Arrange
(bool Verbose, string DirectoryPath, bool Help) settings = (false, "some_directory", false);
(bool Verbose, string DirectoryPath, bool Help, CoreUtils.ExportFormat format) settings = (false, "some_directory", false, CoreUtils.ExportFormat.CSV);

// Act
var result = CoreUtils.CheckSettings(settings);

// Assert
Assert.True(result);
}

[Fact]
public void CheckSettings_WhenSettingsAreInvalid_ReturnsFalse()
{
// Arrange
(bool Verbose, string? DirectoryPath, bool Help, CoreUtils.ExportFormat format) settings = (false, null, false, CoreUtils.ExportFormat.CSV);
using var sw = new StringWriter();
Console.SetOut(sw);

// Act
var result = CoreUtils.CheckSettings(settings);

// Assert
Assert.False(result);
}
}
}
121 changes: 0 additions & 121 deletions CodeLineCounter.Tests/CsvExporterTests.cs

This file was deleted.

135 changes: 135 additions & 0 deletions CodeLineCounter.Tests/DataExporterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using Xunit;
using System;
using System.IO;
using System.Collections.Generic;
using CodeLineCounter.Models;
using CodeLineCounter.Utils;
using System.Linq;

namespace CodeLineCounter.Tests
{
public class DataExporterTests : IDisposable
{
private readonly string _testDirectory;
private bool _disposed;

public DataExporterTests()
{
_testDirectory = Path.Combine(Path.GetTempPath(), "DataExporterTests");
Directory.CreateDirectory(_testDirectory);
}

[Theory]
[InlineData(CoreUtils.ExportFormat.CSV, ".csv")]
[InlineData(CoreUtils.ExportFormat.JSON, ".json")]
public void Export_SingleItem_CreatesFileWithCorrectExtension(CoreUtils.ExportFormat format, string expectedExtension)
{
// Arrange
var testItem = new TestClass { Id = 1, Name = "Test" };
var filePath = Path.Combine(_testDirectory, "test");

// Act
DataExporter.Export(filePath, testItem, format);

// Assert
Assert.True(File.Exists(filePath + expectedExtension));
}

[Theory]
[InlineData(CoreUtils.ExportFormat.CSV, ".csv")]
[InlineData(CoreUtils.ExportFormat.JSON, ".json")]
public void ExportCollection_WithMultipleItems_CreatesFile(CoreUtils.ExportFormat format, string expectedExtension)
{
// Arrange
var items = new List<TestClass>
{
new() { Id = 1, Name = "Test1" },
new() { Id = 2, Name = "Test2" }
};
var filePath = Path.Combine(_testDirectory, "collection");

// Act
DataExporter.ExportCollection(filePath, items, format);

// Assert
Assert.True(File.Exists(filePath + expectedExtension));
}

[Fact]
public void ExportMetrics_CreatesFileWithCorrectData()
{
// Arrange
var metrics = new List<NamespaceMetrics>
{
new() { ProjectName = "Project1", LineCount = 100 },
new() { ProjectName = "Project2", LineCount = 200 }
};
var projectTotals = new Dictionary<string, int>
{
{ "Project1", 100 },
{ "Project2", 200 }
};
var duplications = new List<DuplicationCode>();
var filePath = Path.Combine(_testDirectory, "metrics");

// Act
DataExporter.ExportMetrics(filePath, metrics, projectTotals, 300, duplications, null, CoreUtils.ExportFormat.CSV);

// Assert
Assert.True(File.Exists(filePath + ".csv"));
}

[Fact]
public void ExportDuplications_CreatesFileWithCorrectData()
{
// Arrange
var duplications = new List<DuplicationCode>
{
new() { CodeHash = "hash1", FilePath = "file1.cs", MethodName = "method1", StartLine = 10, NbLines = 20 },
new() { CodeHash = "hash2", FilePath = "file2.cs", MethodName = "method2", StartLine = 8, NbLines = 10 }
};
var filePath = Path.Combine(_testDirectory, "duplications");

// Act
DataExporter.ExportDuplications(filePath, duplications, CoreUtils.ExportFormat.CSV);

// Assert
Assert.True(File.Exists(filePath + ".csv"));
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing && Directory.Exists(_testDirectory))
{
// Dispose managed resources
Directory.Delete(_testDirectory, true);
}

// Dispose unmanaged resources (if any)

_disposed = true;
}
}

private class TestClass
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~DataExporterTests()
{
Dispose(false);
}


}
}
13 changes: 9 additions & 4 deletions CodeLineCounter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace CodeLineCounter
{
Expand All @@ -30,17 +31,19 @@ static void Main(string[] args)
if (choice == -1) return;

var solutionPath = Path.GetFullPath(solutionFiles[choice - 1]);
AnalyzeAndExportSolution(solutionPath, settings.Verbose);
AnalyzeAndExportSolution(solutionPath, settings.Verbose, settings.format);
}
}

private static void AnalyzeAndExportSolution(string solutionPath, bool verbose)
private static void AnalyzeAndExportSolution(string solutionPath, bool verbose, CoreUtils.ExportFormat format )
{
var timer = new Stopwatch();
timer.Start();
string solutionFilename = Path.GetFileName(solutionPath);
string csvFilePath = $"{solutionFilename}-CodeMetrics.csv";
csvFilePath = CoreUtils.GetExportFileNameWithExtension(csvFilePath, format);
string duplicationCsvFilePath = $"{solutionFilename}-CodeDuplications.csv";
duplicationCsvFilePath = CoreUtils.GetExportFileNameWithExtension(duplicationCsvFilePath, format);

var (metrics, projectTotals, totalLines, totalFiles, duplicationMap) = CodeAnalyzer.AnalyzeSolution(solutionPath);
timer.Stop();
Expand All @@ -67,8 +70,10 @@ private static void AnalyzeAndExportSolution(string solutionPath, bool verbose)
Console.WriteLine($"Percentage of duplicated code: {percentageDuplication:F2} %");

Parallel.Invoke(
() => CsvExporter.ExportToCsv(csvFilePath, metrics, projectTotals, totalLines, duplicationMap, solutionPath),
() => CsvExporter.ExportCodeDuplicationsToCsv(duplicationCsvFilePath, duplicationMap)
//() => CsvExporter.ExportToCsv(csvFilePath, metrics, projectTotals, totalLines, duplicationMap, solutionPath),
//() => CsvExporter.ExportCodeDuplicationsToCsv(duplicationCsvFilePath, duplicationMap),
() => DataExporter.ExportMetrics(csvFilePath, metrics, projectTotals, totalLines, duplicationMap, solutionPath, format),
() => DataExporter.ExportDuplications(duplicationCsvFilePath, duplicationMap, format)
);

Console.WriteLine($"The data has been exported to {csvFilePath}");
Expand Down
Loading