Skip to content

Commit 6aaed0a

Browse files
shannonzhufacebook-github-bot
authored andcommitted
Print toplevel unbound names if sources unequal but statements identical
Summary: A huge pain point for me when trying to debug a diff was running into tests that compared sources, where the two sources were ostensibly identical based on the test output and the printed diff between expected and actual was empty. It turned out that sometimes the top level unbound names were different in the source, sometimes there was some statement node nested deep into the source that had some metadata that was different, etc. It would have saved me a lot of time and print debugging if the source equality testing function could tell when it was not going to print any helpful output around why this test was failing, and adjust to print something useful instead. Decided to implement this and land it - for now, if the sources are not equal even without counting the statements in the body, print the pretty json and compare that. Otherwise, just do the normal printing. Can be modified to maybe do pretty printing more aggressively if trying to debug in the future and something still seems to show no difference. Reviewed By: grievejia Differential Revision: D30213681 fbshipit-source-id: 6a9447f0301858c121e8a2ee98a7d3cc15144586
1 parent e40e8c2 commit 6aaed0a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

source/ast/source.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ let pp format { statements; _ } =
204204
List.iter statements ~f:print_statement
205205

206206

207+
let pp_all format source = Sexp.pp_hum format (sexp_of_t source)
208+
207209
let location_insensitive_compare left right =
208210
match Metadata.compare left.metadata right.metadata with
209211
| x when x <> 0 -> x

source/ast/source.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ val create
6565
Statement.t list ->
6666
t
6767

68+
val pp_all : Format.formatter -> t -> unit
69+
6870
val location_insensitive_compare : t -> t -> int
6971

7072
val mode : configuration:Configuration.Analysis.t -> local_mode:local_mode Node.t option -> mode

source/test/test.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,16 @@ let assert_source_equal ?(location_insensitive = false) left right =
249249
else
250250
Source.equal
251251
in
252+
let print_difference format (left, right) =
253+
if Source.equal { left with Source.statements = [] } { right with Source.statements = [] } then
254+
diff ~print:Source.pp format (left, right)
255+
else
256+
diff ~print:Source.pp_all format (left, right)
257+
in
252258
assert_equal
253259
~cmp
254260
~printer:(fun source -> Format.asprintf "%a" Source.pp source)
255-
~pp_diff:(diff ~print:Source.pp)
261+
~pp_diff:print_difference
256262
left
257263
right
258264

0 commit comments

Comments
 (0)