Welcome to mirror list, hosted at ThFree Co, Russian Federation.

help-search.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 567097a2eb183f404fb24abbbbbfae61fc2a8066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { test } = require('tap')
const { join } = require('path')
const requireInject = require('require-inject')
const mockNpm = require('../fixtures/mock-npm')
const ansicolors = require('ansicolors')

const OUTPUT = []
const output = (msg) => {
  OUTPUT.push(msg)
}

const config = {
  long: false,
}
const npmHelpErr = null
const npm = mockNpm({
  color: false,
  config,
  flatOptions: {
    long: false,
  },
  usage: 'npm test usage',
  commands: {
    help: (args, cb) => {
      return cb(npmHelpErr)
    },
  },
  output,
})

let globRoot = null
const globDir = {
  'npm-exec.md': 'the exec command\nhelp has multiple lines of exec help\none of them references exec',
  'npm-something.md': 'another\ncommand you run\nthat\nreferences exec\nand has multiple lines\nwith no matches\nthat will be ignored\nand another line\nthat does have exec as well',
  'npm-run-script.md': 'the scripted run-script command runs scripts\nand has lines\nsome of which dont match the string run\nor script\nscript',
  'npm-install.md': 'does a thing in a script\nif a thing does not exist in a thing you run\nto install it and run it maybe in a script',
  'npm-help.md': 'will run the `help-search` command if you need to run it to help you search',
  'npm-help-search.md': 'is the help search command\nthat you get if you run help-search',
  'npm-useless.md': 'exec\nexec',
  'npm-more-useless.md': 'exec exec',
  'npm-extra-useless.md': 'exec\nexec\nexec',
}
const glob = (p, cb) =>
  cb(null, Object.keys(globDir).map((file) => join(globRoot, file)))

const HelpSearch = requireInject('../../lib/help-search.js', {
  glob,
})
const helpSearch = new HelpSearch(npm)

test('npm help-search', t => {
  globRoot = t.testdir(globDir)
  t.teardown(() => {
    OUTPUT.length = 0
    globRoot = null
  })

  return helpSearch.exec(['exec'], (err) => {
    if (err)
      throw err

    t.match(OUTPUT, /Top hits for "exec"/, 'outputs results')
    t.end()
  })
})

test('npm help-search multiple terms', t => {
  globRoot = t.testdir(globDir)
  t.teardown(() => {
    OUTPUT.length = 0
    globRoot = null
  })

  return helpSearch.exec(['run', 'script'], (err) => {
    if (err)
      throw err

    t.match(OUTPUT, /Top hits for/, 'outputs results')
    t.match(OUTPUT, /run:\d+ script:\d+/, 'shows hit counts for both terms')
    t.end()
  })
})

test('npm help-search long output', t => {
  globRoot = t.testdir(globDir)
  config.long = true
  t.teardown(() => {
    OUTPUT.length = 0
    config.long = false
    globRoot = null
  })

  return helpSearch.exec(['exec'], (err) => {
    if (err)
      throw err

    t.match(OUTPUT, /has multiple lines of exec help/, 'outputs detailed results')
    t.end()
  })
})

test('npm help-search long output with color', t => {
  globRoot = t.testdir(globDir)
  config.long = true
  npm.color = true
  t.teardown(() => {
    OUTPUT.length = 0
    config.long = false
    npm.color = false
    globRoot = null
  })

  return helpSearch.exec(['help-search'], (err) => {
    if (err)
      throw err

    const highlightedText = ansicolors.bgBlack(ansicolors.red('help-search'))
    t.equal(OUTPUT.some((line) => line.includes(highlightedText)), true, 'returned highlighted search terms')
    t.end()
  })
})

test('npm help-search no args', t => {
  return helpSearch.exec([], (err) => {
    t.notOk(err)
    t.match(OUTPUT, /npm help-search/, 'outputs usage')
    t.end()
  })
})

test('npm help-search no matches', t => {
  globRoot = t.testdir(globDir)
  t.teardown(() => {
    OUTPUT.length = 0
    globRoot = null
  })

  return helpSearch.exec(['asdfasdf'], (err) => {
    if (err)
      throw err

    t.match(OUTPUT, /No matches/)
    t.end()
  })
})