-
Notifications
You must be signed in to change notification settings - Fork 2.1k
C#: Re-factor DependabotProxy class to allow unit-testing. #22477
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
Open
michaelnebel
wants to merge
5
commits into
github:main
Choose a base branch
from
michaelnebel:csharp/refactordependabotproxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee6a179
C#: Add explicit class with dependabot configuration.
michaelnebel 68d8351
C#: Move some logic into the DependabotProxy object construction.
michaelnebel b3ae7ff
C#: Minor re-write to enable OS independant testing of dependabot pro…
michaelnebel 8583ecc
C#: Add DependabotProxy unit-tests.
michaelnebel 2dc8ef9
C#: Address some review comments.
michaelnebel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxyConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using System; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.DependencyFetching | ||
| { | ||
| public class DependabotProxyConfiguration : IDependabotProxyConfiguration | ||
| { | ||
| public string? Host { get; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost); | ||
|
|
||
| public string? Port { get; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort); | ||
|
|
||
| public string? Certificate { get; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate); | ||
|
|
||
| public string? RegistryURLs { get; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyURLs); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...rp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDependabotProxyConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.DependencyFetching | ||
| { | ||
| public interface IDependabotProxyConfiguration | ||
| { | ||
| // The host of the Dependabot proxy, if available. | ||
| string? Host { get; } | ||
|
|
||
| // The port of the Dependabot proxy, if available. | ||
| string? Port { get; } | ||
|
|
||
| // The certificate of the Dependabot proxy, if available. | ||
| string? Certificate { get; } | ||
|
|
||
| // The list of package registries that are configured for the proxy, if any. | ||
| // The value of the environment variable should be a JSON array of objects, such as: | ||
| // [ { "type": "nuget_feed", "url": "https://nuget.pkg.github.com/org/index.json" } ] | ||
| string? RegistryURLs { get; } | ||
| } | ||
| } |
185 changes: 185 additions & 0 deletions
185
csharp/extractor/Semmle.Extraction.Tests/DependabotProxy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| using Xunit; | ||
| using System; | ||
| using System.IO; | ||
| using Semmle.Extraction.CSharp.DependencyFetching; | ||
| using Semmle.Util; | ||
|
|
||
| namespace Semmle.Extraction.Tests | ||
| { | ||
| public class DependabotConfigurationStub : IDependabotProxyConfiguration | ||
| { | ||
| public string? Host { get; set; } | ||
| public string? Port { get; set; } | ||
| public string? Certificate { get; set; } | ||
| public string? RegistryURLs { get; set; } | ||
| } | ||
|
|
||
| public class DiagnosticsWriterStub : IDiagnosticsWriter | ||
| { | ||
| public void AddEntry(Semmle.Util.DiagnosticMessage entry) { } | ||
| public void Dispose() { } | ||
| } | ||
|
|
||
| public class DependabotProxyTests | ||
| { | ||
| private static TemporaryDirectory MakeTemporaryDirectory() | ||
| { | ||
| var tmp = Path.Join(Path.GetTempPath(), "DependabotProxyTests", Guid.NewGuid().ToString()); | ||
| return new TemporaryDirectory(tmp, "testing", new LoggerStub()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestDependabotProxyCreation1() | ||
| { | ||
| // Setup | ||
| var config = new DependabotConfigurationStub | ||
| { | ||
| Host = "localhost", | ||
| Port = "", | ||
| }; | ||
|
|
||
| // Execute | ||
| using var tempWorkingDirectory = MakeTemporaryDirectory(); | ||
| using var proxy = DependabotProxy.Make(config, new LoggerStub(), new DiagnosticsWriterStub(), tempWorkingDirectory); | ||
|
|
||
| // Verify | ||
| Assert.Null(proxy); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestDependabotProxyCreation2() | ||
| { | ||
| // Setup | ||
| var config = new DependabotConfigurationStub | ||
| { | ||
| Port = "8080", | ||
| }; | ||
|
|
||
| // Execute | ||
| using var tempWorkingDirectory = MakeTemporaryDirectory(); | ||
| using var proxy = DependabotProxy.Make(config, new LoggerStub(), new DiagnosticsWriterStub(), tempWorkingDirectory); | ||
|
|
||
| // Verify | ||
| Assert.Null(proxy); | ||
| } | ||
|
|
||
| private const string ExampleCertificate = """ | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIFJTCCAw2gAwIBAgIUDImU6YnuAqJ1QuRp+OpJQPnPu6wwDQYJKoZIhvcNAQEL | ||
| BQAwFD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BCwUAA4ICAQCc5u8qNHHG | ||
| kONjfvq7Denq6QaEt4dZZDDODAvgUzZnnBjEhgrp7zfxtbyU/I0+DWnKQMKA9wPM | ||
| ktiFGd0lldEqoT+E7b0kN124lBGqZ/uYkhsWZ0Nc5dD+UB9oJszwOc5KNuquOnr6 | ||
| SbsfXVm4yLvVLXl67c0jvqvRgGg9/6Q6eMzohW6abMdbYhS28/DsJhCea/dV3+L1 | ||
| oVJ3O/A8e86m174ZCGE8s9UtnVYylBkAryDqaaQLdOBQ2C7uxdRAUNHSIa2JlqUc | ||
| 5+cod8lFojKb74hbgj6wkXyajsFttqYMh7CeASsnjZXDQ4MC3DqqDVCZuNvJ85Rt | ||
| ya3Tljp4Ln2AAAoKC3REUeU8PQqpk1vVIj0FSr3RvBTvwzyNfWFVqyBiXTATuV9n | ||
| 6AemqqXo5MZrHHeRaSTF8A70Jxbt9yx75xQxp3O3tdEL1Mxbl9X7c/hizOfLbeHH | ||
| IkAgzALQgi87Zbf2tOhRwH5NrB4ijyUUfovRHUwzsZOoTNqlVeNzbDRVbegx9V99 | ||
| /3vwNZgpStGl/JYhN9qY5hJKnC64ltMvuNGpLeJCGyFkrtFS8gKkgR7VKrGo7h3+ | ||
| Zo8rz8TFjP7RmSgQbrmFuPqNOGXzPidu2sMMFacKV7Rn4bEtHzW3MDhqVD4w/pGD | ||
| L0xpnWjzLYltVjz8mo07yh+zQ10G71Cl1w== | ||
| -----END CERTIFICATE----- | ||
| """; | ||
|
|
||
| [Fact] | ||
| public void TestDependabotProxyCertificate() | ||
| { | ||
| // Setup | ||
| var config = new DependabotConfigurationStub | ||
| { | ||
| Port = "8080", | ||
| Host = "localhost", | ||
| Certificate = ExampleCertificate | ||
| }; | ||
|
|
||
| // Execute | ||
| using var tempWorkingDirectory = MakeTemporaryDirectory(); | ||
| using var proxy = DependabotProxy.Make(config, new LoggerStub(), new DiagnosticsWriterStub(), tempWorkingDirectory); | ||
|
|
||
| // Verify | ||
| Assert.NotNull(proxy); | ||
| Assert.Equal("http://localhost:8080", proxy.Address); | ||
| Assert.NotNull(proxy.Certificate); | ||
| Assert.NotNull(proxy.CertificatePath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestDependabotRegistryUrls1() | ||
| { | ||
| // Setup | ||
| var config = new DependabotConfigurationStub | ||
| { | ||
| Port = "8080", | ||
| Host = "localhost", | ||
| RegistryURLs = "Doesn't parse as a JSON list" | ||
| }; | ||
|
|
||
| // Execute | ||
| using var tempWorkingDirectory = MakeTemporaryDirectory(); | ||
| using var proxy = DependabotProxy.Make(config, new LoggerStub(), new DiagnosticsWriterStub(), tempWorkingDirectory); | ||
|
|
||
| // Verify | ||
| Assert.NotNull(proxy); | ||
| Assert.Equal([], proxy.RegistryURLs); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestDependabotRegistryUrls2() | ||
| { | ||
| // Setup | ||
| var config = new DependabotConfigurationStub | ||
| { | ||
| Port = "8080", | ||
| Host = "localhost", | ||
| RegistryURLs = "[ { \"type\": \"nuget_feed\", \"url\": \"https://nuget.pkg.github.com/org/index.json\" } ]" | ||
| }; | ||
|
|
||
| // Execute | ||
| using var tempWorkingDirectory = MakeTemporaryDirectory(); | ||
| using var proxy = DependabotProxy.Make(config, new LoggerStub(), new DiagnosticsWriterStub(), tempWorkingDirectory); | ||
|
|
||
| // Verify | ||
| Assert.NotNull(proxy); | ||
| Assert.Equal([ | ||
| "https://nuget.pkg.github.com/org/index.json" | ||
| ], proxy.RegistryURLs); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestDependabotRegistryUrls3() | ||
| { | ||
| // Setup | ||
| var config = new DependabotConfigurationStub | ||
| { | ||
| Port = "8080", | ||
| Host = "localhost", | ||
| RegistryURLs = "[ { \"type\": \"nuget_feed\", \"url\": \"https://example.com/org/index.json\" }, { \"type\": \"wrong_type\", \"url\": \"https://nuget.pkg.github.com/org/index.json\" } ]" | ||
| }; | ||
|
|
||
| // Execute | ||
| using var tempWorkingDirectory = MakeTemporaryDirectory(); | ||
| using var proxy = DependabotProxy.Make(config, new LoggerStub(), new DiagnosticsWriterStub(), tempWorkingDirectory); | ||
|
|
||
| // Verify | ||
| Assert.NotNull(proxy); | ||
| Assert.Equal([ | ||
| "https://example.com/org/index.json" | ||
| ], proxy.RegistryURLs); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Again probably something I was responsible for in the original implementation, but here and elsewhere, it might make sense to change the wording of the log messages to not mention "Dependabot". While that's technically accurate, it is probably confusing for users to see in the log. E.g. "Authentication proxy" or "Registry proxy" might be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Should we rename the class to
RegistryProxy?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do if you wanted, but since the class name is not user-facing it's less significant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but it may be confusing at a later point in time, if we are not consistent with the (internal) naming and the user facing messaging. Will make a follow up PR with renaming/updating of log messages.