Skip to content
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

Adding public API test coverage for Aspire.Hosting.Python #5110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c67cdf3
CA1062#Aspire.Hosting.Python
Zombach Jul 29, 2024
ad059e5
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Jul 29, 2024
f3bd0d3
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Jul 29, 2024
ec25311
Fix PR by feedback
Zombach Jul 30, 2024
96efb2e
Merge branch 'zombach/validate-arguments-of-public-methods#aspire-hos…
Zombach Jul 30, 2024
90a67ac
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Jul 30, 2024
386bd9c
Remove Assert.Multiple by feedback
Zombach Jul 30, 2024
baf09c4
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Jul 31, 2024
d038841
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Jul 31, 2024
0ad4c44
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 1, 2024
fc8f9b2
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 1, 2024
f75eb99
builder replace to TestDistributedApplicationBuilder
Zombach Aug 1, 2024
1179728
To expression body
Zombach Aug 1, 2024
1961d80
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 2, 2024
a21b603
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 4, 2024
9fd8ab6
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 5, 2024
4466e5d
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 6, 2024
a54821a
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 8, 2024
be2c5c8
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 14, 2024
3616f3a
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 17, 2024
d2cd1b4
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 20, 2024
2420a46
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 20, 2024
0fa62c5
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 24, 2024
ae319dc
Merge branch 'dotnet:main' into zombach/validate-arguments-of-public-…
Zombach Aug 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Aspire.Hosting.Python/PythonProjectResource.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +14,8 @@ namespace Aspire.Hosting.Python;
/// <param name="executablePath">The path to the executable used to run the python project.</param>
/// <param name="projectDirectory">The path to the directory containing the python project.</param>
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)
=> argument ?? throw new ArgumentNullException(paramName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static class PythonProjectResourceBuilderExtensions
public static IResourceBuilder<PythonProjectResource> AddPythonProject(
this IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, params string[] scriptArgs)
{
ArgumentNullException.ThrowIfNull(builder);
radical marked this conversation as resolved.
Show resolved Hide resolved

return builder.AddPythonProject(name, projectDirectory, scriptPath, ".venv", scriptArgs);
}

Expand Down Expand Up @@ -108,7 +110,12 @@ public static IResourceBuilder<PythonProjectResource> 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)
Expand Down
270 changes: 270 additions & 0 deletions tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
// 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
{
[Fact]
public void CtorPythonProjectResourceShouldThrowWhenNameIsNull()
{
string name = null!;
const string executablePath = "/src/python";
const string projectDirectory = "/data/python";

var action = () => new PythonProjectResource(name, executablePath, projectDirectory);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNull()
{
const string name = "Python";
string executablePath = null!;
const string projectDirectory = "/data/python";

var action = () => new PythonProjectResource(name, executablePath, projectDirectory);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(executablePath), exception.ParamName);
}

[Fact]
public void CtorPythonProjectResourceShouldThrowWhenProjectDirectoryIsNull()
{
const string name = "Python";
const string executablePath = "/src/python";
string projectDirectory = null!;

var action = () => new PythonProjectResource(name, executablePath, projectDirectory);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(projectDirectory), exception.ParamName);
}

[Fact]
public void AddPythonProjectShouldThrowWhenBuilderIsNull()
{
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddPythonProjectShouldThrowWhenNameIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
string name = null!;
const string projectDirectory = "/src/python";
const string scriptPath = "scripts";
string[] scriptArgs = ["--traces"];

var action = () => builder.AddPythonProject(
name,
projectDirectory,
scriptPath,
scriptArgs);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void AddPythonProjectShouldThrowWhenProjectDirectoryIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
const string name = "Python";
string projectDirectory = null!;
const string scriptPath = "scripts";
string[] scriptArgs = ["--traces"];

var action = () => builder.AddPythonProject(
name,
projectDirectory,
scriptPath,
scriptArgs);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(projectDirectory), exception.ParamName);
}

[Fact]
public void AddPythonProjectShouldThrowWhenScriptPathIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
const string name = "Python";
const string projectDirectory = "/src/python";
string scriptPath = null!;
string[] scriptArgs = ["--traces"];

var action = () => builder.AddPythonProject(
name,
projectDirectory,
scriptPath,
scriptArgs);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(scriptPath), exception.ParamName);
}

[Fact]
public void AddPythonProjectShouldThrowWhenScriptArgsIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
const string name = "Python";
const string projectDirectory = "/src/python";
const string scriptPath = "scripts";
string[] scriptArgs = null!;

var action = () => builder.AddPythonProject(
name,
projectDirectory,
scriptPath,
scriptArgs);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(scriptArgs), exception.ParamName);
}

[Fact]
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenBuilderIsNull()
{
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenNameIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenProjectDirectoryIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(projectDirectory), exception.ParamName);
}

[Fact]
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptPathIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(scriptPath), exception.ParamName);
}

[Fact]
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenVirtualEnvironmentPathIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(virtualEnvironmentPath), exception.ParamName);
}

[Fact]
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptArgsIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(scriptArgs), exception.ParamName);
}
}