a5a1bf94695af24be8135538b9bf9cd6077c1bdd3173c07bbbe7153d16230e328dab7ae12df1b8b5ab883ee8695bc41a693e7be6f5cbcd309849c4dc0c7ebf 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict'
  2. const test = require('tape')
  3. const concat = require('concat-stream')
  4. const fs = require('fs')
  5. const helpMe = require('./')
  6. test('throws if no directory is passed', function (t) {
  7. try {
  8. helpMe()
  9. t.fail()
  10. } catch (err) {
  11. t.equal(err.message, 'missing dir')
  12. }
  13. t.end()
  14. })
  15. test('throws if a normal file is passed', function (t) {
  16. try {
  17. helpMe({
  18. dir: __filename
  19. })
  20. t.fail()
  21. } catch (err) {
  22. t.equal(err.message, `${__filename} is not a directory`)
  23. }
  24. t.end()
  25. })
  26. test('throws if the directory cannot be accessed', function (t) {
  27. try {
  28. helpMe({
  29. dir: './foo'
  30. })
  31. t.fail()
  32. } catch (err) {
  33. t.equal(err.message, './foo is not a directory')
  34. }
  35. t.end()
  36. })
  37. test('show a generic help.txt from a folder to a stream', function (t) {
  38. t.plan(2)
  39. helpMe({
  40. dir: 'fixture/basic'
  41. }).createStream()
  42. .pipe(concat(function (data) {
  43. fs.readFile('fixture/basic/help.txt', function (err, expected) {
  44. t.error(err)
  45. t.equal(data.toString(), expected.toString())
  46. })
  47. }))
  48. })
  49. test('custom help command with an array', function (t) {
  50. t.plan(2)
  51. helpMe({
  52. dir: 'fixture/basic'
  53. }).createStream(['hello'])
  54. .pipe(concat(function (data) {
  55. fs.readFile('fixture/basic/hello.txt', function (err, expected) {
  56. t.error(err)
  57. t.equal(data.toString(), expected.toString())
  58. })
  59. }))
  60. })
  61. test('custom help command without an ext', function (t) {
  62. t.plan(2)
  63. helpMe({
  64. dir: 'fixture/no-ext',
  65. ext: ''
  66. }).createStream(['hello'])
  67. .pipe(concat(function (data) {
  68. fs.readFile('fixture/no-ext/hello', function (err, expected) {
  69. t.error(err)
  70. t.equal(data.toString(), expected.toString())
  71. })
  72. }))
  73. })
  74. test('custom help command with a string', function (t) {
  75. t.plan(2)
  76. helpMe({
  77. dir: 'fixture/basic'
  78. }).createStream('hello')
  79. .pipe(concat(function (data) {
  80. fs.readFile('fixture/basic/hello.txt', function (err, expected) {
  81. t.error(err)
  82. t.equal(data.toString(), expected.toString())
  83. })
  84. }))
  85. })
  86. test('missing help file', function (t) {
  87. t.plan(1)
  88. helpMe({
  89. dir: 'fixture/basic'
  90. }).createStream('abcde')
  91. .on('error', function (err) {
  92. t.equal(err.message, 'no such help file')
  93. })
  94. .resume()
  95. })
  96. test('custom help command with an array', function (t) {
  97. const helper = helpMe({
  98. dir: 'fixture/shortnames'
  99. })
  100. t.test('abbreviates two words in one', function (t) {
  101. t.plan(2)
  102. helper
  103. .createStream(['world'])
  104. .pipe(concat(function (data) {
  105. fs.readFile('fixture/shortnames/hello world.txt', function (err, expected) {
  106. t.error(err)
  107. t.equal(data.toString(), expected.toString())
  108. })
  109. }))
  110. })
  111. t.test('abbreviates three words in two', function (t) {
  112. t.plan(2)
  113. helper
  114. .createStream(['abcde', 'fghi'])
  115. .pipe(concat(function (data) {
  116. fs.readFile('fixture/shortnames/abcde fghi lmno.txt', function (err, expected) {
  117. t.error(err)
  118. t.equal(data.toString(), expected.toString())
  119. })
  120. }))
  121. })
  122. t.test('abbreviates a word', function (t) {
  123. t.plan(2)
  124. helper
  125. .createStream(['abc', 'fg'])
  126. .pipe(concat(function (data) {
  127. fs.readFile('fixture/shortnames/abcde fghi lmno.txt', function (err, expected) {
  128. t.error(err)
  129. t.equal(data.toString(), expected.toString())
  130. })
  131. }))
  132. })
  133. t.test('abbreviates a word using strings', function (t) {
  134. t.plan(2)
  135. helper
  136. .createStream('abc fg')
  137. .pipe(concat(function (data) {
  138. fs.readFile('fixture/shortnames/abcde fghi lmno.txt', function (err, expected) {
  139. t.error(err)
  140. t.equal(data.toString(), expected.toString())
  141. })
  142. }))
  143. })
  144. t.test('print a disambiguation', function (t) {
  145. t.plan(1)
  146. const expected = '' +
  147. 'There are 2 help pages that matches the given request, please disambiguate:\n' +
  148. ' * abcde fghi lmno\n' +
  149. ' * abcde hello\n'
  150. helper
  151. .createStream(['abc'])
  152. .pipe(concat({ encoding: 'string' }, function (data) {
  153. t.equal(data, expected)
  154. }))
  155. })
  156. t.test('choose exact match over partial', function (t) {
  157. t.plan(1)
  158. helpMe({
  159. dir: 'fixture/sameprefix'
  160. }).createStream(['hello'])
  161. .pipe(concat({ encoding: 'string' }, function (data) {
  162. t.equal(data, 'hello')
  163. }))
  164. })
  165. })
  166. test('support for help files organized in folders', function (t) {
  167. const helper = helpMe({
  168. dir: 'fixture/dir'
  169. })
  170. t.test('passing an array', function (t) {
  171. t.plan(2)
  172. helper
  173. .createStream(['a', 'b'])
  174. .pipe(concat(function (data) {
  175. fs.readFile('fixture/dir/a/b.txt', function (err, expected) {
  176. t.error(err)
  177. t.equal(data.toString(), expected.toString())
  178. })
  179. }))
  180. })
  181. t.test('passing a string', function (t) {
  182. t.plan(2)
  183. helper
  184. .createStream('a b')
  185. .pipe(concat(function (data) {
  186. fs.readFile('fixture/dir/a/b.txt', function (err, expected) {
  187. t.error(err)
  188. t.equal(data.toString(), expected.toString())
  189. })
  190. }))
  191. })
  192. })