Skip to content

UseProjectRelative + SourceFileRelativeDirectory #1758

Description

@snebjorn

Is the feature request related to a problem

ProjectRelative snapshots folder quickly becomes unwieldy.

Describe the solution

I think it would be easier to reason about what snapshots are orphaned if files inside the ProjectRelativeDirectory would be nested in folders that mirrored the path of the source.

example:

Root
├── Tests
│   └── Foo
│       └── FooTests.cs
└── Snapshots
    └── Foo
        └── FooTests.Bar.verified.json

Additional context

This accomplishes the above. But would be nice with built-in support 😄

/// <summary>
/// Configures Verify to store snapshots under a root directory while mirroring
/// the relative folder structure of the test source files.
/// </summary>
/// <param name="snapshotsDirectoryName">
/// The project-relative root directory where snapshots are written.
/// </param>
/// <param name="testsDirectoryName">
/// The project-relative root directory used to calculate each test file's relative path.
/// </param>
private static void UseMirroredSnapshotDirectories(
    string snapshotsDirectoryName,
    string testsDirectoryName
) =>
    Verifier.DerivePathInfo(
        (sourceFile, projectDirectory, type, method) =>
        {
            var testsDirectory = Path.Combine(projectDirectory, testsDirectoryName);
            var sourceDirectory = Path.GetDirectoryName(sourceFile)!;
            var relativeDirectory = Path.GetRelativePath(testsDirectory, sourceDirectory);

            var snapshotDirectory =
                relativeDirectory.StartsWith("..", StringComparison.Ordinal)
                    ? Path.Combine(projectDirectory, snapshotsDirectoryName)
                : relativeDirectory == "."
                    ? Path.Combine(projectDirectory, snapshotsDirectoryName)
                : Path.Combine(projectDirectory, snapshotsDirectoryName, relativeDirectory);

            return new(
                directory: snapshotDirectory,
                typeName: type.Name,
                methodName: method.Name
            );
        }
    );

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions