Skip to content

Develop #74

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

Merged
merged 8 commits into from
Jun 20, 2024
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
5 changes: 4 additions & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ jobs:
- name: Lint (codestyle)
run: dotnet format --verify-no-changes --verbosity normal
- name: Test
run: dotnet test --no-build --verbosity normal
run: >
dotnet test --no-build
--verbosity normal
--logger "console;verbosity=detailed"
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ COPY ./CODE_OF_CONDUCT.md ${WORKDIR}/

# Code source
COPY ./algorithm-exercises-csharp ${WORKDIR}/algorithm-exercises-csharp
COPY ./algorithm-exercises-csharp-base ${WORKDIR}/algorithm-exercises-csharp-base
COPY ./algorithm-exercises-csharp-test ${WORKDIR}/algorithm-exercises-csharp-test
COPY ./algorithm-exercises-csharp.sln ${WORKDIR}/algorithm-exercises-csharp.sln
COPY ./Makefile ${WORKDIR}/
Expand All @@ -52,6 +53,7 @@ CMD ["make", "lint"]
FROM base AS development

COPY ./algorithm-exercises-csharp ${WORKDIR}/algorithm-exercises-csharp
COPY ./algorithm-exercises-csharp-base ${WORKDIR}/algorithm-exercises-csharp-base
COPY ./algorithm-exercises-csharp-test ${WORKDIR}/algorithm-exercises-csharp-test
COPY ./algorithm-exercises-csharp.sln ${WORKDIR}/algorithm-exercises-csharp.sln
COPY ./Makefile ${WORKDIR}/
Expand Down Expand Up @@ -111,4 +113,4 @@ RUN ls -alh
USER worker
CMD ["make", "run"]

# checkov:skip= CKV_DOCKER_2: production image isn't a service process (yet)
# checkov:skip= CKV_DOCKER_2: production image isn't a service process (yet)
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ release: dependencies
${PACKAGE_TOOL} publish --verbosity ${VERBOSITY_LEVEL}

test: build
${PACKAGE_TOOL} test --verbosity ${VERBOSITY_LEVEL} --collect:"Code Coverage"
${PACKAGE_TOOL} test --verbosity ${VERBOSITY_LEVEL} \
--collect:"Code Coverage" \
--logger "console;verbosity=detailed"

coverage: dependencies test
cat coverage-report/Summary.txt
Expand Down
98 changes: 83 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ require docker-compose and make installed.
Using a dotnet 8.0 stack in your SO. You must install dependencies:

```bash
dotnet restore --verbosity=normal
dotnet restore --verbosity normal
```

Every problem is a function with unit test.
Expand All @@ -91,34 +91,46 @@ Unit test has test cases and input data to solve the problem.

Run all tests (skips static analysis, and "clean" test cache before running):

By default, there are no log outputs from the application or tests to the console.
"--verbosity \<LEVEL\>" only control verbosity of dotnet proccess
(building, test running, ...)

```bash
dotnet test --verbosity normal
dotnet clean && dotnet test --verbosity normal
```

#### Test run with alternative behaviors
To enable console detailed output use:

> [!IMPORTANT]
> BRUTEFORCE and LOG_LEVEL environment variables not yet implemented.
>
> Currently tests only have one behavior.
```bash
dotnet test --logger "console;verbosity=detailed"
```

~~You can change test running behaviour using some environment variables as follows:~~
#### Test run with alternative behaviors

You can change test running behaviour using some environment variables as follows:

| Variable | Values | Default |
| ------ | ------ | ------ |
| LOG_LEVEL | `debug`, `warning`, `error`, `info` | `info` |
| BRUTEFORCE | `true`, `false`| `false` |

- ~~`LOG_LEVEL`: change verbosity level in outputs.~~
- ~~`BRUTEFORCE`: enable or disable running large tests.
(long time, large amount of data, high memory consumition).~~
- `LOG_LEVEL`: change verbosity level in outputs.
- `BRUTEFORCE`: enable or disable running large tests.
(long time, large amount of data, high memory consumition).

#### Examples running tests with alternative behaviors

> [!IMPORTANT]
> BRUTEFORCE and LOG_LEVEL environment variables not yet implemented.
>
> Currently tests only have one behavior.
Run tests with debug outputs:

```bash
LOG_LEVEL=debug dotnet test --logger "console;verbosity=detailed"
```

Run brute-force tests with debug outputs:

```bash
BRUTEFORCE=true LOG_LEVEL=debug dotnet test --logger "console;verbosity=detailed"
```

### Install and Run using make

Expand All @@ -131,6 +143,24 @@ Run tests (libraries are installed as dependency task in make):
make test
```

Run tests with debug outputs:

```bash
make test -e LOG_LEVEL=debug
```

Run brute-force tests with debug outputs:

```bash
make test -e BRUTEFORCE=true -e LOG_LEVEL=debug
```

Alternative way, use environment variables as prefix:

```bash
BRUTEFORCE=true LOG_LEVEL=debug make test
```

### Install and Running with Docker 🐳

Build an image of the test stage.
Expand All @@ -143,13 +173,41 @@ environment using docker-compose.
docker-compose --profile testing run --rm algorithm-exercises-csharp-test
```

To change behavior using environment variables, you can pass to containers
in the following ways:

From host using docker-compose (compose.yaml) mechanism:

```bash
BRUTEFORCE=true docker-compose --profile testing run --rm algorithm-exercises-csharp-test
```

