test.js 4.6 KB

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