Skip to content

Commit 302f8ca

Browse files
committed
Use git ls-files instead of recursive directory walk
1 parent 113d6e0 commit 302f8ca

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

ScipDotnet.Tests/SnapshotTests.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,25 @@ public void Snapshot(string inputDirectory)
3939
}
4040
else
4141
{
42-
var files = new List<string>();
43-
RecursivelyListFiles(outputDirectory, files);
4442
Assert.Multiple(() =>
4543
{
44+
var files = GitLsFiles(outputDirectory);
4645
foreach (var file in files)
4746
{
47+
if (!file.EndsWith(".cs"))
48+
{
49+
continue;
50+
}
51+
4852
var relativePath = Path.GetRelativePath(outputDirectory, file);
4953
var obtained = snapshots.GetValueOrDefault(relativePath, "");
5054
var expected = File.ReadAllText(file);
5155
var diff = DiffStrings(obtained, expected);
5256
if (diff.Length > 0)
5357
{
54-
Assert.Fail("(+ expected, - obtained). To update the expected output to match the obtained behavior, run: " +
55-
"SCIP_UPDATE_SNAPSHOTS=true dotnet test\n\n" + diff, file);
58+
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);
5661
}
5762
}
5863

@@ -111,16 +116,6 @@ private static string[] ListSnapshotInputDirectories()
111116
return Directory.GetDirectories(inputs);
112117
}
113118

114-
private static void RecursivelyListFiles(string path, List<string> result)
115-
{
116-
if (!Directory.Exists(path)) return;
117-
result.AddRange(Directory.GetFiles(path));
118-
foreach (var directory in Directory.GetDirectories(path))
119-
{
120-
RecursivelyListFiles(directory, result);
121-
}
122-
}
123-
124119
private static string IndexDirectory(string directory)
125120
{
126121
var include = Environment.GetEnvironmentVariable("SCIP_INCLUDE");
@@ -151,6 +146,27 @@ private static string IndexDirectory(string directory)
151146
return Path.Join(directory, "index.scip");
152147
}
153148

149+
private static string[] GitLsFiles(string directory)
150+
{
151+
var process = new Process()
152+
{
153+
StartInfo = new ProcessStartInfo()
154+
{
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.
159+
FileName = "git",
160+
Arguments = "rev-parse --show-toplevel",
161+
UseShellExecute = false,
162+
RedirectStandardOutput = true,
163+
WorkingDirectory = directory
164+
}
165+
};
166+
process.Start();
167+
return process.StandardOutput.ReadToEnd().Trim().Split("\n");
168+
}
169+
154170
private static string RootDirectory()
155171
{
156172
var process = new Process()

0 commit comments

Comments
 (0)