summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/publish.js
blob: 765cfb07c6c82e16ef16ee74e8c930563ef5a8f0 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'

const BB = require('bluebird')

const common = require('../common-tap')
const fs = require('fs')
const mkdirp = require('mkdirp')
const mr = BB.promisify(require('npm-registry-mock'))
const path = require('path')
const rimraf = require('rimraf')
const test = require('tap').test

const testDir = path.join(__dirname, 'publish_test_package')

function setup () {
  cleanup()
  mkdirp.sync(path.join(testDir, 'cache'))

  fs.writeFileSync(
    path.join(testDir, 'package.json'),
    JSON.stringify({
      name: 'publish-organized',
      version: '1.2.5'
    }, null, 2),
    'utf8'
  )

  fs.writeFileSync(
    path.join(testDir, 'index.js'),
    'hello',
    'utf8'
  )
}

let port = common.port
function withServer (cb) {
  return mr({port: port++, throwOnUnmatched: true})
    .tap(cb)
    .then((server) => {
      server.done()
      return server.close()
    })
}

test('basic npm publish', (t) => {
  setup()
  return withServer((server) => {
    server.filteringRequestBody(verify)
      .put('/publish-organized', true)
      .reply(201, {ok: true})

    return common.npm([
      'publish',
      '--no-color',
      '--cache', path.join(testDir, 'cache'),
      '--registry=' + common.registry.replace(common.port, server.port),
      `--//localhost:${server.port}/:username=username`,
      `--//localhost:${server.port}/:_password=` + Buffer.from('password').toString('base64'),
      `--//localhost:${server.port}/:email=` + 'ogd@aoaioxxysz.net'
    ], {'cwd': testDir})
      .spread((code, stdout, stderr) => {
        t.comment(stdout)
        t.comment(stderr)
        t.is(code, 0, 'published without error')
      })

    function verify (body) {
      t.doesNotThrow(() => {
        const parsed = JSON.parse(body)
        const current = parsed.versions['1.2.5']
        t.equal(
          current._npmVersion,
          require(path.resolve(__dirname, '../../package.json')).version,
          'npm version is correct'
        )

        t.equal(
          current._nodeVersion,
          process.versions.node,
          'node version is correct'
        )
      }, 'converted body back into object')

      return true
    }
  })
})

test('npm publish --dry-run', (t) => {
  setup()
  return common.npm([
    'publish',
    '--dry-run',
    '--registry=https://example.registry/fake',
    '--cache', path.join(testDir, 'cache'),
    '--loglevel=notice',
    '--no-color'
  ], {'cwd': testDir})
    .spread((code, stdout, stderr) => {
      t.comment(stdout)
      t.comment(stderr)
      t.is(code, 0, 'published without error')
      t.match(stderr, /notice\s+\d+[a-z]+\s+package\.json/gi, 'mentions package.json')
      t.match(stderr, /notice\s+\d+[a-z]+\s+index\.js/gi, 'mentions index.js')
    })
})

test('npm publish --json', (t) => {
  setup()
  return withServer((server) => {
    server.filteringRequestBody(() => true)
      .put('/publish-organized', true)
      .reply(201, {ok: true})
    return common.npm([
      'publish',
      '--json',
      '--registry', common.registry.replace(common.port, server.port),
      '--cache', path.join(testDir, 'cache')
    ], {'cwd': testDir})
      .spread((code, stdout, stderr) => {
        t.comment(stdout)
        t.comment(stderr)
        t.is(code, 0, 'published without error')
        t.similar(JSON.parse(stdout), {
          name: 'publish-organized',
          version: '1.2.5',
          files: [
            {path: 'package.json'},
            {path: 'index.js'}
          ],
          entryCount: 2
        }, 'JSON output reflects package contents')
        t.equal(stderr.trim(), '', 'nothing on stderr')
      })
  })
})

test('npm publish --dry-run --json', (t) => {
  setup()
  return common.npm([
    'publish',
    '--dry-run',
    '--json',
    '--registry=https://example.registry/fake',
    '--cache', path.join(testDir, 'cache'),
    '--loglevel=notice',
    '--no-color'
  ], {'cwd': testDir})
    .spread((code, stdout, stderr) => {
      t.comment(stdout)
      t.comment(stderr)
      t.is(code, 0, 'published without error')
      t.similar(JSON.parse(stdout), {
        name: 'publish-organized',
        version: '1.2.5',
        files: [
          {path: 'package.json'},
          {path: 'index.js'}
        ],
        entryCount: 2
      }, 'JSON output reflects package contents')
      t.equal(stderr.trim(), '', 'nothing on stderr')
    })
})

test('cleanup', (t) => {
  cleanup()
  t.end()
})

function cleanup () {
  process.chdir(__dirname)
  rimraf.sync(testDir)
}