Skip to content

Commit 858da4d

Browse files
committed
Fix snapshot testing path logic
1 parent 302f8ca commit 858da4d

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

ScipDotnet.Tests/SnapshotTests.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,53 @@ public void Snapshot(string inputDirectory)
4141
{
4242
Assert.Multiple(() =>
4343
{
44-
var files = GitLsFiles(outputDirectory);
45-
foreach (var file in files)
44+
var relativeInputPaths = new HashSet<string>(GitLsFiles(inputDirectory));
45+
var absoluteOutputPaths = new List<string>();
46+
RecursivelyListFiles(outputDirectory, absoluteOutputPaths);
47+
foreach (var absolutePath in absoluteOutputPaths)
4648
{
47-
if (!file.EndsWith(".cs"))
49+
if (!absolutePath.EndsWith(".cs"))
4850
{
4951
continue;
5052
}
5153

52-
var relativePath = Path.GetRelativePath(outputDirectory, file);
53-
var obtained = snapshots.GetValueOrDefault(relativePath, "");
54-
var expected = File.ReadAllText(file);
54+
var relativePath = Path.GetRelativePath(outputDirectory, absolutePath);
55+
var obtained = snapshots[relativePath];
56+
var expected = File.ReadAllText(absolutePath);
5557
var diff = DiffStrings(obtained, expected);
5658
if (diff.Length > 0)
5759
{
60+
// Console.WriteLine(diff);
5861
Assert.Fail(
59-
"(+ expected, - obtained). To update the expected output to match the obtained behavior, run: " +
60-
"SCIP_UPDATE_SNAPSHOTS=true dotnet test\n\n" + diff, file);
62+
$"{absolutePath}\n(+ expected, - obtained). To update the expected output to match the obtained behavior, run: " +
63+
"SCIP_UPDATE_SNAPSHOTS=true dotnet test\n\n" + diff);
6164
}
6265
}
6366

64-
var filesSet = new HashSet<string>(files);
67+
var absoluteOutputPathsSet = new HashSet<string>(absoluteOutputPaths);
6568
foreach (var (relativePath, _) in snapshots)
6669
{
67-
var outputPath = Path.Join(outputDirectory, relativePath);
68-
if (filesSet.Contains(outputPath))
70+
var absolutePath = Path.Join(outputDirectory, relativePath);
71+
if (relativeInputPaths.Contains(relativePath) && !absoluteOutputPathsSet.Contains(absolutePath))
6972
{
70-
continue;
73+
Assert.Fail(
74+
$"relative path '{relativePath}' missing an output file. To fix this problem, run the following command: SCIP_UPDATE_SNAPSHOTS=true dotnet test");
7175
}
72-
73-
Assert.Fail(
74-
$"relative path '{relativePath}' missing an output file. To fix this problem, run the following command: SCIP_UPDATE_SNAPSHOTS=true dotnet test");
7576
}
7677
});
7778
}
7879
}
7980

81+
private static void RecursivelyListFiles(string path, List<string> result)
82+
{
83+
if (!Directory.Exists(path)) return;
84+
result.AddRange(Directory.GetFiles(path));
85+
foreach (var directory in Directory.GetDirectories(path))
86+
{
87+
RecursivelyListFiles(directory, result);
88+
}
89+
}
90+
8091
private static string DiffStrings(string obtained, string expected)
8192
{
8293
var diff = InlineDiffBuilder.Diff(expected, obtained);
@@ -152,12 +163,8 @@ private static string[] GitLsFiles(string directory)
152163
{
153164
StartInfo = new ProcessStartInfo()
154165
{
155-
// The working directory of `dotnet test` is not the root directory of the project
156-
// so we infer it by invoking `git rev-parse --show-toplevel`. It would be cleaner
157-
// to get the root directory from MSBuild but I wasn't able to figure out how to do it
158-
// after searching for ~20 minutes. This works for now and unblocks writing tests.
159166
FileName = "git",
160-
Arguments = "rev-parse --show-toplevel",
167+
Arguments = "ls-files",
161168
UseShellExecute = false,
162169
RedirectStandardOutput = true,
163170
WorkingDirectory = directory

0 commit comments

Comments
 (0)