• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2/* eslint-disable camelcase */
3const common = require('../common-tap.js')
4const http = require('http')
5const mr = require('npm-registry-mock')
6const npm = require('../../lib/npm.js')
7const path = require('path')
8const Tacks = require('tacks')
9const t = require('tap')
10const which = require('which')
11
12const Dir = Tacks.Dir
13const File = Tacks.File
14
15const ROOT = common.pkg
16const CACHE = common.cache
17const TMP = path.join(ROOT, 'tmp')
18const PREFIX = path.join(ROOT, 'global-prefix')
19const PKG = path.join(ROOT, 'pkg')
20
21let server
22let node_url
23const pingResponse = {
24  host: 'registry.npmjs.org',
25  ok: true,
26  username: null,
27  peer: 'example.com'
28}
29const npmResponse = {
30  name: 'npm',
31  'dist-tags': {latest: '0.0.0'},
32  'versions': {
33    '0.0.0': {
34      name: 'npm',
35      version: '0.0.0',
36      _shrinkwrap: null,
37      _hasShrinkwrap: false,
38      dist: {
39        shasum: 'deadbeef',
40        tarball: 'https://reg.eh/npm-0.0.0.tgz'
41      }
42    }
43  }
44}
45
46let nodeServer
47
48t.teardown(() => {
49  if (server) {
50    server.close()
51  }
52  if (nodeServer) {
53    nodeServer.close()
54  }
55})
56
57t.test('setup', (t) => {
58  const port = common.port + 1
59  nodeServer = http.createServer(function (q, s) {
60    s.end(JSON.stringify([{lts: true, version: '0.0.0'}]))
61  })
62  nodeServer.listen(port, () => {
63    node_url = 'http://localhost:' + port
64    mr({port: common.port}, (err, s) => {
65      t.ifError(err, 'registry mocked successfully')
66      server = s
67      server.get('/-/ping?write=true').reply(200, JSON.stringify(pingResponse))
68      server.get('/npm').reply(200, JSON.stringify(npmResponse))
69      const fixture = new Tacks(Dir({
70        [path.basename(PKG)]: Dir({
71          'package.json': File({name: 'foo', version: '1.0.0'})
72        }),
73        [path.basename(PREFIX)]: Dir({})
74      }))
75      fixture.create(ROOT)
76      npm.load({
77        registry: common.registry,
78        loglevel: 'silent',
79        cache: CACHE,
80        tmp: TMP,
81        prefix: PREFIX
82      }, (err) => {
83        t.ifError(err, 'npm loaded successfully')
84        t.pass('all set up')
85        t.done()
86      })
87    })
88  })
89})
90
91t.test('npm doctor', function (t) {
92  npm.commands.doctor({'node-url': node_url}, true, function (e, list) {
93    t.ifError(e, 'npm loaded successfully')
94    t.same(list.length, 9, 'list should have 9 prop')
95    t.same(list[0][1], 'ok', 'npm ping')
96    t.same(list[1][1], 'v' + npm.version, 'npm -v')
97    t.same(list[2][1], process.version, 'node -v')
98    t.same(list[3][1], common.registry + '/', 'npm config get registry')
99    t.same(list[5][1], 'ok', 'Perms check on cached files')
100    t.same(list[6][1], 'ok', 'Perms check on global node_modules')
101    t.same(list[7][1], 'ok', 'Perms check on local node_modules')
102    t.match(list[8][1], /^verified \d+ tarballs?$/, 'Cache verified')
103    which('git', function (e, resolvedPath) {
104      t.ifError(e, 'git command is installed')
105      t.same(list[4][1], resolvedPath, 'which git')
106      t.done()
107    })
108  })
109})
110
111t.test('npm doctor works without registry', function (t) {
112  npm.config.set('registry', false)
113  npm.commands.doctor({'node-url': node_url}, true, function (e, list) {
114    t.ifError(e, 'npm loaded successfully')
115    t.same(list.length, 9, 'list should have 9 prop')
116    t.same(list[0][1], 'ok', 'npm ping')
117    t.same(list[1][1], 'v' + npm.version, 'npm -v')
118    t.same(list[2][1], process.version, 'node -v')
119    t.same(list[3][1], '', 'no registry, but no crash')
120    t.same(list[5][1], 'ok', 'Perms check on cached files')
121    t.same(list[6][1], 'ok', 'Perms check on global node_modules')
122    t.same(list[7][1], 'ok', 'Perms check on local node_modules')
123    t.match(list[8][1], /^verified \d+ tarballs?$/, 'Cache verified')
124    which('git', function (e, resolvedPath) {
125      t.ifError(e, 'git command is installed')
126      t.same(list[4][1], resolvedPath, 'which git')
127      server.close()
128      t.done()
129    })
130  })
131})
132