Skip to content

Commit 807c5ae

Browse files
frank-emrichmeta-codesync[bot]
authored andcommitted
distance 0 selection
Summary: This diff adds special handling for the case where a symbol passed to `hh --find-my-tests` is inside a file that contains test classes. In this case, we immediately select it (with distance 0). Technical details: Given that `hh --find-my-tests` is just doing a BFS traversal of the call graph at the moment, one may expect that we would handle this case (i.e, distance 0) uniformly with how we already select test files during the traversal. However, there's a bit of a difference in terms of when we find out which file a symbol lives in for the roots vs. the symbols we find on the way. Because of that, the logic for distance 0 is implemented as a special case. Reviewed By: mheiber Differential Revision: D83848013 fbshipit-source-id: 241ca89160d2ace03710b6442c455f41106954a0
1 parent 19967cc commit 807c5ae

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

hphp/hack/src/server/serverFindMyTests.ml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ let get_references
232232
in
233233
List.fold_result search_results ~init:([], []) ~f:resolve_found_position
234234

235+
let is_root_symbol_in_test_file ctx = function
236+
| Method { class_name; method_name = _ } ->
237+
(match Naming_provider.get_class_path ctx (Utils.add_ns class_name) with
238+
| None ->
239+
failwith
240+
(Printf.sprintf
241+
"Could not resolve class %s, even though it was given as part of a root"
242+
class_name)
243+
| Some relative_path ->
244+
let nast = Ast_provider.get_ast ~full:true ctx relative_path in
245+
let path = Relative_path.suffix relative_path in
246+
if is_test_file ctx path nast then
247+
Some path
248+
else
249+
None)
250+
235251
let search
236252
~(ctx : Provider_context.t)
237253
~(genv : ServerEnv.genv)
@@ -245,10 +261,12 @@ let search
245261

246262
List.iter roots ~f:(fun root_symbol ->
247263
let full_name = full_name_of_symbol_def root_symbol in
248-
match Hash_set.strict_add seen_symbols full_name with
249-
| Result.Ok () ->
250-
Queue.enqueue queue { distance = 0; symbol_def = root_symbol }
251-
| _ -> ());
264+
let seen = Result.is_error (Hash_set.strict_add seen_symbols full_name) in
265+
match is_root_symbol_in_test_file ctx root_symbol with
266+
| Some test_file -> ignore (Hashtbl.add test_files ~key:test_file ~data:0)
267+
| None ->
268+
if not seen then
269+
Queue.enqueue queue { distance = 0; symbol_def = root_symbol });
252270

253271
let rec bfs () =
254272
match Queue.dequeue queue with

hphp/hack/test/integration/test_find_my_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def check_has_tests_with_distances(
3535

3636
(output, err, retcode) = self.test_driver.run_check(
3737
options=[
38+
"--autostart-server=false",
3839
"--json",
3940
"--find-my-tests-max-distance",
4041
str(max_distance),
@@ -119,6 +120,17 @@ def test_overrides(self) -> None:
119120
],
120121
)
121122

123+
def test_distance_zero(self) -> None:
124+
"""Tests that if we ask about a test file itself, it is returned with distance 0
125+
126+
Uses files from the a/ subdirectory (all of which have prefix A_)
127+
"""
128+
129+
self.check_has_tests_with_distances(
130+
symbols=["A_SubTest::target"],
131+
expected_test_files=[("A_SubTest.php", 0)],
132+
)
133+
122134
def test_indirection(self) -> None:
123135
"""Tests that if we set --find-my-tests-max-distance > 1, we get tests that don't call the symbol directly
124136

0 commit comments

Comments
 (0)