@@ -41,42 +41,53 @@ public void Snapshot(string inputDirectory)
41
41
{
42
42
Assert . Multiple ( ( ) =>
43
43
{
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 )
46
48
{
47
- if ( ! file . EndsWith ( ".cs" ) )
49
+ if ( ! absolutePath . EndsWith ( ".cs" ) )
48
50
{
49
51
continue ;
50
52
}
51
53
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 ) ;
55
57
var diff = DiffStrings ( obtained , expected ) ;
56
58
if ( diff . Length > 0 )
57
59
{
60
+ // Console.WriteLine(diff);
58
61
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 ) ;
61
64
}
62
65
}
63
66
64
- var filesSet = new HashSet < string > ( files ) ;
67
+ var absoluteOutputPathsSet = new HashSet < string > ( absoluteOutputPaths ) ;
65
68
foreach ( var ( relativePath , _) in snapshots )
66
69
{
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 ) )
69
72
{
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") ;
71
75
}
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") ;
75
76
}
76
77
} ) ;
77
78
}
78
79
}
79
80
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
+
80
91
private static string DiffStrings ( string obtained , string expected )
81
92
{
82
93
var diff = InlineDiffBuilder . Diff ( expected , obtained ) ;
@@ -152,12 +163,8 @@ private static string[] GitLsFiles(string directory)
152
163
{
153
164
StartInfo = new ProcessStartInfo ( )
154
165
{
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
166
FileName = "git" ,
160
- Arguments = "rev-parse --show-toplevel " ,
167
+ Arguments = "ls-files " ,
161
168
UseShellExecute = false ,
162
169
RedirectStandardOutput = true ,
163
170
WorkingDirectory = directory
0 commit comments