Skip to content

Commit a835ded

Browse files
authored
Merge pull request #74 from sir-gon/develop
Develop
2 parents 04508c4 + d608d72 commit a835ded

File tree

11 files changed

+240
-19
lines changed

11 files changed

+240
-19
lines changed

.github/workflows/dotnet.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ jobs:
3838
- name: Lint (codestyle)
3939
run: dotnet format --verify-no-changes --verbosity normal
4040
- name: Test
41-
run: dotnet test --no-build --verbosity normal
41+
run: >
42+
dotnet test --no-build
43+
--verbosity normal
44+
--logger "console;verbosity=detailed"

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ COPY ./CODE_OF_CONDUCT.md ${WORKDIR}/
3232

3333
# Code source
3434
COPY ./algorithm-exercises-csharp ${WORKDIR}/algorithm-exercises-csharp
35+
COPY ./algorithm-exercises-csharp-base ${WORKDIR}/algorithm-exercises-csharp-base
3536
COPY ./algorithm-exercises-csharp-test ${WORKDIR}/algorithm-exercises-csharp-test
3637
COPY ./algorithm-exercises-csharp.sln ${WORKDIR}/algorithm-exercises-csharp.sln
3738
COPY ./Makefile ${WORKDIR}/
@@ -52,6 +53,7 @@ CMD ["make", "lint"]
5253
FROM base AS development
5354

5455
COPY ./algorithm-exercises-csharp ${WORKDIR}/algorithm-exercises-csharp
56+
COPY ./algorithm-exercises-csharp-base ${WORKDIR}/algorithm-exercises-csharp-base
5557
COPY ./algorithm-exercises-csharp-test ${WORKDIR}/algorithm-exercises-csharp-test
5658
COPY ./algorithm-exercises-csharp.sln ${WORKDIR}/algorithm-exercises-csharp.sln
5759
COPY ./Makefile ${WORKDIR}/
@@ -111,4 +113,4 @@ RUN ls -alh
111113
USER worker
112114
CMD ["make", "run"]
113115

114-
# checkov:skip= CKV_DOCKER_2: production image isn't a service process (yet)
116+
# checkov:skip= CKV_DOCKER_2: production image isn't a service process (yet)

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ release: dependencies
9191
${PACKAGE_TOOL} publish --verbosity ${VERBOSITY_LEVEL}
9292

9393
test: build
94-
${PACKAGE_TOOL} test --verbosity ${VERBOSITY_LEVEL} --collect:"Code Coverage"
94+
${PACKAGE_TOOL} test --verbosity ${VERBOSITY_LEVEL} \
95+
--collect:"Code Coverage" \
96+
--logger "console;verbosity=detailed"
9597

9698
coverage: dependencies test
9799
cat coverage-report/Summary.txt

README.md

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ require docker-compose and make installed.
8282
Using a dotnet 8.0 stack in your SO. You must install dependencies:
8383

8484
```bash
85-
dotnet restore --verbosity=normal
85+
dotnet restore --verbosity normal
8686
```
8787

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

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

94+
By default, there are no log outputs from the application or tests to the console.
95+
"--verbosity \<LEVEL\>" only control verbosity of dotnet proccess
96+
(building, test running, ...)
97+
9498
```bash
95-
dotnet test --verbosity normal
99+
dotnet clean && dotnet test --verbosity normal
96100
```
97101

98-
#### Test run with alternative behaviors
102+
To enable console detailed output use:
99103

100-
> [!IMPORTANT]
101-
> BRUTEFORCE and LOG_LEVEL environment variables not yet implemented.
102-
>
103-
> Currently tests only have one behavior.
104+
```bash
105+
dotnet test --logger "console;verbosity=detailed"
106+
```
104107

105-
~~You can change test running behaviour using some environment variables as follows:~~
108+
#### Test run with alternative behaviors
109+
110+
You can change test running behaviour using some environment variables as follows:
106111

107112
| Variable | Values | Default |
108113
| ------ | ------ | ------ |
109114
| LOG_LEVEL | `debug`, `warning`, `error`, `info` | `info` |
110115
| BRUTEFORCE | `true`, `false`| `false` |
111116

