tests: Fix check for global scope pollution

Since commit c570011 dropped the `with` statement, this bit of
the test has only checked for writes to a specific variable
in the file.

There is no direct replacement for `with` here, the best we can
do (I think) is comparing property names on the global objects
before and after the `eval()` call.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3132>
This commit is contained in:
Florian Müllner 2024-01-21 18:50:54 +01:00 committed by Marge Bot
parent ab40b5c711
commit b02460fa63

View File

@ -210,8 +210,8 @@ for (let i = 0; i < testsIsUnsafeExpression.length; i++) {
for (let i = 0; i < testsModifyScope.length; i++) {
let text = testsModifyScope[i];
// We need to use var here for the with statement
var obj = {};
const globalPropsPre = Object.getOwnPropertyNames(globalThis).sort();
// Just as in JsParse.getCompletions, we will find the offset
// of the expression, test whether it is unsafe, and then eval it.
@ -232,6 +232,8 @@ for (let i = 0; i < testsModifyScope.length; i++) {
}
}
}
let propertyNames = Object.getOwnPropertyNames(obj);
JsUnit.assertEquals(`The context '${JSON.stringify(obj)}' was not modified`, propertyNames.length, 0);
const globalPropsPost = Object.getOwnPropertyNames(globalThis).sort();
Assertions.assertArrayEquals('The global object was not modified',
globalPropsPre, globalPropsPost);
}