Bug 1772733 - Change directory in xpcshell without OS.File r=jmaher,nika

Instead of relying on OS.File.setCurrentDirectory in the xpcshell test harness
we instead provide a function to directly change directory.

Differential Revision: https://phabricator.services.mozilla.com/D148977
This commit is contained in:
Barret Rennie 2022-06-16 19:14:01 +00:00
Родитель 4036bec2bb
Коммит fa2f90fc19
2 изменённых файлов: 43 добавлений и 15 удалений

Просмотреть файл

@ -37,6 +37,7 @@
#include "nsDirectoryServiceUtils.h"
#include "nsCOMPtr.h"
#include "nsJSPrincipals.h"
#include "nsJSUtils.h"
#include "xpcpublic.h"
#include "xpcprivate.h"
#include "BackstagePass.h"
@ -638,6 +639,41 @@ static bool RegisterAppManifest(JSContext* cx, unsigned argc, Value* vp) {
return true;
}
static bool ChangeTestShellDir(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() != 1) {
JS_ReportErrorASCII(cx, "changeTestShellDir() takes one argument");
return false;
}
#ifdef XP_WIN
nsAutoJSString path;
#else
nsAutoJSCString path;
#endif
if (!path.init(cx, args[0])) {
JS_ReportErrorASCII(
cx, "changeTestShellDir(): could not convert argument 1 to string");
return false;
}
bool success;
#ifdef XP_WIN
success = !!SetCurrentDirectoryW(path.get());
#else
success = !chdir(path.get());
#endif
if (!success) {
JS_ReportErrorASCII(cx, "changeTestShellDir(): could not change directory");
return false;
}
return true;
}
#ifdef ENABLE_TESTS
static bool RegisterXPCTestComponents(JSContext* cx, unsigned argc, Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@ -673,6 +709,7 @@ static const JSFunctionSpec glob_functions[] = {
JS_FN("setInterruptCallback", SetInterruptCallback, 1,0),
JS_FN("simulateNoScriptActivity", SimulateNoScriptActivity, 1,0),
JS_FN("registerAppManifest", RegisterAppManifest, 1, 0),
JS_FN("changeTestShellDir", ChangeTestShellDir, 1,0),
#ifdef ENABLE_TESTS
JS_FN("registerXPCTestComponents", RegisterXPCTestComponents, 0, 0),
#endif

Просмотреть файл

@ -16,7 +16,7 @@
_PREFS_FILE */
/* defined by XPCShellImpl.cpp */
/* globals load, sendCommand */
/* globals load, sendCommand, changeTestShellDir */
/* must be defined by tests using do_await_remote_message/do_send_remote_message */
/* globals Cc, Ci */
@ -62,8 +62,6 @@ let { XPCOMUtils: _XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
let { OS: _OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
// Support a common assertion library, Assert.jsm.
var { Assert: AssertCls } = ChromeUtils.import(
"resource://testing-common/Assert.jsm"
@ -501,19 +499,12 @@ function _initDebugging(port) {
function _execute_test() {
if (typeof _TEST_CWD != "undefined") {
let cwd_complete = false;
_OS.File.setCurrentDirectory(_TEST_CWD)
.then(_ => (cwd_complete = true))
.catch(e => {
_testLogger.error(_exception_message(e));
cwd_complete = true;
});
_Services.tm.spinEventLoopUntil(
"Test(xpcshell/head.js:setCurrentDirectory)",
() => cwd_complete
);
try {
changeTestShellDir(_TEST_CWD);
} catch (e) {
_testLogger.error(_exception_message(e));
}
}
if (runningInParent && _AppConstants.platform == "android") {
try {
// GeckoView initialization needs the profile