112-
- ~~`LOG_LEVEL`: change verbosity level in outputs.~~
113-
- ~~`BRUTEFORCE`: enable or disable running large tests.
114-
(long time, large amount of data, high memory consumition).~~
117+
- `LOG_LEVEL`: change verbosity level in outputs.
118+
- `BRUTEFORCE`: enable or disable running large tests.
119+
(long time, large amount of data, high memory consumition).
115120

116121
#### Examples running tests with alternative behaviors
117122

118-
> [!IMPORTANT]
119-
> BRUTEFORCE and LOG_LEVEL environment variables not yet implemented.
120-
>
121-
> Currently tests only have one behavior.
123+
Run tests with debug outputs:
124+
125+
```bash
126+
LOG_LEVEL=debug dotnet test --logger "console;verbosity=detailed"
127+
```
128+
129+
Run brute-force tests with debug outputs:
130+
131+
```bash
132+
BRUTEFORCE=true LOG_LEVEL=debug dotnet test --logger "console;verbosity=detailed"
133+
```
122134

123135
### Install and Run using make
124136

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

146+
Run tests with debug outputs:
147+
148+
```bash
149+
make test -e LOG_LEVEL=debug
150+
```
151+
152+
Run brute-force tests with debug outputs:
153+
154+
```bash
155+
make test -e BRUTEFORCE=true -e LOG_LEVEL=debug
156+
```
157+
158+
Alternative way, use environment variables as prefix:
159+
160+
```bash
161+
BRUTEFORCE=true LOG_LEVEL=debug make test
162+
```
163+
134164
### Install and Running with Docker 🐳
135165

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

176+
To change behavior using environment variables, you can pass to containers
177+
in the following ways:
178+
179+
From host using docker-compose (compose.yaml) mechanism:
180+
181+
```bash
182+
BRUTEFORCE=true docker-compose --profile testing run --rm algorithm-exercises-csharp-test
183+
```
184+
185+
Overriding docker CMD, as parameter of make "-e":
186+
187+
```bash
188+
docker-compose --profile testing run --rm algorithm-exercises-csharp-test make test -e BRUTEFORCE=true
189+
```
190+
191+
```bash
192+
docker-compose --profile testing run --rm algorithm-exercises-csharp-test make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
193+
```
194+
146195
### Install and Running with Docker 🐳 using make
147196

148197
```bash
149198
make compose/build
150199
make compose/test
151200
```
152201

202+
To pass environment variables you can use docker-compose
203+
or overriding CMD and passing to make as "-e" argument.
204+
205+
Passing environment variables using docker-compose (compose.yaml mechanism):
206+
207+
```bash
208+
BRUTEFORCE=true LOG_LEVEL=debug make compose/test
209+
```
210+
153211
## Development workflow using Docker / docker-compose
154212

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

227+
Or with detailed output to terminal:
228+
229+
```bash
230+
# Build development target image
231+
docker-compose build --compress algorithm-exercises-csharp-dev
232+
233+
# Run ephemeral container and override command to run test
234+
docker-compose run --rm algorithm-exercises-csharp-dev dotnet test --logger "console;verbosity=detailed"
235+
```
236+
169237
## Run complete workflow (Docker + make)
170238

