From b8cd4ffb935d9f60e3b1da8c28f60c9e093b40ae Mon Sep 17 00:00:00 2001 From: Matt Blagden Date: Tue, 12 Mar 2024 10:32:54 -0700 Subject: [PATCH] Fix getLoadedScripts test Summary: The `getLoadedScripts` test was invoking the observer that was installed in the `DebuggerAPITest` setup, and inspecting program state at an inopportune time. Make the test construct its own runtime instead. Reviewed By: ftanuma Differential Revision: D54800717 fbshipit-source-id: c2a438e3744601857792e770c63523cc3b914ee4 --- unittests/API/DebuggerTest.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/unittests/API/DebuggerTest.cpp b/unittests/API/DebuggerTest.cpp index 102ad357636..044f4523c32 100644 --- a/unittests/API/DebuggerTest.cpp +++ b/unittests/API/DebuggerTest.cpp @@ -44,12 +44,9 @@ struct DebuggerAPITest : public ::testing::Test { TestEventObserver observer; DebuggerAPITest() - : rt(makeHermesRuntime( - ((hermes::vm::RuntimeConfig::Builder()) - .withEnableBlockScoping(true) - .withCompilationMode( - hermes::vm::CompilationMode::ForceLazyCompilation) - .build()))) { + : rt(makeHermesRuntime(((hermes::vm::RuntimeConfig::Builder()) + .withEnableBlockScoping(true) + .build()))) { rt->getDebugger().setEventObserver(&observer); } }; @@ -135,11 +132,21 @@ TEST_F(DebuggerAPITest, SingleFrameStackTraceTest) { } TEST_F(DebuggerAPITest, GetLoadedScriptsTest) { - auto scripts = rt->getDebugger().getLoadedScripts(); + // Don't use the member runtime, as we don't want to + // trigger the observer on script loads. + std::unique_ptr runtime = makeHermesRuntime( + ((hermes::vm::RuntimeConfig::Builder()) + .withCompilationMode( + hermes::vm::CompilationMode::ForceLazyCompilation) + .build())); + + auto scripts = runtime->getDebugger().getLoadedScripts(); EXPECT_EQ(scripts.size(), 0); - eval("var x = 1;"); - scripts = rt->getDebugger().getLoadedScripts(); + runtime->global() + .getPropertyAsFunction(*runtime, "eval") + .call(*runtime, "var x = 1;"); + scripts = runtime->getDebugger().getLoadedScripts(); EXPECT_EQ(scripts.size(), 1); EXPECT_EQ(scripts[0].line, 1); EXPECT_EQ(scripts[0].column, 1); @@ -151,8 +158,8 @@ TEST_F(DebuggerAPITest, GetLoadedScriptsTest) { // compilation in the test setup) to cause multiple runtime modules for this // single script, allowing this test to verify we don't get duplicate // results. - rt->debugJavaScript("(function(){var x = 2;})()", "Test.js", {}); - scripts = rt->getDebugger().getLoadedScripts(); + runtime->debugJavaScript("(function(){var x = 2;})()", "Test.js", {}); + scripts = runtime->getDebugger().getLoadedScripts(); EXPECT_EQ(scripts.size(), 2); for (auto script : scripts) { if (script.fileName == "JavaScript") {