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

docs.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2a65786bf4d8e9d1427e905ef9fa8392c36fe12 (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
146
147
const t = require('tap')
const { fake: mockNpm } = require('../../fixtures/mock-npm.js')
const { join, sep } = require('path')

const pkgDirs = t.testdir({
  'package.json': JSON.stringify({
    name: 'thispkg',
    version: '1.2.3',
    homepage: 'https://example.com',
  }),
  nodocs: {
    'package.json': JSON.stringify({
      name: 'nodocs',
      version: '1.2.3',
    }),
  },
  docsurl: {
    'package.json': JSON.stringify({
      name: 'docsurl',
      version: '1.2.3',
      homepage: 'https://bugzilla.localhost/docsurl',
    }),
  },
  repourl: {
    'package.json': JSON.stringify({
      name: 'repourl',
      version: '1.2.3',
      repository: 'https://github.com/foo/repourl',
    }),
  },
  repoobj: {
    'package.json': JSON.stringify({
      name: 'repoobj',
      version: '1.2.3',
      repository: { url: 'https://github.com/foo/repoobj' },
    }),
  },
  repourlobj: {
    'package.json': JSON.stringify({
      name: 'repourlobj',
      version: '1.2.3',
      repository: { url: { works: false } },
    }),
  },
  workspaces: {
    'package.json': JSON.stringify({
      name: 'workspaces-test',
      version: '1.2.3-test',
      workspaces: ['workspace-a', 'workspace-b', 'workspace-c'],
    }),
    'workspace-a': {
      'package.json': JSON.stringify({
        name: 'workspace-a',
        version: '1.2.3-a',
        homepage: 'http://docs.workspace-a/',
      }),
    },
    'workspace-b': {
      'package.json': JSON.stringify({
        name: 'workspace-b',
        version: '1.2.3-n',
        repository: 'https://github.com/npm/workspace-b',
      }),
    },
    'workspace-c': JSON.stringify({
      'package.json': {
        name: 'workspace-n',
        version: '1.2.3-n',
      },
    }),
  },
})

// keep a tally of which urls got opened
let opened = {}
const openUrl = async (npm, url, errMsg) => {
  opened[url] = opened[url] || 0
  opened[url]++
}

const Docs = t.mock('../../../lib/commands/docs.js', {
  '../../../lib/utils/open-url.js': openUrl,
})
const flatOptions = {}
const npm = mockNpm({ flatOptions })
const docs = new Docs(npm)

t.afterEach(() => opened = {})

t.test('open docs urls', t => {
  npm.localPrefix = pkgDirs
  const expect = {
    nodocs: 'https://www.npmjs.com/package/nodocs',
    docsurl: 'https://bugzilla.localhost/docsurl',
    repourl: 'https://github.com/foo/repourl#readme',
    repoobj: 'https://github.com/foo/repoobj#readme',
    repourlobj: 'https://www.npmjs.com/package/repourlobj',
    '.': 'https://example.com',
  }
  const keys = Object.keys(expect)
  t.plan(keys.length)
  keys.forEach(pkg => {
    t.test(pkg, async t => {
      await docs.exec([['.', pkg].join(sep)])
      const url = expect[pkg]
      t.match({
        [url]: 1,
      }, opened, `opened ${url}`, { opened })
    })
  })
})

t.test('open default package if none specified', async t => {
  await docs.exec([])
  t.equal(opened['https://example.com'], 1, 'opened expected url', { opened })
})

t.test('workspaces', (t) => {
  npm.localPrefix = join(pkgDirs, 'workspaces')
  t.test('all workspaces', async t => {
    await docs.execWorkspaces([], [])
    t.match({
      'http://docs.workspace-a/': 1,
      'https://github.com/npm/workspace-b#readme': 1,
    }, opened, 'opened two valid docs urls')
  })

  t.test('one workspace', async t => {
    await docs.execWorkspaces([], ['workspace-a'])
    t.match({
      'http://docs.workspace-a/': 1,
    }, opened, 'opened one requested docs urls')
  })

  t.test('invalid workspace', async t => {
    await t.rejects(
      docs.execWorkspaces([], ['workspace-x']),
      /No workspaces found/
    )
    await t.rejects(
      docs.execWorkspaces([], ['workspace-x']),
      /workspace-x/
    )
    t.match({}, opened, 'opened no docs urls')
  })
  t.end()
})