From c67cdf37ca002f1dc02c8f15a967f6fcee2ee2a2 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Mon, 29 Jul 2024 20:52:13 +0300 Subject: [PATCH 1/5] CA1062#Aspire.Hosting.Python --- .../PythonProjectResourceBuilderExtensions.cs | 7 + .../PythonPublicApiTests.cs | 319 ++++++++++++++++++ 2 files changed, 326 insertions(+) create mode 100644 tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs diff --git a/src/Aspire.Hosting.Python/PythonProjectResourceBuilderExtensions.cs b/src/Aspire.Hosting.Python/PythonProjectResourceBuilderExtensions.cs index c1a04d78d7b..0e4734137b9 100644 --- a/src/Aspire.Hosting.Python/PythonProjectResourceBuilderExtensions.cs +++ b/src/Aspire.Hosting.Python/PythonProjectResourceBuilderExtensions.cs @@ -60,6 +60,8 @@ public static class PythonProjectResourceBuilderExtensions public static IResourceBuilder AddPythonProject( this IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, params string[] scriptArgs) { + ArgumentNullException.ThrowIfNull(builder); + return builder.AddPythonProject(name, projectDirectory, scriptPath, ".venv", scriptArgs); } @@ -108,7 +110,12 @@ public static IResourceBuilder AddPythonProject( this IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, string virtualEnvironmentPath, params string[] scriptArgs) { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(name); + ArgumentNullException.ThrowIfNull(projectDirectory); + ArgumentNullException.ThrowIfNull(scriptPath); ArgumentNullException.ThrowIfNull(virtualEnvironmentPath); + ArgumentNullException.ThrowIfNull(scriptArgs); projectDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, projectDirectory)); var virtualEnvironment = new VirtualEnvironment(Path.IsPathRooted(virtualEnvironmentPath) diff --git a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs new file mode 100644 index 00000000000..f175659ff92 --- /dev/null +++ b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs @@ -0,0 +1,319 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +namespace Aspire.Hosting.Python.Tests; + +public class PythonPublicApiTests +{ + #region PythonProjectResource + + [Fact] + public void CtorPythonProjectResourceShouldThrowsWhenNameIsNull() + { + string name = null!; + const string executablePath = "/src/python"; + const string projectDirectory = "/data/python"; + + var action = () => new PythonProjectResource(name, executablePath, projectDirectory); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + }); + } + + [Fact] + public void CtorPythonProjectResourceShouldThrowsWhenExecutablePathIsNull() + { + const string name = "Python"; + string executablePath = null!; + const string projectDirectory = "/data/python"; + + var action = () => new PythonProjectResource(name, executablePath, projectDirectory); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal("command", exception.ParamName); + }); + } + + [Fact] + public void CtorPythonProjectResourceShouldThrowsWhenProjectDirectoryIsNull() + { + const string name = "Python"; + const string executablePath = "/src/python"; + string projectDirectory = null!; + + var action = () => new PythonProjectResource(name, executablePath, projectDirectory); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal("workingDirectory", exception.ParamName); + }); + } + + #endregion + + #region PythonProjectResourceBuilderExtensions + + [Fact] + public void AddPythonProjectShouldThrowsWhenBuilderIsNull() + { + IDistributedApplicationBuilder builder = null!; + const string name = "Python"; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + string[] scriptArgs = ["--traces"]; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectShouldThrowsWhenNameIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + string name = null!; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + string[] scriptArgs = ["--traces"]; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectShouldThrowsWhenProjectDirectoryIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + string projectDirectory = null!; + const string scriptPath = "scripts"; + string[] scriptArgs = ["--traces"]; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(projectDirectory), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectShouldThrowsWhenScriptPathIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + const string projectDirectory = "/src/python"; + string scriptPath = null!; + string[] scriptArgs = ["--traces"]; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptPath), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectShouldThrowsWhenScriptArgsIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + string[] scriptArgs = null!; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptArgs), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenBuilderIsNull() + { + IDistributedApplicationBuilder builder = null!; + const string name = "Python"; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + var virtualEnvironmentPath = ".venv"; + string[] scriptArgs = ["--traces"]; ; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + virtualEnvironmentPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenNameIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + string name = null!; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + const string virtualEnvironmentPath = ".venv"; + string[] scriptArgs = ["--traces"]; ; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + virtualEnvironmentPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenProjectDirectoryIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + string projectDirectory = null!; + const string scriptPath = "scripts"; + const string virtualEnvironmentPath = ".venv"; + string[] scriptArgs = ["--traces"]; ; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + virtualEnvironmentPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(projectDirectory), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenScriptPathIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + const string projectDirectory = "/src/python"; + string scriptPath = null!; + const string virtualEnvironmentPath = ".venv"; + string[] scriptArgs = ["--traces"]; ; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + virtualEnvironmentPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptPath), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenVirtualEnvironmentPathIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + string virtualEnvironmentPath = null!; + string[] scriptArgs = ["--traces"]; ; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + virtualEnvironmentPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(virtualEnvironmentPath), exception.ParamName); + }); + } + + [Fact] + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenScriptArgsIsNull() + { + var builder = DistributedApplication.CreateBuilder(); + const string name = "Python"; + const string projectDirectory = "/src/python"; + const string scriptPath = "scripts"; + const string virtualEnvironmentPath = ".venv"; + string[] scriptArgs = null!; + + var action = () => builder.AddPythonProject( + name, + projectDirectory, + scriptPath, + virtualEnvironmentPath, + scriptArgs); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptArgs), exception.ParamName); + }); + } + + #endregion +} From ec25311a008d1ee8ccda4ac5196ec7ce0f43df4f Mon Sep 17 00:00:00 2001 From: ZLoo Date: Tue, 30 Jul 2024 08:38:08 +0300 Subject: [PATCH 2/5] Fix PR by feedback --- .../PythonPublicApiTests.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs index f175659ff92..53912430eca 100644 --- a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs +++ b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs @@ -10,7 +10,7 @@ public class PythonPublicApiTests #region PythonProjectResource [Fact] - public void CtorPythonProjectResourceShouldThrowsWhenNameIsNull() + public void CtorPythonProjectResourceShouldThrowWhenNameIsNull() { string name = null!; const string executablePath = "/src/python"; @@ -26,7 +26,7 @@ public void CtorPythonProjectResourceShouldThrowsWhenNameIsNull() } [Fact] - public void CtorPythonProjectResourceShouldThrowsWhenExecutablePathIsNull() + public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNull() { const string name = "Python"; string executablePath = null!; @@ -42,7 +42,7 @@ public void CtorPythonProjectResourceShouldThrowsWhenExecutablePathIsNull() } [Fact] - public void CtorPythonProjectResourceShouldThrowsWhenProjectDirectoryIsNull() + public void CtorPythonProjectResourceShouldThrowWhenProjectDirectoryIsNull() { const string name = "Python"; const string executablePath = "/src/python"; @@ -62,7 +62,7 @@ public void CtorPythonProjectResourceShouldThrowsWhenProjectDirectoryIsNull() #region PythonProjectResourceBuilderExtensions [Fact] - public void AddPythonProjectShouldThrowsWhenBuilderIsNull() + public void AddPythonProjectShouldThrowWhenBuilderIsNull() { IDistributedApplicationBuilder builder = null!; const string name = "Python"; @@ -84,7 +84,7 @@ public void AddPythonProjectShouldThrowsWhenBuilderIsNull() } [Fact] - public void AddPythonProjectShouldThrowsWhenNameIsNull() + public void AddPythonProjectShouldThrowWhenNameIsNull() { var builder = DistributedApplication.CreateBuilder(); string name = null!; @@ -106,7 +106,7 @@ public void AddPythonProjectShouldThrowsWhenNameIsNull() } [Fact] - public void AddPythonProjectShouldThrowsWhenProjectDirectoryIsNull() + public void AddPythonProjectShouldThrowWhenProjectDirectoryIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; @@ -128,7 +128,7 @@ public void AddPythonProjectShouldThrowsWhenProjectDirectoryIsNull() } [Fact] - public void AddPythonProjectShouldThrowsWhenScriptPathIsNull() + public void AddPythonProjectShouldThrowWhenScriptPathIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; @@ -150,7 +150,7 @@ public void AddPythonProjectShouldThrowsWhenScriptPathIsNull() } [Fact] - public void AddPythonProjectShouldThrowsWhenScriptArgsIsNull() + public void AddPythonProjectShouldThrowWhenScriptArgsIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; @@ -172,7 +172,7 @@ public void AddPythonProjectShouldThrowsWhenScriptArgsIsNull() } [Fact] - public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenBuilderIsNull() + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenBuilderIsNull() { IDistributedApplicationBuilder builder = null!; const string name = "Python"; @@ -196,7 +196,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenBuilderIsN } [Fact] - public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenNameIsNull() + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenNameIsNull() { var builder = DistributedApplication.CreateBuilder(); string name = null!; @@ -220,7 +220,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenNameIsNull } [Fact] - public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenProjectDirectoryIsNull() + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenProjectDirectoryIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; @@ -244,7 +244,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenProjectDir } [Fact] - public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenScriptPathIsNull() + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptPathIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; @@ -268,7 +268,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenScriptPath } [Fact] - public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenVirtualEnvironmentPathIsNull() + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenVirtualEnvironmentPathIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; @@ -292,7 +292,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenVirtualEnv } [Fact] - public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowsWhenScriptArgsIsNull() + public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptArgsIsNull() { var builder = DistributedApplication.CreateBuilder(); const string name = "Python"; From 386bd9ccbac4af698ba91b689b748c9f4a34800d Mon Sep 17 00:00:00 2001 From: ZLoo Date: Tue, 30 Jul 2024 20:06:20 +0300 Subject: [PATCH 3/5] Remove Assert.Multiple by feedback --- .../PythonPublicApiTests.cs | 98 ++++++------------- 1 file changed, 28 insertions(+), 70 deletions(-) diff --git a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs index 53912430eca..ca4f0ab30f0 100644 --- a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs +++ b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs @@ -18,11 +18,8 @@ public void CtorPythonProjectResourceShouldThrowWhenNameIsNull() var action = () => new PythonProjectResource(name, executablePath, projectDirectory); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(name), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); } [Fact] @@ -34,11 +31,8 @@ public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNull() var action = () => new PythonProjectResource(name, executablePath, projectDirectory); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal("command", exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal("command", exception.ParamName); } [Fact] @@ -50,11 +44,8 @@ public void CtorPythonProjectResourceShouldThrowWhenProjectDirectoryIsNull() var action = () => new PythonProjectResource(name, executablePath, projectDirectory); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal("workingDirectory", exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal("workingDirectory", exception.ParamName); } #endregion @@ -76,11 +67,8 @@ public void AddPythonProjectShouldThrowWhenBuilderIsNull() scriptPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(builder), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); } [Fact] @@ -98,11 +86,8 @@ public void AddPythonProjectShouldThrowWhenNameIsNull() scriptPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(name), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); } [Fact] @@ -120,11 +105,8 @@ public void AddPythonProjectShouldThrowWhenProjectDirectoryIsNull() scriptPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(projectDirectory), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(projectDirectory), exception.ParamName); } [Fact] @@ -142,11 +124,8 @@ public void AddPythonProjectShouldThrowWhenScriptPathIsNull() scriptPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(scriptPath), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptPath), exception.ParamName); } [Fact] @@ -164,11 +143,8 @@ public void AddPythonProjectShouldThrowWhenScriptArgsIsNull() scriptPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(scriptArgs), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptArgs), exception.ParamName); } [Fact] @@ -188,11 +164,8 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenBuilderIsNu virtualEnvironmentPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(builder), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); } [Fact] @@ -212,11 +185,8 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenNameIsNull( virtualEnvironmentPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(name), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); } [Fact] @@ -236,11 +206,8 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenProjectDire virtualEnvironmentPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(projectDirectory), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(projectDirectory), exception.ParamName); } [Fact] @@ -260,11 +227,8 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptPathI virtualEnvironmentPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(scriptPath), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptPath), exception.ParamName); } [Fact] @@ -284,11 +248,8 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenVirtualEnvi virtualEnvironmentPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(virtualEnvironmentPath), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(virtualEnvironmentPath), exception.ParamName); } [Fact] @@ -308,11 +269,8 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptArgsI virtualEnvironmentPath, scriptArgs); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(scriptArgs), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(scriptArgs), exception.ParamName); } #endregion From f75eb99a6fda9db2db3d7b625e8731ff496a3f22 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Thu, 1 Aug 2024 22:58:23 +0300 Subject: [PATCH 4/5] builder replace to TestDistributedApplicationBuilder Fix PR by feedback --- .../PythonProjectResource.cs | 12 ++++++- .../PythonPublicApiTests.cs | 31 +++++++------------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/Aspire.Hosting.Python/PythonProjectResource.cs b/src/Aspire.Hosting.Python/PythonProjectResource.cs index d50fa3b0dfe..267876dde39 100644 --- a/src/Aspire.Hosting.Python/PythonProjectResource.cs +++ b/src/Aspire.Hosting.Python/PythonProjectResource.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using Aspire.Hosting.ApplicationModel; namespace Aspire.Hosting.Python; @@ -12,7 +14,15 @@ namespace Aspire.Hosting.Python; /// The path to the executable used to run the python project. /// The path to the directory containing the python project. public class PythonProjectResource(string name, string executablePath, string projectDirectory) - : ExecutableResource(name, executablePath, projectDirectory), IResourceWithServiceDiscovery + : ExecutableResource(ThrowIfNull(name), ThrowIfNull(executablePath), ThrowIfNull(projectDirectory)), IResourceWithServiceDiscovery { + private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) + { + if (argument is null) + { + throw new ArgumentNullException(paramName); + } + return argument; + } } diff --git a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs index ca4f0ab30f0..cd74a091324 100644 --- a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs +++ b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs @@ -1,14 +1,13 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Aspire.Hosting.Utils; using Xunit; namespace Aspire.Hosting.Python.Tests; public class PythonPublicApiTests { - #region PythonProjectResource - [Fact] public void CtorPythonProjectResourceShouldThrowWhenNameIsNull() { @@ -32,7 +31,7 @@ public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNull() var action = () => new PythonProjectResource(name, executablePath, projectDirectory); var exception = Assert.Throws(action); - Assert.Equal("command", exception.ParamName); + Assert.Equal(nameof(executablePath), exception.ParamName); } [Fact] @@ -45,13 +44,9 @@ public void CtorPythonProjectResourceShouldThrowWhenProjectDirectoryIsNull() var action = () => new PythonProjectResource(name, executablePath, projectDirectory); var exception = Assert.Throws(action); - Assert.Equal("workingDirectory", exception.ParamName); + Assert.Equal(nameof(projectDirectory), exception.ParamName); } - #endregion - - #region PythonProjectResourceBuilderExtensions - [Fact] public void AddPythonProjectShouldThrowWhenBuilderIsNull() { @@ -74,7 +69,7 @@ public void AddPythonProjectShouldThrowWhenBuilderIsNull() [Fact] public void AddPythonProjectShouldThrowWhenNameIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); string name = null!; const string projectDirectory = "/src/python"; const string scriptPath = "scripts"; @@ -93,7 +88,7 @@ public void AddPythonProjectShouldThrowWhenNameIsNull() [Fact] public void AddPythonProjectShouldThrowWhenProjectDirectoryIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; string projectDirectory = null!; const string scriptPath = "scripts"; @@ -112,7 +107,7 @@ public void AddPythonProjectShouldThrowWhenProjectDirectoryIsNull() [Fact] public void AddPythonProjectShouldThrowWhenScriptPathIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; const string projectDirectory = "/src/python"; string scriptPath = null!; @@ -131,7 +126,7 @@ public void AddPythonProjectShouldThrowWhenScriptPathIsNull() [Fact] public void AddPythonProjectShouldThrowWhenScriptArgsIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; const string projectDirectory = "/src/python"; const string scriptPath = "scripts"; @@ -171,7 +166,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenBuilderIsNu [Fact] public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenNameIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); string name = null!; const string projectDirectory = "/src/python"; const string scriptPath = "scripts"; @@ -192,7 +187,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenNameIsNull( [Fact] public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenProjectDirectoryIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; string projectDirectory = null!; const string scriptPath = "scripts"; @@ -213,7 +208,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenProjectDire [Fact] public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptPathIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; const string projectDirectory = "/src/python"; string scriptPath = null!; @@ -234,7 +229,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptPathI [Fact] public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenVirtualEnvironmentPathIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; const string projectDirectory = "/src/python"; const string scriptPath = "scripts"; @@ -255,7 +250,7 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenVirtualEnvi [Fact] public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptArgsIsNull() { - var builder = DistributedApplication.CreateBuilder(); + var builder = TestDistributedApplicationBuilder.Create(); const string name = "Python"; const string projectDirectory = "/src/python"; const string scriptPath = "scripts"; @@ -272,6 +267,4 @@ public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptArgsI var exception = Assert.Throws(action); Assert.Equal(nameof(scriptArgs), exception.ParamName); } - - #endregion } From 117972859d1ae3d473e563b5f515e5dcde3098ba Mon Sep 17 00:00:00 2001 From: ZLoo Date: Thu, 1 Aug 2024 23:05:37 +0300 Subject: [PATCH 5/5] To expression body --- src/Aspire.Hosting.Python/PythonProjectResource.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Aspire.Hosting.Python/PythonProjectResource.cs b/src/Aspire.Hosting.Python/PythonProjectResource.cs index 267876dde39..0b00879b374 100644 --- a/src/Aspire.Hosting.Python/PythonProjectResource.cs +++ b/src/Aspire.Hosting.Python/PythonProjectResource.cs @@ -17,12 +17,5 @@ public class PythonProjectResource(string name, string executablePath, string pr : ExecutableResource(ThrowIfNull(name), ThrowIfNull(executablePath), ThrowIfNull(projectDirectory)), IResourceWithServiceDiscovery { private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - if (argument is null) - { - throw new ArgumentNullException(paramName); - } - - return argument; - } + => argument ?? throw new ArgumentNullException(paramName); }