Overriding docker CMD, as parameter of make "-e":

```bash
docker-compose --profile testing run --rm algorithm-exercises-csharp-test make test -e BRUTEFORCE=true
```

```bash
docker-compose --profile testing run --rm algorithm-exercises-csharp-test make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
```

### Install and Running with Docker 🐳 using make

```bash
make compose/build
make compose/test
```

To pass environment variables you can use docker-compose
or overriding CMD and passing to make as "-e" argument.

Passing environment variables using docker-compose (compose.yaml mechanism):

```bash
BRUTEFORCE=true LOG_LEVEL=debug make compose/test
```

## Development workflow using Docker / docker-compose

Running container with development target.
Expand All @@ -166,6 +224,16 @@ docker-compose build --compress algorithm-exercises-csharp-dev
docker-compose run --rm algorithm-exercises-csharp-dev dotnet test --verbosity normal
```

Or with detailed output to terminal:

```bash
# Build development target image
docker-compose build --compress algorithm-exercises-csharp-dev

# Run ephemeral container and override command to run test
docker-compose run --rm algorithm-exercises-csharp-dev dotnet test --logger "console;verbosity=detailed"
```

## Run complete workflow (Docker + make)

Following command simulates a standarized pipeline across environments,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>algorithm_exercises_csharp</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<SonarQubeExclude>true</SonarQubeExclude>

<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>

<!-- Static Analysis -->
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>

</PropertyGroup>

<!-- Exclude test project from coverage -->
<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>


</Project>
69 changes: 69 additions & 0 deletions algorithm-exercises-csharp-base/src/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace algorithm_exercises_csharp;

using Microsoft.Extensions.Logging;
using System;

public sealed class LoggerSingleton
{
private static readonly Lazy<LoggerSingleton> _instance = new Lazy<LoggerSingleton>(() => new LoggerSingleton());

public static LoggerSingleton Instance => _instance.Value;

public ILogger Logger { get; }

private LoggerSingleton()
{
// Read the LOG_LEVEL environment variable
var logLevelEnv = Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "Information";

// Convert the environment variable value to LogLevel
if (!Enum.TryParse<LogLevel>(logLevelEnv, ignoreCase: true, out var logLevel))
{
logLevel = LogLevel.Information; // Set the minimum logging level
}

var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddConsole()
.SetMinimumLevel(logLevel); // Configura el nivel mínimo de logging
});

Logger = loggerFactory.CreateLogger("GlobalLogger");

Logger.LogInformation("Initializing");

Logger.LogInformation("Info level enabled");
Logger.LogWarning("Warning level enabled");
Logger.LogError("Error level enabled");
Logger.LogDebug("Debug level enabled");
}
}

public static class Log
{
public static ILogger getLogger()
{
return LoggerSingleton.Instance.Logger;
}

public static void info(string message)
{
LoggerSingleton.Instance.Logger.LogInformation(message);
}

public static void warning(string message)
{
LoggerSingleton.Instance.Logger.LogWarning(message);
}

public static void error(string message)
{
LoggerSingleton.Instance.Logger.LogError(message);
}

public static void debug(string message)
{
LoggerSingleton.Instance.Logger.LogDebug(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<PackageReference Include="ReportGenerator" Version="5.3.6" />
</ItemGroup>

<!-- Exclude test project from coverage -->
<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions algorithm-exercises-csharp-test/src/Hello.BruteForce.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace algorithm_exercises_csharp;

[TestClass]
public class HelloWorldBruteForceTest
{
private static readonly string[] allowedValues = ["TRUE", "YES", "1"];

[TestInitialize]
public void testInitialize()
{
var envValue = Environment.GetEnvironmentVariable("BRUTEFORCE");
envValue = envValue?.ToUpper();
if (!allowedValues.Contains(envValue, StringComparer.OrdinalIgnoreCase))
{
Assert.Inconclusive($"Skipping BRUTEFORCE test because environment variable 'BRUTEFORCE' is not one of the expected values.");
}
}

[TestMethod]
public void testHelloBruteForce()
{
string expected = "Hello World!";
string result = HelloWorld.hello();

Assert.AreEqual(expected, result);
}
}
7 changes: 6 additions & 1 deletion algorithm-exercises-csharp-test/src/Hello.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ namespace algorithm_exercises_csharp;
[TestClass]
public class HelloWorldTest
{
[TestInitialize]
public void testInitialize()
{
Log.info("Hello World");
}

[TestMethod]
public void testInstance()
{
Expand Down Expand Up @@ -34,7 +40,6 @@ public void testHello()
string result = HelloWorld.hello();

Assert.AreEqual(expected, result);

}
}

6 changes: 6 additions & 0 deletions algorithm-exercises-csharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp-
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp", "algorithm-exercises-csharp\algorithm-exercises-csharp.csproj", "{B162EE62-90C6-4871-B278-390804615987}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp-base", "algorithm-exercises-csharp-base\algorithm-exercises-csharp-base.csproj", "{1BC65C42-83A6-486D-84DB-0DC63002FA24}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{B162EE62-90C6-4871-B278-390804615987}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B162EE62-90C6-4871-B278-390804615987}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B162EE62-90C6-4871-B278-390804615987}.Release|Any CPU.Build.0 = Release|Any CPU
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions algorithm-exercises-csharp/algorithm-exercises-csharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@

</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj" />
</ItemGroup>
</Project>