diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs index 84e319352d9e..6f438a7fbbf0 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs @@ -56,10 +56,11 @@ public static Test MakeVerifier(ICollection sources, ICollection { var verifier = new Test(); - verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $""" - is_global = true - build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath} - """)); + verifier.TestState.AddGlobalConfig(new Dictionary() + { + { "is_global", "true" }, + { "build_property.GodotProjectDir", Constants.ExecutingAssemblyPath } + }); verifier.TestState.Sources.AddRange(sources.Select(source => ( source, diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs index 783b1e429872..d503c084be6c 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs @@ -5,6 +5,8 @@ namespace Godot.SourceGenerators.Tests; public static class Constants { + public const string GlobalConfigPath = "/.globalconfig"; + public static Assembly GodotSharpAssembly => typeof(GodotObject).Assembly; public static string ExecutingAssemblyPath { get; } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Extensions.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Extensions.cs index 6ff890e5d8a5..69c6497cc70c 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Extensions.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Extensions.cs @@ -1,5 +1,9 @@ +using System.Collections.Generic; using System.Reflection; +using System.Text; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeAnalysis.Text; namespace Godot.SourceGenerators.Tests; @@ -9,4 +13,24 @@ public static MetadataReference CreateMetadataReference(this Assembly assembly) { return MetadataReference.CreateFromFile(assembly.Location); } + + public static void AddGlobalConfig(this SolutionState state, Dictionary config) + { + var rawContent = new StringBuilder(); + var index = state.AnalyzerConfigFiles.FindIndex(((string Name, SourceText) file) => file.Name == Constants.GlobalConfigPath); + if (index >= 0) + { + rawContent.AppendLine(state.AnalyzerConfigFiles[index].content.ToString()); + state.AnalyzerConfigFiles.RemoveAt(index); + } + + foreach (var (key, value) in config) + { + rawContent.Append(key); + rawContent.Append(" = "); + rawContent.AppendLine(value); + } + + state.AnalyzerConfigFiles.Add((Constants.GlobalConfigPath, rawContent.ToString())); + } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptMethodsGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptMethodsGeneratorTests.cs index 294932eb9a38..f5aa4127fee8 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptMethodsGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptMethodsGeneratorTests.cs @@ -1,9 +1,21 @@ +using System; using Xunit; namespace Godot.SourceGenerators.Tests; public class ScriptMethodsGeneratorTests { + [Fact] + public async void DisableGenerator() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "ScriptBoilerplate.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptMethods")); + await verifier.RunAsync(); + } + [Fact] public async void Methods() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPathAttributeGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPathAttributeGeneratorTests.cs index 4f6b50cf0285..2a57b296822a 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPathAttributeGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPathAttributeGeneratorTests.cs @@ -21,6 +21,29 @@ private static (string, SourceText) MakeAssemblyScriptTypesGeneratedSource(IColl ); } + [Fact] + private async void DisableGenerator() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "ScriptBoilerplate.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptPathAttribute")); + await verifier.RunAsync(); + } + + // TODO: Remove the skip when the upstream issue is resolved, https://github.com/dotnet/roslyn/pull/51625 + [Fact(Skip = "This is currently failing due to an upstream issue with ';' in the editorconfig file format.")] + private async void DisableGeneratorButNotFirst() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "ScriptBoilerplate.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("SomePlaceholder", "ScriptPathAttribute")); + await verifier.RunAsync(); + } + [Fact] public async void ScriptBoilerplate() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs index 724fb164e022..fdf4ac1427c9 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs @@ -1,9 +1,21 @@ +using System; using Xunit; namespace Godot.SourceGenerators.Tests; public class ScriptPropertiesGeneratorTests { + [Fact] + public async void DisableGenerator() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "ScriptBoilerplate.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptProperties")); + await verifier.RunAsync(); + } + [Fact] public async void ExportedFields() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertyDefValGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertyDefValGeneratorTests.cs index 7711bce1c792..0c26a24d148e 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertyDefValGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertyDefValGeneratorTests.cs @@ -1,9 +1,21 @@ +using System; using Xunit; namespace Godot.SourceGenerators.Tests; public class ScriptPropertyDefValGeneratorTests { + [Fact] + public async void DisableGenerator() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "ExportedFields.cs", "ExportedProperties.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptPropertyDefVal")); + await verifier.RunAsync(); + } + [Fact] public async void ExportedFields() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSerializationGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSerializationGeneratorTests.cs index 24cd446087c2..44c0b6316439 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSerializationGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSerializationGeneratorTests.cs @@ -1,9 +1,21 @@ +using System; using Xunit; namespace Godot.SourceGenerators.Tests; public class ScriptSerializationGeneratorTests { + [Fact] + public async void DisableGenerator() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "ScriptBoilerplate.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptSerialization")); + await verifier.RunAsync(); + } + [Fact] public async void ScriptBoilerplate() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSignalsGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSignalsGeneratorTests.cs index 2a783cedcedd..b10f4b77aa36 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSignalsGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptSignalsGeneratorTests.cs @@ -1,9 +1,21 @@ +using System; using Xunit; namespace Godot.SourceGenerators.Tests; public class ScriptSignalsGeneratorTests { + [Fact] + public async void DisableGenerator() + { + var verifier = CSharpSourceGeneratorVerifier.MakeVerifier( + new string[] { "SimpleEventSignals.cs" }, + Array.Empty() + ); + verifier.TestState.AddGlobalConfig(Utils.DisabledGenerators("ScriptSignals")); + await verifier.RunAsync(); + } + [Fact] public async void EventSignals() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/SimpleEventSignals.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/SimpleEventSignals.cs new file mode 100644 index 000000000000..160c5d193de6 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/SimpleEventSignals.cs @@ -0,0 +1,7 @@ +using Godot; + +public partial class EventSignals : GodotObject +{ + [Signal] + public delegate void MySignalEventHandler(string str, int num); +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Utils.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Utils.cs new file mode 100644 index 000000000000..c16d91f08522 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Utils.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Godot.SourceGenerators.Tests; + +public static class Utils +{ + public static Dictionary DisabledGenerators(params string[] generators) + { + return new Dictionary() + { + { "build_property.GodotDisabledSourceGenerators", string.Join(";", generators) } + }; + } +}