test-find-node-directory.js 4.2 KB

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