• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const { describe, it } = require('mocha')
4const assert = require('assert')
5const path = require('path')
6const findNodeDirectory = require('../lib/find-node-directory')
7
8const platforms = ['darwin', 'freebsd', 'linux', 'sunos', 'win32', 'aix', 'os400']
9
10describe('find-node-directory', function () {
11  // we should find the directory based on the directory
12  // the script is running in and it should match the layout
13  // in a build tree where npm is installed in
14  // .... /deps/npm
15  it('test find-node-directory - node install', function () {
16    for (var next = 0; next < platforms.length; next++) {
17      var processObj = { execPath: '/x/y/bin/node', platform: platforms[next] }
18      assert.strictEqual(
19        findNodeDirectory('/x/deps/npm/node_modules/node-gyp/lib', processObj),
20        path.join('/x'))
21    }
22  })
23
24  // we should find the directory based on the directory
25  // the script is running in and it should match the layout
26  // in an installed tree where npm is installed in
27  // .... /lib/node_modules/npm or .../node_modules/npm
28  // depending on the patform
29  it('test find-node-directory - node build', function () {
30    for (var next = 0; next < platforms.length; next++) {
31      var processObj = { execPath: '/x/y/bin/node', platform: platforms[next] }
32      if (platforms[next] === 'win32') {
33        assert.strictEqual(
34          findNodeDirectory('/y/node_modules/npm/node_modules/node-gyp/lib',
35            processObj), path.join('/y'))
36      } else {
37        assert.strictEqual(
38          findNodeDirectory('/y/lib/node_modules/npm/node_modules/node-gyp/lib',
39            processObj), path.join('/y'))
40      }
41    }
42  })
43
44  // we should find the directory based on the execPath
45  // for node and match because it was in the bin directory
46  it('test find-node-directory - node in bin directory', function () {
47    for (var next = 0; next < platforms.length; next++) {
48      var processObj = { execPath: '/x/y/bin/node', platform: platforms[next] }
49      assert.strictEqual(
50        findNodeDirectory('/nothere/npm/node_modules/node-gyp/lib', processObj),
51        path.join('/x/y'))
52    }
53  })
54
55  // we should find the directory based on the execPath
56  // for node and match because it was in the Release directory
57  it('test find-node-directory - node in build release dir', function () {
58    for (var next = 0; next < platforms.length; next++) {
59      var processObj
60      if (platforms[next] === 'win32') {
61        processObj = { execPath: '/x/y/Release/node', platform: platforms[next] }
62      } else {
63        processObj = {
64          execPath: '/x/y/out/Release/node',
65          platform: platforms[next]
66        }
67      }
68
69      assert.strictEqual(
70        findNodeDirectory('/nothere/npm/node_modules/node-gyp/lib', processObj),
71        path.join('/x/y'))
72    }
73  })
74
75  // we should find the directory based on the execPath
76  // for node and match because it was in the Debug directory
77  it('test find-node-directory - node in Debug release dir', function () {
78    for (var next = 0; next < platforms.length; next++) {
79      var processObj
80      if (platforms[next] === 'win32') {
81        processObj = { execPath: '/a/b/Debug/node', platform: platforms[next] }
82      } else {
83        processObj = { execPath: '/a/b/out/Debug/node', platform: platforms[next] }
84      }
85
86      assert.strictEqual(
87        findNodeDirectory('/nothere/npm/node_modules/node-gyp/lib', processObj),
88        path.join('/a/b'))
89    }
90  })
91
92  // we should not find it as it will not match based on the execPath nor
93  // the directory from which the script is running
94  it('test find-node-directory - not found', function () {
95    for (var next = 0; next < platforms.length; next++) {
96      var processObj = { execPath: '/x/y/z/y', platform: next }
97      assert.strictEqual(findNodeDirectory('/a/b/c/d', processObj), '')
98    }
99  })
100
101  // we should find the directory based on the directory
102  // the script is running in and it should match the layout
103  // in a build tree where npm is installed in
104  // .... /deps/npm
105  // same test as above but make sure additional directory entries
106  // don't cause an issue
107  it('test find-node-directory - node install', function () {
108    for (var next = 0; next < platforms.length; next++) {
109      var processObj = { execPath: '/x/y/bin/node', platform: platforms[next] }
110      assert.strictEqual(
111        findNodeDirectory('/x/y/z/a/b/c/deps/npm/node_modules/node-gyp/lib',
112          processObj), path.join('/x/y/z/a/b/c'))
113    }
114  })
115})
116