Skip to content

TUNU0001 code fixer leaves orphaned #endif when #if directive is removed #4488

@thomhurst

Description

@thomhurst

Description

The TUNU0001 NUnit migration code fixer sometimes removes #if preprocessor directives but leaves the corresponding #endif orphaned, causing CS1028 syntax errors.

Reproduction Steps

  1. Create a test file with conditional compilation:
using NUnit.Framework;

#if FEATURE_ASSEMBLYBUILDER_SAVE
[TestFixture]
public class PersistentProxyBuilderTestCase
{
    [SetUp]
    public void SetupProxyBuilder()
    {
        // setup code
    }

    [Test]
    public void SavesOneAssemblyPerProxiedType()
    {
        Assert.AreEqual(1, 1);
    }
}
#endif
  1. Run the TUNU0001 code fixer

Expected Behavior

Either:

  • Both #if and #endif should be preserved
  • Both #if and #endif should be removed

Actual Behavior

The #if directive is removed but #endif remains:

public class PersistentProxyBuilderTestCase
{
    [Before(HookType.Test)]
    public void SetupProxyBuilder()
    {
        // setup code
    }

    [Test]
    public async Task SavesOneAssemblyPerProxiedType()
    {
        await Assert.That(1).IsEqualTo(1);
    }
}
#endif

This causes:

error CS1028: Unexpected preprocessor directive

Environment

  • TUnit version: 1.11.64
  • .NET SDK: 10.0.102

Tests should be added to verify the code fixer correctly handles #if/#endif pairs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions