Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ag 14044 Testing sonar #4

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
bdf7003
Create sonarcloud.yml workflow
vanguille Dec 17, 2024
7988eae
Update sonarcloud.yml
vanguille Dec 18, 2024
e2d43f5
Update sonarcloud.yml
vanguille Dec 18, 2024
8148a0f
Update sonarcloud.yml
vanguille Dec 18, 2024
8b75281
testing
xusterlg Dec 18, 2024
b25e620
fixed key
xusterlg Dec 18, 2024
6b2c306
added features to scan
xusterlg Dec 18, 2024
6466144
test
xusterlg Dec 18, 2024
1ea80a1
test
xusterlg Dec 18, 2024
53ed24b
test
xusterlg Dec 18, 2024
626d561
test
xusterlg Dec 18, 2024
65e635c
test
xusterlg Dec 18, 2024
a6758cb
test
xusterlg Dec 18, 2024
d3eb216
test
xusterlg Dec 18, 2024
25365a1
test
xusterlg Dec 18, 2024
e6bc8f7
tests and coverage ok without coverage tests
xusterlg Dec 24, 2024
2220c4f
Fixed dictionary tests
xusterlg Dec 26, 2024
b7aab95
test dotnet version
xusterlg Dec 26, 2024
81a9bde
test
xusterlg Dec 26, 2024
b421686
test
xusterlg Dec 26, 2024
0023978
test
xusterlg Dec 26, 2024
dbfb789
test
xusterlg Dec 26, 2024
680bc6e
test
xusterlg Dec 26, 2024
e35d716
test
xusterlg Dec 26, 2024
3e24cb1
test
xusterlg Dec 26, 2024
c3cadcb
test
xusterlg Dec 26, 2024
f8f9afe
test
xusterlg Dec 26, 2024
d3e52ef
test
xusterlg Dec 26, 2024
694c811
test
xusterlg Dec 26, 2024
647a127
test
xusterlg Dec 26, 2024
46ffa73
test
xusterlg Dec 26, 2024
a1681d3
test
xusterlg Dec 26, 2024
358c5ac
test
xusterlg Dec 26, 2024
3d4b943
test
xusterlg Dec 26, 2024
6ebb736
test
xusterlg Dec 31, 2024
adfb722
test
xusterlg Dec 31, 2024
eba5f27
test
xusterlg Dec 31, 2024
0588ce5
test
xusterlg Dec 31, 2024
ba86025
test
xusterlg Dec 31, 2024
e58d416
test
xusterlg Dec 31, 2024
f8578b0
test
xusterlg Dec 31, 2024
24ec7ae
test
xusterlg Dec 31, 2024
ceb5cd2
test
xusterlg Dec 31, 2024
31eec52
test
xusterlg Dec 31, 2024
b7336a3
test
xusterlg Dec 31, 2024
0fe07b9
test
xusterlg Dec 31, 2024
9590ac6
Added test to cover the requisites
xusterlg Jan 5, 2025
b6411ed
test
xusterlg Jan 5, 2025
1a3d1f0
test
xusterlg Jan 5, 2025
5b3ea4d
Fixed feature trigger
xusterlg Jan 5, 2025
bda5c14
.net 8.0
xusterlg Jan 8, 2025
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
67 changes: 67 additions & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: SonarCloud Analysis (C#)

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
sonarcloud:
name: Analyze with SonarCloud
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'

- name: Install SonarScanner for .NET
run: dotnet tool install --global dotnet-sonarscanner

- name: Install Coverlet.Console
run: dotnet tool install --global coverlet.console

- name: Restore dependencies
run: dotnet restore ./AutoGuru.KeyValuePush.sln

- name: Build tests
run: dotnet build ./src/KeyValuePush.Tests/KeyValuePush.Tests.csproj --configuration Debug --framework net8.0

- name: Run tests with coverage
run: |
coverlet ./src/KeyValuePush.Tests/bin/Debug/net8.0/KeyValuePush.Tests.dll \
--target "dotnet" \
--targetargs "test ./src/KeyValuePush.Tests/KeyValuePush.Tests.csproj --no-build --framework net8.0 --logger trx --results-directory ./TestResults/" \
--output ./TestResults/Coverage/ \
--format opencover

- name: Upload test results (optional, for visibility in GitHub Actions)
uses: actions/upload-artifact@v3
with:
name: TestResults
path: ./TestResults/

- name: Run SonarCloud analysis
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
dotnet sonarscanner begin \
/k:"autoguru-au_kv-push" \
/o:"autoguru-au" \
/d:sonar.cs.opencover.reportsPaths="./TestResults/Coverage/coverage.opencover.xml" \
/d:sonar.inclusions="**/*.cs" \
/d:sonar.exclusions="**/bin/**,**/obj/**" \
/d:sonar.scanner.skipJreProvisioning=true \
/d:sonar.verbose=true \
/d:sonar.login="${{ secrets.SONAR_TOKEN }}"
dotnet build ./AutoGuru.KeyValuePush.sln
dotnet sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
11 changes: 11 additions & 0 deletions AutoGuru.KeyValuePush.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".Repo Items", ".Repo Items"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGuru.KeyValuePush.Redis", "src\KeyValuePush.Redis\AutoGuru.KeyValuePush.Redis.csproj", "{379E9996-CE05-44CD-B255-5C88B1CE1BED}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{6598E4B4-D7D3-4633-9E2F-5C470917258D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyValuePush.Tests", "src\KeyValuePush.Tests\KeyValuePush.Tests.csproj", "{14E2DBAE-46BF-4109-8F87-5894B0C7E07E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,11 +33,18 @@ Global
{379E9996-CE05-44CD-B255-5C88B1CE1BED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{379E9996-CE05-44CD-B255-5C88B1CE1BED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{379E9996-CE05-44CD-B255-5C88B1CE1BED}.Release|Any CPU.Build.0 = Release|Any CPU
{14E2DBAE-46BF-4109-8F87-5894B0C7E07E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{14E2DBAE-46BF-4109-8F87-5894B0C7E07E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14E2DBAE-46BF-4109-8F87-5894B0C7E07E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14E2DBAE-46BF-4109-8F87-5894B0C7E07E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EE37C22E-E3CE-4E6B-8004-BB25FE05CB98}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{14E2DBAE-46BF-4109-8F87-5894B0C7E07E} = {6598E4B4-D7D3-4633-9E2F-5C470917258D}
EndGlobalSection
EndGlobal
3 changes: 0 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
Expand All @@ -12,11 +11,9 @@
<PackageProjectUrl>https://github.com/autoguru-au/kv-push</PackageProjectUrl>
<RepositoryUrl>https://github.com/autoguru-au/kv-push</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<IsPackable>false</IsPackable>
<NoPackageAnalysis>true</NoPackageAnalysis>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateFullPaths Condition="'$(TERM_PROGRAM)' == 'vscode'">true</GenerateFullPaths>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
19 changes: 19 additions & 0 deletions src/KeyValuePush.Redis.Tests/KeyValuePush.Redis.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="StackExchange.Redis" Version="2.7.17" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Moq" Version="4.20.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\KeyValuePush.Redis\AutoGuru.KeyValuePush.Redis.csproj" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions src/KeyValuePush.Redis.Tests/RedisPusherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Moq;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace AutoGuru.KeyValuePush.Redis.Tests
{
public class RedisPusherTests
{
private readonly Mock<IDatabase> _mockDatabase;
private readonly RedisPusher _pusher;

public RedisPusherTests()
{
_mockDatabase = new Mock<IDatabase>();
_pusher = new RedisPusher();

// Usamos reflexión para inyectar el mock en el campo privado
var field = typeof(RedisPusher).GetField("_db", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field?.SetValue(_pusher, _mockDatabase.Object);
}


}
}
13 changes: 13 additions & 0 deletions src/KeyValuePush.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Xunit;

namespace KeyValuePush.Tests
{
public class BasicTests
{
[Fact]
public void ExampleTest()
{
Assert.True(1 + 1 == 2);
}
}
}
158 changes: 158 additions & 0 deletions src/KeyValuePush.Tests/DefaultDictionaryBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using AutoGuru.KeyValuePush;
using Xunit;

namespace KeyValuePush.Tests
{
public class DefaultDictionaryBuilderTests
{
private readonly DefaultDictionaryBuilder _builder;

public DefaultDictionaryBuilderTests()
{
_builder = new DefaultDictionaryBuilder();
}

[Fact]
public void TryAdd_ShouldAddKeyValuePair_WhenKeyDoesNotExist()
{
var dict = new Dictionary<string, string>();
var key = "key1";
var value = "value1";

DefaultDictionaryBuilder.TryAdd(dict, key, value);

Assert.Single(dict);
Assert.Equal(value, dict[key]);
}

[Fact]
public void TryAdd_ShouldNotThrow_WhenKeyExistsWithSameValue()
{
var dict = new Dictionary<string, string> { { "key1", "value1" } };
var key = "key1";
var value = "value1";

DefaultDictionaryBuilder.TryAdd(dict, key, value);

Assert.Single(dict);
Assert.Equal(value, dict[key]);
}

[Fact]
public void TryAdd_ShouldThrowException_WhenKeyExistsWithDifferentValue()
{
var dict = new Dictionary<string, string> { { "key1", "value1" } };
var key = "key1";
var value = "differentValue";

var exception = Assert.Throws<Exception>(() => DefaultDictionaryBuilder.TryAdd(dict, key, value));
Assert.Equal("Duplicate key of 'key1' with a different value detected.", exception.Message);
}

[Fact]
public async Task BuildAsync_ShouldThrowException_WhenFileAccessFailsAsync()
{
var path = "RestrictedFiles";
Directory.CreateDirectory(path);
var filePath = Path.Combine(path, "file1.txt");
File.WriteAllText(filePath, "Content1");

// Bloquear el archivo para simular acceso denegado
using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
{
var exception = await Assert.ThrowsAsync<IOException>(() =>
_builder.BuildAsync(path, "*.txt", SearchOption.TopDirectoryOnly, false, CancellationToken.None));

Assert.Contains("being used by another process", exception.Message);
}

File.Delete(filePath);
Directory.Delete(path);
}


[Fact]
public async Task BuildAsync_ShouldRespectCancellationTokenAsync()
{
var path = "TestFiles";
Directory.CreateDirectory(path);
File.WriteAllText(Path.Combine(path, "file1.txt"), "Content1");

using var cts = new CancellationTokenSource();
cts.Cancel();

await Assert.ThrowsAsync<OperationCanceledException>(() =>
_builder.BuildAsync(path, "*.txt", SearchOption.TopDirectoryOnly, false, cts.Token));

Directory.Delete(path, true);
}

[Fact]
public async Task BuildAsync_ShouldHandleDuplicateKeysInJsonFilesAsync()
{
var path = "TestFiles";
Directory.CreateDirectory(path);

// Crear un archivo JSON con claves únicas
var jsonContent = JsonSerializer.Serialize(new Dictionary<string, string>
{
{ "Key1", "Value1" },
{ "Key2", "Value2" }
});
File.WriteAllText(Path.Combine(path, "file1.json"), jsonContent);

var result = await _builder.BuildAsync(path, "*.json", SearchOption.TopDirectoryOnly, true, CancellationToken.None);

Assert.Equal(2, result.Count);
Assert.Equal("Value1", result["Key1"]);
Assert.Equal("Value2", result["Key2"]);

Directory.Delete(path, true);
}


[Fact]
public async Task BuildAsync_ShouldIgnoreFilesWithNonJsonExtensionsAsync()
{
var path = "TestFiles";
Directory.CreateDirectory(path);

// Crear un archivo con extensión no soportada
File.WriteAllText(Path.Combine(path, "file1.unsupported"), "Unsupported Content");

var result = await _builder.BuildAsync(path, "*.json", SearchOption.TopDirectoryOnly, false, CancellationToken.None);

Assert.Empty(result);
Directory.Delete(path, true);
}


[Fact]
public async Task BuildAsync_ShouldCombineJsonAndTextFilesAsync()
{
var path = "TestFiles";
Directory.CreateDirectory(path);
var jsonContent = JsonSerializer.Serialize(new Dictionary<string, string>
{
{ "Key1", "Value1" },
{ "Key2", "Value2" }
});
File.WriteAllText(Path.Combine(path, "file1.json"), jsonContent);
File.WriteAllText(Path.Combine(path, "file2.txt"), "TextContent");

var result = await _builder.BuildAsync(path, "*.*", SearchOption.TopDirectoryOnly, true, CancellationToken.None);

Assert.Equal(3, result.Count);
Assert.Equal("Value1", result["Key1"]);
Assert.Equal("Value2", result["Key2"]);
Assert.Equal("TextContent", result["file2"]);
Directory.Delete(path, true);
}
}
}
Loading
Loading