Add specs for Module._nodeModulesPath
authorKevin Sawicki <kevinsawicki@gmail.com>
Thu, 23 Jun 2016 22:20:04 +0000 (15:20 -0700)
committerKevin Sawicki <kevinsawicki@gmail.com>
Thu, 23 Jun 2016 22:20:14 +0000 (15:20 -0700)
spec/modules-spec.js

index fbc66a0..c3ad4bc 100644 (file)
@@ -1,4 +1,5 @@
 const assert = require('assert')
+const Module = require('module')
 const path = require('path')
 const temp = require('temp')
 
@@ -47,3 +48,50 @@ describe('third-party module', function () {
     })
   })
 })
+
+describe('Module._nodeModulePaths', function () {
+  describe('when the path is inside the resources path', function () {
+    it('does not include paths outside of the resources path', function () {
+      let modulePath = process.resourcesPath
+      assert.deepEqual(Module._nodeModulePaths(modulePath), [
+        path.join(process.resourcesPath, 'node_modules')
+      ])
+
+      modulePath = path.join(process.resourcesPath, 'foo')
+      assert.deepEqual(Module._nodeModulePaths(modulePath), [
+        path.join(process.resourcesPath, 'foo', 'node_modules'),
+        path.join(process.resourcesPath, 'node_modules')
+      ])
+
+      modulePath = path.join(process.resourcesPath, 'node_modules', 'foo')
+      assert.deepEqual(Module._nodeModulePaths(modulePath), [
+        path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
+        path.join(process.resourcesPath, 'node_modules')
+      ])
+
+      modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar')
+      assert.deepEqual(Module._nodeModulePaths(modulePath), [
+        path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
+        path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
+        path.join(process.resourcesPath, 'node_modules')
+      ])
+
+      modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar')
+      assert.deepEqual(Module._nodeModulePaths(modulePath), [
+        path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
+        path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
+        path.join(process.resourcesPath, 'node_modules')
+      ])
+    })
+  })
+
+  describe('when the path is outside the resources path', function () {
+    it('includes paths outside of the resources path', function () {
+      let modulePath = path.resolve('/foo')
+      assert.deepEqual(Module._nodeModulePaths(modulePath), [
+        path.join(modulePath, 'node_modules'),
+        path.join('node_modules')
+      ])
+    })
+  })
+})