Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving built test files (CI/CD) results in snapshots not being found #212

Open
mlstubblefield opened this issue Dec 27, 2024 · 0 comments
Open

Comments

@mlstubblefield
Copy link

Describe the bug
We build all of our files in one CI/CD job and then execute some (integration) tests later on a differing file structure.
Because the SnapshotFullName "FolderPath" uses the stack information (build time file location) and is absolute to the build time file system, the test fails since it can't locate the snapshot.

To Reproduce
Steps to reproduce the behavior:

  1. Make a snapshot test
  2. Build it and run the test to produce a snapshot
  3. Copy the entire project somewhere else and run off of the copied build (/bin/Release/whatever/something.dll)
  4. Test Fails

Expected behavior
The test should pass since the snapshot exists relative to the project still

Additional context
I have a workaround that our team uses that so far seems to be pretty effective... Essentially I modify the paths to absolute, but relative to the runtime. I imagine there's a "more correct" approach, but this was the best I could come up with quickly.

     public static class Snapshot
    {
        public static void Match(object snapshot, Func<MatchOptions, MatchOptions> matchOptions = null)
        {
            var snapshotFullName = Snapshooter.MSTest.Snapshot.FullName();
            var fileName = snapshotFullName.Filename;
            string localizedPathSplit = GetRuntimePath(snapshotFullName);
            var snapshotNameFull = new SnapshotFullName(fileName, localizedPathSplit);
            Snapshooter.MSTest.Snapshot.Match(snapshot, snapshotNameFull, matchOptions);
        }

        private static string GetRuntimePath(SnapshotFullName snapshotFullName)
        {
            string projectName = Assembly.GetEntryAssembly().GetName().Name;
            var pathSplit = snapshotFullName.FolderPath.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
            var projectPosition = Array.FindIndex(pathSplit, x => string.Equals(x, projectName, StringComparison.InvariantCultureIgnoreCase));
            var paths = pathSplit.Skip(projectPosition).ToArray();
            var localizedPathSplit = Path.Combine("../../../", Path.Combine(paths)); // chop off /bin/Release/net9.0
            return localizedPathSplit;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant