Browse Source

test: add spec for --require filtering in NODE_OPTIONS (#29506)

Milan Burda 3 years ago
parent
commit
43384a10f0
3 changed files with 28 additions and 0 deletions
  1. 4 0
      shell/common/node_bindings.cc
  2. 23 0
      spec-main/node-spec.ts
  3. 1 0
      spec/fixtures/module/fail.js

+ 4 - 0
shell/common/node_bindings.cc

@@ -131,6 +131,10 @@ void stop_and_close_uv_loop(uv_loop_t* loop) {
 bool g_is_initialized = false;
 
 bool IsPackagedApp() {
+  auto env = base::Environment::Create();
+  if (env->HasVar("ELECTRON_FORCE_IS_PACKAGED"))
+    return true;
+
   base::FilePath exe_path;
   base::PathService::Get(base::FILE_EXE, &exe_path);
   base::FilePath::StringType base_name =

+ 23 - 0
spec-main/node-spec.ts

@@ -132,6 +132,29 @@ describe('node feature', () => {
       child.stderr.on('data', listener);
       child.stdout.on('data', listener);
     });
+
+    it('does allow --require in non-packaged apps', async () => {
+      const appPath = path.join(fixtures, 'module', 'noop.js');
+      const env = Object.assign({}, process.env, {
+        NODE_OPTIONS: `--require=${path.join(fixtures, 'module', 'fail.js')}`
+      });
+      // App should exit with code 1.
+      const child = childProcess.spawn(process.execPath, [appPath], { env });
+      const [code] = await emittedOnce(child, 'exit');
+      expect(code).to.equal(1);
+    });
+
+    it('does not allow --require in packaged apps', async () => {
+      const appPath = path.join(fixtures, 'module', 'noop.js');
+      const env = Object.assign({}, process.env, {
+        ELECTRON_FORCE_IS_PACKAGED: 'true',
+        NODE_OPTIONS: `--require=${path.join(fixtures, 'module', 'fail.js')}`
+      });
+      // App should exit with code 0.
+      const child = childProcess.spawn(process.execPath, [appPath], { env });
+      const [code] = await emittedOnce(child, 'exit');
+      expect(code).to.equal(0);
+    });
   });
 
   describe('Node.js cli flags', () => {

+ 1 - 0
spec/fixtures/module/fail.js

@@ -0,0 +1 @@
+process.exit(1);