171239
Following command simulates a standarized pipeline across environments,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<RootNamespace>algorithm_exercises_csharp</RootNamespace>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
9+
<SonarQubeExclude>true</SonarQubeExclude>
10+
11+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
12+
13+
<!-- Static Analysis -->
14+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
15+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
16+
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
17+
18+
</PropertyGroup>
19+
20+
<!-- Exclude test project from coverage -->
21+
<ItemGroup>
22+
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
27+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
28+
</ItemGroup>
29+
30+
31+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace algorithm_exercises_csharp;
2+
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
6+
public sealed class LoggerSingleton
7+
{
8+
private static readonly Lazy<LoggerSingleton> _instance = new Lazy<LoggerSingleton>(() => new LoggerSingleton());
9+
10+
public static LoggerSingleton Instance => _instance.Value;
11+
12+
public ILogger Logger { get; }
13+
14+
private LoggerSingleton()
15+
{
16+
// Read the LOG_LEVEL environment variable
17+
var logLevelEnv = Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "Information";
18+
19+
// Convert the environment variable value to LogLevel
20+
if (!Enum.TryParse<LogLevel>(logLevelEnv, ignoreCase: true, out var logLevel))
21+
{
22+
logLevel = LogLevel.Information; // Set the minimum logging level
23+
}
24+
25+
var loggerFactory = LoggerFactory.Create(builder =>
26+
{
27+
builder
28+
.AddConsole()
29+
.SetMinimumLevel(logLevel); // Configura el nivel mínimo de logging
30+
});
31+
32+
Logger = loggerFactory.CreateLogger("GlobalLogger");
33+
34+
Logger.LogInformation("Initializing");
35+
36+
Logger.LogInformation("Info level enabled");
37+
Logger.LogWarning("Warning level enabled");
38+
Logger.LogError("Error level enabled");
39+
Logger.LogDebug("Debug level enabled");
40+
}
41+
}
42+
43+
public static class Log
44+
{
45+
public static ILogger getLogger()
46+
{
47+
return LoggerSingleton.Instance.Logger;
48+
}
49+
50+
public static void info(string message)
51+
{
52+
LoggerSingleton.Instance.Logger.LogInformation(message);
53+
}
54+
55+
public static void warning(string message)
56+
{
57+
LoggerSingleton.Instance.Logger.LogWarning(message);
58+
}
59+
60+
public static void error(string message)
61+
{
62+
LoggerSingleton.Instance.Logger.LogError(message);
63+
}
64+
65+
public static void debug(string message)
66+
{
67+
LoggerSingleton.Instance.Logger.LogDebug(message);
68+
}
69+
}

algorithm-exercises-csharp-test/algorithm-exercises-csharp-test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<PackageReference Include="ReportGenerator" Version="5.3.6" />
3636
</ItemGroup>
3737

38+
<!-- Exclude test project from coverage -->
39+
<ItemGroup>
40+
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
41+
</ItemGroup>
42+
3843
<ItemGroup>
3944
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
4045
</ItemGroup>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace algorithm_exercises_csharp;
2+
3+
[TestClass]
4+
public class HelloWorldBruteForceTest
5+
{
6+
private static readonly string[] allowedValues = ["TRUE", "YES", "1"];
7+
8+
[TestInitialize]
9+
public void testInitialize()
10+
{
11+
var envValue = Environment.GetEnvironmentVariable("BRUTEFORCE");
12+
envValue = envValue?.ToUpper();
13+
if (!allowedValues.Contains(envValue, StringComparer.OrdinalIgnoreCase))
14+
{
15+
Assert.Inconclusive($"Skipping BRUTEFORCE test because environment variable 'BRUTEFORCE' is not one of the expected values.");
16+
}
17+
}
18+
19+
[TestMethod]
20+
public void testHelloBruteForce()
21+
{
22+
string expected = "Hello World!";
23+
string result = HelloWorld.hello();
24+
25+
Assert.AreEqual(expected, result);
26+
}
27+
}

algorithm-exercises-csharp-test/src/Hello.Test.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ namespace algorithm_exercises_csharp;
33
[TestClass]
44
public class HelloWorldTest
55
{
6+
[TestInitialize]
7+
public void testInitialize()
8+
{
9+
Log.info("Hello World");
10+
}
11+
612
[TestMethod]
713
public void testInstance()
814
{
@@ -34,7 +40,6 @@ public void testHello()
3440
string result = HelloWorld.hello();
3541

3642
Assert.AreEqual(expected, result);
37-
3843
}
3944
}
4045

algorithm-exercises-csharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp-
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp", "algorithm-exercises-csharp\algorithm-exercises-csharp.csproj", "{B162EE62-90C6-4871-B278-390804615987}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithm-exercises-csharp-base", "algorithm-exercises-csharp-base\algorithm-exercises-csharp-base.csproj", "{1BC65C42-83A6-486D-84DB-0DC63002FA24}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{B162EE62-90C6-4871-B278-390804615987}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{B162EE62-90C6-4871-B278-390804615987}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{B162EE62-90C6-4871-B278-390804615987}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{1BC65C42-83A6-486D-84DB-0DC63002FA24}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

algorithm-exercises-csharp/algorithm-exercises-csharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@
1515

1616
</PropertyGroup>
1717

18+
<ItemGroup>
19+
<ProjectReference Include="../algorithm-exercises-csharp-base/algorithm-exercises-csharp-base.csproj" />
20+
</ItemGroup>
1821
</Project>

0 commit comments

Comments
 (0)