From a8e15ef2e67bf164f4cf66f3cc271cb47f28bb8f Mon Sep 17 00:00:00 2001 From: st0012 Date: Wed, 30 Mar 2022 22:38:21 +0100 Subject: [PATCH 1/4] Add assert_threads_result helper and threads test --- test/protocol/threads_test.rb | 33 +++++++++++++++++++++++++++++++++ test/support/protocol_utils.rb | 16 ++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/protocol/threads_test.rb diff --git a/test/protocol/threads_test.rb b/test/protocol/threads_test.rb new file mode 100644 index 000000000..856cba3c9 --- /dev/null +++ b/test/protocol/threads_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative '../support/test_case' + +module DEBUGGER__ + class ThreadsTest < TestCase + PROGRAM = <<~RUBY + 1| def foo + 2| Thread.new { sleep 30 } + 3| end + 4| + 5| foo + 6| sleep 0.1 # make sure the thread stops + 7| binding.b + RUBY + + def test_reponse_returns_correct_threads_info + run_protocol_scenario PROGRAM do + req_continue + + assert_threads_result( + [ + /\.rb:\d:in `
'/, + /\.rb:\d:in `block in foo'/ + ] + ) + + req_terminate_debuggee + end + end + end +end + diff --git a/test/support/protocol_utils.rb b/test/support/protocol_utils.rb index ec0cb948c..95775579a 100644 --- a/test/support/protocol_utils.rb +++ b/test/support/protocol_utils.rb @@ -230,6 +230,22 @@ def assert_locals_result expected, frame_idx: 0 end end + def assert_threads_result(expected_names) + case ENV['RUBY_DEBUG_TEST_UI'] + when 'vscode' + res = send_dap_request 'threads' + + threads = res.dig(:body, :threads) + failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}"} + + assert_equal expected_names.count, threads.count, failure_msg + + expected_names.each_with_index do |expected, index| + assert_match expected, threads[index][:name], failure_msg + end + end + end + def assert_hover_result expected, expression: nil, frame_idx: 0 case ENV['RUBY_DEBUG_TEST_UI'] when 'vscode' From bde0a9137c3c36606d7a37d24febb82cad583f69 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 1 Apr 2022 17:00:36 +0800 Subject: [PATCH 2/4] Update test/protocol/threads_test.rb Co-authored-by: Naoto Ono --- test/protocol/threads_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/protocol/threads_test.rb b/test/protocol/threads_test.rb index 856cba3c9..32ba1bdc1 100644 --- a/test/protocol/threads_test.rb +++ b/test/protocol/threads_test.rb @@ -15,7 +15,7 @@ class ThreadsTest < TestCase RUBY def test_reponse_returns_correct_threads_info - run_protocol_scenario PROGRAM do + run_protocol_scenario PROGRAM, cdp: false do req_continue assert_threads_result( From 1119e376ca89287eafaafb3a84b27c37a237b21a Mon Sep 17 00:00:00 2001 From: st0012 Date: Sat, 2 Apr 2022 15:37:14 +0100 Subject: [PATCH 3/4] Update CONTRIBUTING.md --- CONTRIBUTING.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 26121b2db..d7d71bce0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -372,6 +372,23 @@ An variable entry looks like this: `{ name: "bar", value: "nil", type: "NilClass Please note that both `value` and `type` need to be strings. +- assert_threads_result(expected) + +Passes if both conditions are true: + +1. The number of expected pattern matches the number of threads. +2. The expected patterns match the thread names in the given order. + +Example: + +``` +assert_threads_result( + [ + /\.rb:\d:in `
'/, + /\.rb:\d:in `block in foo'/ + ] +) +``` ## To Update README From 8b9a85e76574f936b6f64c5538c111f08b0e3c61 Mon Sep 17 00:00:00 2001 From: st0012 Date: Mon, 11 Apr 2022 12:02:09 +0100 Subject: [PATCH 4/4] Make threads assertion order-agnostic --- CONTRIBUTING.md | 4 ++-- test/support/protocol_utils.rb | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d7d71bce0..b9fa7f1a3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -376,8 +376,8 @@ Please note that both `value` and `type` need to be strings. Passes if both conditions are true: -1. The number of expected pattern matches the number of threads. -2. The expected patterns match the thread names in the given order. +1. The number of expected patterns matches the number of threads. +2. Every pattern matches a thread name. Notice that the order of threads info is not guaranteed. Example: diff --git a/test/support/protocol_utils.rb b/test/support/protocol_utils.rb index 95775579a..54076a46e 100644 --- a/test/support/protocol_utils.rb +++ b/test/support/protocol_utils.rb @@ -234,15 +234,22 @@ def assert_threads_result(expected_names) case ENV['RUBY_DEBUG_TEST_UI'] when 'vscode' res = send_dap_request 'threads' + failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}."} threads = res.dig(:body, :threads) - failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}"} assert_equal expected_names.count, threads.count, failure_msg - expected_names.each_with_index do |expected, index| - assert_match expected, threads[index][:name], failure_msg + thread_names = threads.map { |t| t[:name] } + + expected_names.each do |expected| + thread_names.reject! do |name| + name.match?(expected) + end end + + failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}.\nExpect all thread names to be matched. Unmatched threads:"} + assert_equal [], thread_names, failure_msg end end