test-find-node-directory.js 4.3 KB

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