summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/init-create.js
blob: 22d9090a97ef24461b89fc051fa7e16bd1294bf5 (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
/* eslint-disable standard/no-callback-literal */
var test = require('tap').test
var requireInject = require('require-inject')

var npm = require('../../lib/npm.js')

test('npm init <name>', function (t) {
  var initJsonMock = function () {
    t.ok(false, 'should not run initJson()')
  }
  initJsonMock.yes = function () {
    t.ok(false, 'should not run initJson.yes()')
    return false
  }
  var libnpxMock = function () {
    return Promise.resolve()
  }
  libnpxMock.parseArgs = function (argv, defaultNpm) {
    t.ok(argv[0].includes('node'), 'node is the first arg')
    t.equals(argv[2], '--always-spawn', 'set npx opts.alwaysSpawn')
    t.equals(argv[3], 'create-name', 'expands name')
    t.ok(defaultNpm.endsWith('npm-cli.js'), 'passes npm bin path')
  }

  npm.load({ loglevel: 'silent' }, function () {
    var init = requireInject('../../lib/init', {
      'init-package-json': initJsonMock,
      'libnpx': libnpxMock
    })

    init(['name'], function () {})

    t.end()
  })
})

test('npm init expands scopes', function (t) {
  var libnpxMock = function () {
    return Promise.resolve()
  }

  npm.load({ loglevel: 'silent' }, function () {
    var init = requireInject('../../lib/init', {
      'libnpx': libnpxMock
    })

    libnpxMock.parseArgs = function (argv) {
      t.equals(argv[3], '@scope/create', 'expands @scope')
    }

    init(['@scope'], function () {})

    libnpxMock.parseArgs = function (argv) {
      t.equals(argv[3], '@scope/create-name', 'expands @scope/name')
    }

    init(['@scope/name'], function () {})

    t.end()
  })
})

test('npm init expands version names', function (t) {
  var libnpxMock = function () {
    return Promise.resolve()
  }

  npm.load({ loglevel: 'silent' }, function () {
    var init = requireInject('../../lib/init', {
      'libnpx': libnpxMock
    })

    libnpxMock.parseArgs = function (argv) {
      t.equals(argv[3], 'create-name@1.2.3', 'expands name@1.2.3')
    }

    init(['name@1.2.3'], function () {})

    libnpxMock.parseArgs = function (argv) {
      t.equals(argv[3], 'create-name@^1.2.3', 'expands name@^1.2.3')
    }

    init(['name@^1.2.3'], function () {})

    t.end()
  })
})

test('npm init expands git names', function (t) {
  var libnpxMock = function () {
    return Promise.resolve()
  }

  npm.load({ loglevel: 'silent' }, function () {
    var init = requireInject('../../lib/init', {
      'libnpx': libnpxMock
    })

    libnpxMock.parseArgs = function (argv) {
      t.equals(argv[3], 'user/create-foo', 'expands git repo')
    }

    init(['user/foo'], function () {})

    libnpxMock.parseArgs = function (argv) {
      t.equals(argv[3], 'git+https://github.com/user/create-foo', 'expands git url')
    }

    init(['git+https://github.com/user/foo'], function () {})

    t.end()
  })
})

test('npm init errors on folder and tarballs', function (t) {
  npm.load({ loglevel: 'silent' }, function () {
    var init = require('../../lib/init')

    try {
      init(['../foo/bar/'], function () {})
    } catch (e) {
      t.equals(e.code, 'EUNSUPPORTED')
    }

    t.throws(
      () => init(['../foo/bar/'], function () {}),
      /Unrecognized initializer: \.\.\/foo\/bar\//
    )

    t.throws(
      () => init(['file:foo.tar.gz'], function () {}),
      /Unrecognized initializer: file:foo\.tar\.gz/
    )

    t.throws(
      () => init(['http://x.com/foo.tgz'], function () {}),
      /Unrecognized initializer: http:\/\/x\.com\/foo\.tgz/
    )

    t.end()
  })
})

test('npm init forwards arguments', function (t) {
  var libnpxMock = function () {
    return Promise.resolve()
  }

  npm.load({ loglevel: 'silent' }, function () {
    var origArgv = process.argv
    var init = requireInject('../../lib/init', {
      'libnpx': libnpxMock
    })

    libnpxMock.parseArgs = function (argv) {
      process.argv = origArgv
      t.same(argv.slice(4), ['a', 'b', 'c'])
    }
    process.argv = [
      process.argv0,
      'NPM_CLI_PATH',
      'init',
      'name',
      'a', 'b', 'c'
    ]

    init(['name'], function () {})

    t.end()
  })
})