Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions OptionsBindingsGenerator.UnitTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@
implementationAssembly = GetAssembly("OptionsBindingsGenerator");
}

[Test]
public async Task NoBindings()
{
var source = await ReadCSharpFile<NoBindingsOptions>(true);

await new VerifyCS.Test
{
CompilerDiagnostics = CompilerDiagnostics.None,
TestState = {
ReferenceAssemblies = ReferenceAssemblies.Net.Net90,
AdditionalReferences =
{
implementationAssembly,
GetAssembly("TestLibrary")
},

Sources = { source },
GeneratedSources =
{

},
},
}.RunAsync();
}

[Test]
public async Task MixedBindings()
{
Expand Down Expand Up @@ -66,9 +91,9 @@
{
var currentDirectory = GetCurrentDirectory();

var targetDirectory = isTestLibrary ? GetTestLibraryDirectory(currentDirectory) : currentDirectory;

Check warning on line 94 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Possible null reference argument for parameter 'currentDirectory' in 'DirectoryInfo Tests.GetTestLibraryDirectory(DirectoryInfo currentDirectory)'.

Check warning on line 94 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Possible null reference argument for parameter 'currentDirectory' in 'DirectoryInfo Tests.GetTestLibraryDirectory(DirectoryInfo currentDirectory)'.

var file = targetDirectory.GetFiles(searchPattern).First();

Check warning on line 96 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 96 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

using var fileReader = new StreamReader(file.OpenRead());
return await fileReader.ReadToEndAsync();
Expand All @@ -76,10 +101,10 @@

private static DirectoryInfo? GetCurrentDirectory()
{
return Directory.GetParent(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName);

Check warning on line 104 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 104 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 104 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 104 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 104 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.
}

private static DirectoryInfo GetTestLibraryDirectory(DirectoryInfo currentDirectory)
{
return currentDirectory.Parent.GetDirectories("TestLibrary").First();

Check warning on line 109 in OptionsBindingsGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.
}
Expand Down
8 changes: 6 additions & 2 deletions OptionsBindingsGenerator/Main.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -36,9 +37,12 @@ private static INamedTypeSymbol GetSemanticTargetForGeneration(GeneratorAttribut

private static void Execute(SourceProductionContext context, ImmutableArray<INamedTypeSymbol> typeSymbols)
{
var (source, className) = OutputGenerator.GenerateOutput(typeSymbols);
if (typeSymbols.Any())
{
var (source, className) = OutputGenerator.GenerateOutput(typeSymbols);

context.AddSource($"{className}.generated.cs", SourceText.From(source, Encoding.UTF8, SourceHashAlgorithm.Sha256));
context.AddSource($"{className}.generated.cs", SourceText.From(source, Encoding.UTF8, SourceHashAlgorithm.Sha256));
}
}
}
}
11 changes: 11 additions & 0 deletions TestLibrary/NoBindingsOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace TestLibrary;

internal record NoBindingsOptions
{
[Required]
public required string ServiceHost { get; init; }

public required string Port { get; init; }
}