Issafalcon / neotest-dotnet

Neotest adapter for dotnet
MIT License
63 stars 20 forks source link

fix(custom-attributes): do not ignore the result of `tbl_flatten` #84

Closed massix closed 5 months ago

massix commented 5 months ago

Hello, first of all thanks for this plugin. I recently started developing in C# on Linux and this has been a lifesaver. I'm currently using xUnit as the testing framework and I wanted to create a custom Attribute in order to ignore some tests depending on the value of some environment variables, so I created the following class, which basically sets the Skip property to the value of a given Environment Variable.

using Mozart.Lib;

namespace Mozart.Test.Tooling;

public sealed class SkippableEnvironmentFactAttribute : FactAttribute
{
    public SkippableEnvironmentFactAttribute() : base() { }

    public SkippableEnvironmentFactAttribute(string environmentVariable)
    {
        OptionalEnvironmentVariable.IfSet(environmentVariable, val =>
        {
            Skip = val;
        });
    }
}

And this is the way I'm using it in code:

[CollectionDefinition(nameof(CosmosContainer), DisableParallelization = true)]
public class CosmosConnectorTest(CosmosContainer CosmosContainer) : IClassFixture<CosmosContainer>
{
    [SkippableEnvironmentFact("MOZART_IGNORE_COSMOS_TESTS", DisplayName = "Container starts and is ok")]
    public async void CanConnectToCosmos()
    {
        ConnectionInfo connectionInfo = await CosmosContainer.StartContainerOrGetConnectionInfo();
        Assert.Equal("127.0.0.1", connectionInfo.Host);
    }
}

Now, this works very well when running dotnet test and the behaviour is correct. The problem is that the neotest-dotnet adapter was not retrieving the tests, despite my current configuration:

          require("neotest-dotnet")({
            custom_attributes = {
              xunit = { "SkippableEnvironmentFact" },
            },
            discovery_root = "solution",
          }),

(Of course this is inside the neotest.adapters table).

I noticed though, that if I added even a single test using one of the standard attributes to my test class (for example [Fact]) then all the other tests were detected correctly, but as soon as I removed the [Fact], then neotest was no longer refreshing the file.

Hence, I started investigating and I found the line that I modified in my Pull Request, which apparently solves the issue I was having.

I've been using Neovim for quite some time now and I'm aware that sometimes the underlying APIs are modified, and I think (but I am not sure!) that the vim.tbl_flatten function got modified, now instead of modifying the table in-place, it returns a copy of it?

So I opened this PR, test it out and let me know if there's something more I can do!

Issafalcon commented 5 months ago

Hi @massix ! So glad you find the plugin useful!

Thanks a lot for spotting this and raising a PR. I've just updated it to fix formatting and added a unit test to cover off this use case, which confirms your fix works.

Happy to merge this in!

massix commented 5 months ago

Thanks a lot for your reactivity! 👐