From 9a8eed801d019878ac416f9c46e659050a35ca3d Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 13:01:58 -0400 Subject: [PATCH 1/8] [CONFIG] [.dotnet] test project excluded from collecting coverage. --- .../algorithm-exercises-csharp-test.csproj | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/algorithm-exercises-csharp-test/algorithm-exercises-csharp-test.csproj b/algorithm-exercises-csharp-test/algorithm-exercises-csharp-test.csproj index 3476c5d..3c80cb2 100644 --- a/algorithm-exercises-csharp-test/algorithm-exercises-csharp-test.csproj +++ b/algorithm-exercises-csharp-test/algorithm-exercises-csharp-test.csproj @@ -35,6 +35,11 @@ + + + + + From cdca7985c5ff580f03a9222a10f280230482833d Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 15:12:02 -0400 Subject: [PATCH 2/8] An example class to test conditionally run of test methods using BRUTEFORCE environment variable to control behavior. --- .../src/Hello.BruteForce.Test.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 algorithm-exercises-csharp-test/src/Hello.BruteForce.Test.cs diff --git a/algorithm-exercises-csharp-test/src/Hello.BruteForce.Test.cs b/algorithm-exercises-csharp-test/src/Hello.BruteForce.Test.cs new file mode 100644 index 0000000..258f376 --- /dev/null +++ b/algorithm-exercises-csharp-test/src/Hello.BruteForce.Test.cs @@ -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); + } +} From 00986cc38aedfd354f80785f3f5649554d0a77d7 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 16:26:56 -0400 Subject: [PATCH 3/8] [CONFIG] new BRUTEFORCE behavior added to README.md --- README.md | 71 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7a0bb53..ff03d00 100644 --- a/README.md +++ b/README.md @@ -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. @@ -95,14 +95,20 @@ Run all tests (skips static analysis, and "clean" test cache before running): dotnet test --verbosity normal ``` +Another way to increase verbosity level in console output: + +```bash +dotnet test --logger "console;verbosity=detailed" +``` + #### Test run with alternative behaviors > [!IMPORTANT] -> BRUTEFORCE and LOG_LEVEL environment variables not yet implemented. +> LOG_LEVEL environment variable not yet implemented. > > Currently tests only have one behavior. -~~You can change test running behaviour using some environment variables as follows:~~ +You can change test running behaviour using some environment variables as follows: | Variable | Values | Default | | ------ | ------ | ------ | @@ -110,15 +116,16 @@ dotnet test --verbosity normal | 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).~~ +- `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 brute-force tests with debug outputs: + +```bash +BRUTEFORCE=true dotnet test --logger "console;verbosity=detailed" +``` ### Install and Run using make @@ -131,6 +138,18 @@ Run tests (libraries are installed as dependency task in make): make test ``` +Run brute-force tests: + +```bash +make test -e BRUTEFORCE=true +``` + +Alternative way, use environment variables as prefix: + +```bash +BRUTEFORCE=true make test +``` + ### Install and Running with Docker 🐳 Build an image of the test stage. @@ -143,6 +162,21 @@ 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-java-test make test -e BRUTEFORCE=true +``` + ### Install and Running with Docker 🐳 using make ```bash @@ -150,6 +184,15 @@ 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 make compose/test +``` + ## Development workflow using Docker / docker-compose Running container with development target. @@ -166,6 +209,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, From 794b0d6c13d7c1cc5f8efa9965516a83dbf86945 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 17:39:14 -0400 Subject: [PATCH 4/8] New Logging system added with LOG_LEVEL environment variable to control behavior. --- Dockerfile | 4 +- .../algorithm-exercises-csharp-base.csproj | 29 ++++++++ algorithm-exercises-csharp-base/src/Logger.cs | 69 +++++++++++++++++++ .../src/Hello.Test.cs | 7 +- algorithm-exercises-csharp.sln | 6 ++ .../algorithm-exercises-csharp.csproj | 3 + 6 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj create mode 100644 algorithm-exercises-csharp-base/src/Logger.cs diff --git a/Dockerfile b/Dockerfile index 8e7b644..61a2fe7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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}/ @@ -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}/ @@ -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) \ No newline at end of file +# checkov:skip= CKV_DOCKER_2: production image isn't a service process (yet) diff --git a/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj b/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj new file mode 100644 index 0000000..ab5d5c2 --- /dev/null +++ b/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + algorithm_exercises_csharp + enable + enable + + true + + + true + true + false + + + + + + + + + + + + + + + diff --git a/algorithm-exercises-csharp-base/src/Logger.cs b/algorithm-exercises-csharp-base/src/Logger.cs new file mode 100644 index 0000000..7350307 --- /dev/null +++ b/algorithm-exercises-csharp-base/src/Logger.cs @@ -0,0 +1,69 @@ +namespace algorithm_exercises_csharp; + +using Microsoft.Extensions.Logging; +using System; + +public sealed class LoggerSingleton +{ + private static readonly Lazy _instance = new Lazy(() => 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(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); + } +} diff --git a/algorithm-exercises-csharp-test/src/Hello.Test.cs b/algorithm-exercises-csharp-test/src/Hello.Test.cs index 32b10ea..66b3ea8 100644 --- a/algorithm-exercises-csharp-test/src/Hello.Test.cs +++ b/algorithm-exercises-csharp-test/src/Hello.Test.cs @@ -3,6 +3,12 @@ namespace algorithm_exercises_csharp; [TestClass] public class HelloWorldTest { + [TestInitialize] + public void testInitialize() + { + Log.info("Hello World"); + } + [TestMethod] public void testInstance() { @@ -34,7 +40,6 @@ public void testHello() string result = HelloWorld.hello(); Assert.AreEqual(expected, result); - } } diff --git a/algorithm-exercises-csharp.sln b/algorithm-exercises-csharp.sln index fb8179a..93ef824 100644 --- a/algorithm-exercises-csharp.sln +++ b/algorithm-exercises-csharp.sln @@ -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 @@ -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 diff --git a/algorithm-exercises-csharp/algorithm-exercises-csharp.csproj b/algorithm-exercises-csharp/algorithm-exercises-csharp.csproj index fe86a87..b18b86c 100644 --- a/algorithm-exercises-csharp/algorithm-exercises-csharp.csproj +++ b/algorithm-exercises-csharp/algorithm-exercises-csharp.csproj @@ -15,4 +15,7 @@ + + + From 93cb224231f2ed14836a5be1193d2c9e0a867130 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 19:50:40 -0400 Subject: [PATCH 5/8] [CONFIG] new LOGL_LEVEL behavior added to README.md --- README.md | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ff03d00..e7f85e5 100644 --- a/README.md +++ b/README.md @@ -91,11 +91,15 @@ 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 \" only control verbosity of dotnet proccess +(building, test running, ...) + ```bash -dotnet test --verbosity normal +dotnet clean && dotnet test --verbosity normal ``` -Another way to increase verbosity level in console output: +To enable console detailed output use: ```bash dotnet test --logger "console;verbosity=detailed" @@ -103,11 +107,6 @@ dotnet test --logger "console;verbosity=detailed" #### Test run with alternative behaviors -> [!IMPORTANT] -> LOG_LEVEL environment variable not yet implemented. -> -> Currently tests only have one behavior. - You can change test running behaviour using some environment variables as follows: | Variable | Values | Default | @@ -115,16 +114,22 @@ You can change test running behaviour using some environment variables as follow | LOG_LEVEL | `debug`, `warning`, `error`, `info` | `info` | | BRUTEFORCE | `true`, `false`| `false` | -- ~~`LOG_LEVEL`: change verbosity level in outputs.~~ +- `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 +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 dotnet test --logger "console;verbosity=detailed" +BRUTEFORCE=true LOG_LEVEL=debug dotnet test --logger "console;verbosity=detailed" ``` ### Install and Run using make @@ -138,16 +143,22 @@ Run tests (libraries are installed as dependency task in make): make test ``` -Run brute-force tests: +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 +make test -e BRUTEFORCE=true -e LOG_LEVEL=debug ``` Alternative way, use environment variables as prefix: ```bash -BRUTEFORCE=true make test +BRUTEFORCE=true LOG_LEVEL=debug make test ``` ### Install and Running with Docker 🐳 @@ -174,7 +185,11 @@ BRUTEFORCE=true docker-compose --profile testing run --rm algorithm-exercises-cs Overriding docker CMD, as parameter of make "-e": ```bash -docker-compose --profile testing run --rm algorithm-exercises-java-test make test -e BRUTEFORCE=true +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 @@ -190,7 +205,7 @@ or overriding CMD and passing to make as "-e" argument. Passing environment variables using docker-compose (compose.yaml mechanism): ```bash -BRUTEFORCE=true make compose/test +BRUTEFORCE=true LOG_LEVEL=debug make compose/test ``` ## Development workflow using Docker / docker-compose From 54c9d9403f6a83cb74e60cb83a6d00ff17a141cf Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 19:52:34 -0400 Subject: [PATCH 6/8] [CONFIG] [make] logger enabled in test run as default. --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5f704fa..af78985 100644 --- a/Makefile +++ b/Makefile @@ -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 From b538c59615c77013a6aff8c105a933b42693b164 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 19:53:51 -0400 Subject: [PATCH 7/8] [CONFIG] [Github Actions] logger enabled in test run as default. --- .github/workflows/dotnet.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index aa66cc4..4a8f3f7 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -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" From d608d72a99f522c7d827229ab9b350389bde4d03 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Jun 2024 20:16:57 -0400 Subject: [PATCH 8/8] [CONFIG] [.NET] exclude project from sonarcloud --- .../algorithm-exercises-csharp-base.csproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj b/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj index ab5d5c2..348ceed 100644 --- a/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj +++ b/algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj @@ -6,6 +6,8 @@ enable enable + true + true