• 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, nodeServer
22let node_url
23
24t.teardown(() => {
25  if (server) {
26    server.close()
27  }
28  if (nodeServer) {
29    nodeServer.close()
30  }
31})
32
33t.test('setup', (t) => {
34  const port = common.port + 2
35  nodeServer = http.createServer(function (q, s) {
36    s.end(JSON.stringify([{ lts: true, version: '0.0.0' }]))
37  })
38  nodeServer.listen(port, () => {
39    node_url = 'http://localhost:' + port
40    mr({ port: common.port }, (err, s) => {
41      t.ifError(err, 'registry mocked successfully')
42      server = s
43      server.get('/-/ping?write=true').reply(404)
44      server.get('/npm').reply(
45        200,
46        JSON.stringify({
47          name: 'npm',
48          'dist-tags': { latest: '0.0.0' },
49          versions: {
50            '0.0.0': {
51              name: 'npm',
52              version: '0.0.0',
53              _shrinkwrap: null,
54              _hasShrinkwrap: false,
55              dist: {
56                shasum: 'deadbeef',
57                tarball: 'https://reg.eh/npm-0.0.0.tgz'
58              }
59            }
60          }
61        })
62      )
63      const fixture = new Tacks(
64        Dir({
65          [path.basename(PKG)]: Dir({
66            'package.json': File({ name: 'npm', version: '0.0.0' })
67          }),
68          [path.basename(PREFIX)]: Dir({})
69        })
70      )
71      fixture.create(ROOT)
72      npm.load(
73        {
74          registry: common.registry,
75          loglevel: 'silent',
76          cache: CACHE,
77          tmp: TMP,
78          prefix: PREFIX
79        },
80        (err) => {
81          t.ifError(err, 'npm loaded successfully')
82          t.pass('all set up')
83          t.done()
84        }
85      )
86    })
87  })
88})
89
90t.test('npm doctor ping returns 404 (or any other error)', function (t) {
91  npm.commands.doctor({ 'node-url': node_url }, true, function (e, list) {
92    t.ifError(e, 'npm loaded successfully')
93    t.same(list.length, 9, 'list should have 9 prop')
94    t.same(list[0][1], 'failed', 'npm ping')
95    t.same(list[1][1], 'v' + npm.version, 'npm -v')
96    t.same(list[2][1], process.version, 'node -v')
97    t.same(list[3][1], common.registry + '/', 'npm config get registry')
98    t.same(list[5][1], 'ok', 'Perms check on cached files')
99    t.same(list[6][1], 'ok', 'Perms check on global node_modules')
100    t.same(list[7][1], 'ok', 'Perms check on local node_modules')
101    t.match(list[8][1], /^verified \d+ tarballs?$/, 'Cache verified')
102    which('git', function (e, resolvedPath) {
103      t.ifError(e, 'git command is installed')
104      t.same(list[4][1], resolvedPath, 'which git')
105      t.done()
106    })
107  })
108})
109