Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 8c451a8

Browse files
Youssef1313ethanis
andauthored
Use Microsoft.CodeAnalysis.NetAnalyzers and code-style analyzers (#58)
* Use Microsoft.CodeAnalysis.NetAnalyzers and code-style analyzers * Fix CA1822 (make static) and CA2000 (dispose before losing scope) * Fix CA1062 (validate parameter of public method) * Disable CA2007 * Fix CA1815 * Fix CA1310 * Fix CA1307 * Update AppTests.cs * Cleanup * Address review comments * More cleanup * Update src/Valet.UnitTests/Models/ManifestTests.cs * Update src/Valet.UnitTests/Services/DockerServiceTests.cs * Run dotnet format whitespace * Disable JSON002 Co-authored-by: Ethan Dennis <ethanis@github.com> Co-authored-by: Ethan Dennis <erdennis13@gmail.com>
1 parent 664f380 commit 8c451a8

65 files changed

Lines changed: 271 additions & 147 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/.editorconfig

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
# Don't use tabs for indentation.
5+
[*]
6+
indent_style = space
7+
# (Please don't specify an indent_size here; that has too many unintended consequences.)
8+
9+
# Code files
10+
[*.{cs,csx,vb,vbx}]
11+
indent_size = 4
12+
insert_final_newline = true
13+
charset = utf-8-bom
14+
15+
# XML project files
16+
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
17+
indent_size = 2
18+
19+
# XML config files
20+
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
21+
indent_size = 2
22+
23+
# JSON files
24+
[*.json]
25+
indent_size = 2
26+
27+
# Powershell files
28+
[*.ps1]
29+
indent_size = 2
30+
31+
# Shell script files
32+
[*.sh]
33+
end_of_line = lf
34+
indent_size = 2
35+
36+
# The following applies to unit tests directory only.
37+
[Valet.UnitTests/**.cs]
38+
39+
# CA1707: Identifiers should not contain underscores
40+
dotnet_diagnostic.CA1707.severity = none
41+
42+
# The following applies to all files.
43+
[*.cs]
44+
45+
dotnet_analyzer_diagnostic.category-Style.severity = warning
46+
47+
csharp_style_namespace_declarations = file_scoped
48+
49+
# IDE0008: Use explicit type
50+
dotnet_diagnostic.IDE0008.severity = none
51+
52+
# IDE0058: Expression value is never used
53+
dotnet_diagnostic.IDE0058.severity = none
54+
55+
# IDE0022: Use block body for methods
56+
dotnet_diagnostic.IDE0022.severity = none
57+
58+
# IDE0011: Add braces
59+
dotnet_diagnostic.IDE0011.severity = none
60+
61+
# IDE0130: Namespace does not match folder structure
62+
dotnet_diagnostic.IDE0130.severity = none
63+
64+
# JSON002: Probable JSON string detected
65+
dotnet_diagnostic.JSON002.severity = none
66+
67+
68+
# CA2007: Consider calling ConfigureAwait on the awaited task
69+
dotnet_diagnostic.CA2007.severity = none
70+
71+
# CA1303: Do not pass literals as localized parameters
72+
dotnet_diagnostic.CA1303.severity = none
73+
74+
# CA1002: Do not expose generic lists
75+
dotnet_diagnostic.CA1002.severity = none
76+
77+
# CA1724: Type names should not match namespaces
78+
dotnet_diagnostic.CA1724.severity = none
79+
80+
# CA2227: Collection properties should be read only
81+
dotnet_diagnostic.CA2227.severity = none
82+
83+
# CA2201: Do not raise reserved exception types
84+
dotnet_diagnostic.CA2201.severity = none
85+
86+
# CA1065: Do not raise exceptions in unexpected locations
87+
dotnet_diagnostic.CA1065.severity = none
88+
89+
# CA1031: Do not catch general exception types
90+
dotnet_diagnostic.CA1031.severity = none
91+
92+
# CA1812: Avoid uninstantiated internal classes
93+
dotnet_diagnostic.CA1812.severity = none
94+
95+
# CA1815: Override equals and operator equals on value types
96+
dotnet_diagnostic.CA1815.severity = none
97+
98+
# CA1014: Mark assemblies with CLSCompliant
99+
dotnet_diagnostic.CA1014.severity = none

src/Directory.Build.props

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<AnalysisLevel>latest</AnalysisLevel>
5+
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
6+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
7+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
8+
<Nullable>enable</Nullable>
9+
<Features>strict</Features>
10+
</PropertyGroup>
11+
12+
</Project>

src/Valet.UnitTests/AppTests.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Threading.Tasks;
44
using Moq;
55
using NUnit.Framework;
66
using Valet.Interfaces;
7-
using Valet.Services;
87

98
namespace Valet.UnitTests;
109

@@ -16,6 +15,7 @@ public class AppTests
1615
private Mock<IDockerService> _dockerService;
1716
private Mock<IConfigurationService> _configurationService;
1817
private App _app;
18+
private TextWriter _out;
1919
#pragma warning restore CS8618
2020

2121
[SetUp]
@@ -25,6 +25,13 @@ public void BeforeEachTest()
2525
_processService = new Mock<IProcessService>();
2626
_configurationService = new Mock<IConfigurationService>();
2727
_app = new App(_dockerService.Object, _processService.Object, _configurationService.Object);
28+
_out = Console.Out;
29+
}
30+
31+
[TearDown]
32+
public void AfterEachTest()
33+
{
34+
Console.SetOut(_out);
2835
}
2936

3037
[TestCase("4256ea72fd01deac3e967f6b19f907587dcd6f0a976301f1aecc73dc6f146a4a", "4256ea72fd01deac3e967f6b19f907587dcd6f0a976301f1aecc73dc6f146a4a", "")]
@@ -35,7 +42,7 @@ public async Task CheckForUpdates_NoUpdatesNeeded(string? latestImage, string? c
3542
var image = "valet-customers/valet-cli";
3643
var server = "ghcr.io";
3744

38-
var stringWriter = new StringWriter();
45+
using var stringWriter = new StringWriter();
3946
Console.SetOut(stringWriter);
4047

4148
_dockerService.Setup(handler =>
@@ -61,7 +68,7 @@ public async Task CheckForUpdates_RaisesCaughtException()
6168
var image = "valet-customers/valet-cli";
6269
var server = "ghcr.io";
6370

64-
var stringWriter = new StringWriter();
71+
using var stringWriter = new StringWriter();
6572
Console.SetOut(stringWriter);
6673

6774
_dockerService.Setup(handler =>
@@ -75,4 +82,4 @@ public async Task CheckForUpdates_RaisesCaughtException()
7582
Assert.AreEqual("", stringWriter.ToString());
7683
_processService.VerifyAll();
7784
}
78-
}
85+
}

src/Valet.UnitTests/Models/ManifestTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text.Json;
2-
using Moq;
1+
using System.Text.Json;
32
using NUnit.Framework;
43
using Valet.Models.Docker;
54

@@ -8,7 +7,7 @@ namespace Valet.UnitTests;
87
[TestFixture]
98
public class ManifestTests
109
{
11-
private string manifestResult = @"
10+
private readonly string manifestResult = @"
1211
{
1312
""schemaVersion"": 2,
1413
""mediaType"": ""application/vnd.docker.distribution.manifest.v2+json"",
@@ -84,4 +83,4 @@ public void GetDigest()
8483

8584
Assert.AreEqual("4256ea72fd01deac3e967f6b19f907587dcd6f0a976301f1aecc73dc6f146a4a", manifest?.GetDigest());
8685
}
87-
}
86+
}

src/Valet.UnitTests/Models/StringExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NUnit.Framework;
1+
using NUnit.Framework;
22
using Valet.Models;
33

44
namespace Valet.UnitTests.Models;
@@ -12,4 +12,4 @@ public void EscapeIfNeeded_ReturnsExpected(string input, string expected)
1212
{
1313
Assert.AreEqual(expected, input.EscapeIfNeeded());
1414
}
15-
}
15+
}

src/Valet.UnitTests/Models/VariableTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NUnit.Framework;
1+
using NUnit.Framework;
22
using Valet.Models;
33

44
namespace Valet.UnitTests.Models;
@@ -45,4 +45,4 @@ public void Placeholder_ReturnsExpected(bool isPassword, string? defaultValue, s
4545
// Act
4646
Assert.AreEqual(expectedPlaceholder, variable.Placeholder);
4747
}
48-
}
48+
}

src/Valet.UnitTests/Services/ConfigurationServiceTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Collections.Immutable;
43
using System.IO;
54
using System.Threading.Tasks;
@@ -98,4 +97,4 @@ public async Task ReadCurrentVariablesAsync_FileExists_ReturnsVariables()
9897
// Assert
9998
Assert.AreEqual(expectedResult, result);
10099
}
101-
}
100+
}

src/Valet.UnitTests/Services/DockerServiceTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Threading.Tasks;
@@ -157,7 +157,7 @@ public async Task ExecuteCommandAsync_InvokesDocker_ReturnsTrue()
157157
"docker",
158158
$"run --rm -t -v \"{Directory.GetCurrentDirectory()}\":/data {server}/{image}:{version} {string.Join(' ', arguments)}",
159159
Directory.GetCurrentDirectory(),
160-
new[] { new System.ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
160+
new[] { new ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
161161
true,
162162
null
163163
)
@@ -188,7 +188,7 @@ public async Task ExecuteCommandAsync_InvokesDocker_WithEnvironmentVariables_Ret
188188
"docker",
189189
$"run --rm -t --env GITHUB_ACCESS_TOKEN=foo --env GITHUB_INSTANCE_URL=https://github.fabrikam.com --env JENKINS_ACCESS_TOKEN=bar -v \"{Directory.GetCurrentDirectory()}\":/data {server}/{image}:{version} {string.Join(' ', arguments)}",
190190
Directory.GetCurrentDirectory(),
191-
new[] { new System.ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
191+
new[] { new ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
192192
true,
193193
null
194194
)
@@ -217,7 +217,7 @@ public async Task ExecuteCommandAsync_InvokesDocker_WithAdditionalDockerArgument
217217
"docker",
218218
$"run --rm -t --network=host -v \"{Directory.GetCurrentDirectory()}\":/data {server}/{image}:{version} {string.Join(' ', arguments)}",
219219
Directory.GetCurrentDirectory(),
220-
new[] { new System.ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
220+
new[] { new ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
221221
true,
222222
null
223223
)
@@ -422,4 +422,4 @@ public async Task GetLatestImageDigest_ParsesDigestCorrectly()
422422
Assert.AreEqual("4256ea72fd01deac3e967f6b19f907587dcd6f0a976301f1aecc73dc6f146a4a", result);
423423
_processService.VerifyAll();
424424
}
425-
}
425+
}

src/Valet.UnitTests/Valet.UnitTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5-
<Nullable>enable</Nullable>
65

76
<IsPackable>false</IsPackable>
87
</PropertyGroup>

src/Valet/App.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using Valet.Interfaces;
1+
using Valet.Interfaces;
22
using Valet.Models;
33

44
namespace Valet;
55

66
public class App
77
{
8-
const string ValetImage = "valet-customers/valet-cli";
9-
const string ValetContainerRegistry = "ghcr.io";
8+
private const string ValetImage = "valet-customers/valet-cli";
9+
private const string ValetContainerRegistry = "ghcr.io";
1010

1111
private readonly IDockerService _dockerService;
1212
private readonly IProcessService _processService;
@@ -64,7 +64,7 @@ public async Task<int> GetVersionAsync()
6464

6565
var formattedGhVersion = ghVersion.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault();
6666
var formattedGhValetVersion = ghValetVersion.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
67-
.FirstOrDefault(x => x.Contains("github/gh-valet"));
67+
.FirstOrDefault(x => x.Contains("github/gh-valet", StringComparison.Ordinal));
6868
var formattedValetVersion = valetVersion.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault() ?? "unknown";
6969

7070
Console.WriteLine(formattedGhVersion);
@@ -83,10 +83,10 @@ public async Task CheckForUpdatesAsync()
8383

8484
await Task.WhenAll(latestImageDigestTask, currentImageDigestTask);
8585

86-
var latestImageDigest = latestImageDigestTask?.Result;
87-
var currentImageDigest = currentImageDigestTask?.Result;
86+
var latestImageDigest = await latestImageDigestTask;
87+
var currentImageDigest = await currentImageDigestTask;
8888

89-
if (latestImageDigest != null && currentImageDigest != null && !latestImageDigest.Equals(currentImageDigest))
89+
if (latestImageDigest != null && currentImageDigest != null && !latestImageDigest.Equals(currentImageDigest, StringComparison.Ordinal))
9090
{
9191
Console.WriteLine("A new version of the Valet CLI is available. Run 'gh valet update' to update.");
9292
}
@@ -108,4 +108,4 @@ public async Task<int> ConfigureAsync()
108108
Console.WriteLine("Environment variables successfully updated.");
109109
return 0;
110110
}
111-
}
111+
}

0 commit comments

Comments
 (0)