• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var fs = require('graceful-fs')
2var path = require('path')
3
4var Bluebird = require('bluebird')
5var mr = Bluebird.promisify(require('npm-registry-mock'))
6var test = require('tap').test
7
8var common = require('../common-tap')
9
10var pkg = common.pkg
11
12var EXEC_OPTS = {
13  cwd: pkg,
14  env: common.newEnv().extend({
15    npm_config_registry: common.registry
16  }),
17  stdio: [0, 'pipe', 2]
18}
19
20var json = {
21  name: 'ls-depth-cli',
22  version: '0.0.0',
23  dependencies: {
24    'test-package-with-one-dep': '0.0.0'
25  }
26}
27
28test('setup', function (t) {
29  fs.writeFileSync(
30    path.join(pkg, 'package.json'),
31    JSON.stringify(json, null, 2)
32  )
33  return mr({ port: common.port }).then((s) => {
34    return common.npm(['install'], EXEC_OPTS).spread((c) => {
35      t.is(c, 0)
36    }).finally(() => s.close())
37  })
38})
39
40test('npm ls --depth=0', function (t) {
41  return common.npm(['ls', '--depth=0'], EXEC_OPTS).spread((c, out) => {
42    t.equal(c, 0, 'ls ran without raising error code')
43    t.has(
44      out,
45      /test-package-with-one-dep@0\.0\.0/,
46      'output contains test-package-with-one-dep@0.0.0'
47    )
48    t.doesNotHave(
49      out,
50      /test-package@0\.0\.0/,
51      'output not contains test-package@0.0.0'
52    )
53  })
54})
55
56test('npm ls --depth=1', function (t) {
57  common.npm(
58    ['ls', '--depth=1'],
59    EXEC_OPTS,
60    function (er, c, out) {
61      t.ifError(er, 'npm ls ran without issue')
62      t.equal(c, 0, 'ls ran without raising error code')
63      t.has(
64        out,
65        /test-package-with-one-dep@0\.0\.0/,
66        'output contains test-package-with-one-dep@0.0.0'
67      )
68      t.has(
69        out,
70        /test-package@0\.0\.0/,
71        'output contains test-package@0.0.0'
72      )
73      t.end()
74    }
75  )
76})
77
78test('npm ls --depth=Infinity', function (t) {
79  // travis has a preconfigured depth=0, in general we can not depend
80  // on the default value in all environments, so explicitly set it here
81  common.npm(
82    ['ls', '--depth=Infinity'],
83    EXEC_OPTS,
84    function (er, c, out) {
85      t.ifError(er, 'npm ls ran without issue')
86      t.equal(c, 0, 'ls ran without raising error code')
87      t.has(
88        out,
89        /test-package-with-one-dep@0\.0\.0/,
90        'output contains test-package-with-one-dep@0.0.0'
91      )
92      t.has(
93        out,
94        /test-package@0\.0\.0/,
95        'output contains test-package@0.0.0'
96      )
97      t.end()
98    }
99  )
100})
101
102test('npm ls --depth=0 --json', function (t) {
103  common.npm(
104    ['ls', '--depth=0', '--json'],
105    EXEC_OPTS,
106    function (er, c, out) {
107      t.ifError(er, 'npm ls ran without issue')
108      t.equal(c, 0, 'ls ran without raising error code')
109      t.has(JSON.parse(out), {
110        'name': 'ls-depth-cli',
111        'version': '0.0.0',
112        'dependencies': {
113          'test-package-with-one-dep': {
114            'version': '0.0.0',
115            'resolved': 'http://localhost:' + common.port + '/test-package-with-one-dep/-/test-package-with-one-dep-0.0.0.tgz'
116          }
117        }
118      })
119      t.end()
120    }
121  )
122})
123
124test('npm ls --depth=Infinity --json', function (t) {
125  // travis has a preconfigured depth=0, in general we can not depend
126  // on the default value in all environments, so explicitly set it here
127  common.npm(
128    ['ls', '--depth=Infinity', '--json'],
129    EXEC_OPTS,
130    function (er, c, out) {
131      t.ifError(er, 'npm ls ran without issue')
132      t.equal(c, 0, 'ls ran without raising error code')
133      t.has(JSON.parse(out), {
134        'name': 'ls-depth-cli',
135        'version': '0.0.0',
136        'dependencies': {
137          'test-package-with-one-dep': {
138            'version': '0.0.0',
139            'resolved': 'http://localhost:' + common.port + '/test-package-with-one-dep/-/test-package-with-one-dep-0.0.0.tgz',
140            'dependencies': {
141              'test-package': {
142                'version': '0.0.0',
143                'resolved': 'http://localhost:' + common.port + '/test-package/-/test-package-0.0.0.tgz'
144              }
145            }
146          }
147        }
148      })
149      t.end()
150    }
151  )
152})
153
154test('npm ls --depth=0 --parseable --long', function (t) {
155  common.npm(
156    ['ls', '--depth=0', '--parseable', '--long'],
157    EXEC_OPTS,
158    function (er, c, out) {
159      t.ifError(er, 'npm ls ran without issue')
160      t.equal(c, 0, 'ls ran without raising error code')
161      t.has(
162        out,
163        /.*test-package-with-one-dep@0\.0\.0/,
164        'output contains test-package-with-one-dep'
165      )
166      t.doesNotHave(
167        out,
168        /.*test-package@0\.0\.0/,
169        'output not contains test-package'
170      )
171      t.end()
172    }
173  )
174})
175
176test('npm ls --depth=1 --parseable --long', function (t) {
177  common.npm(
178    ['ls', '--depth=1', '--parseable', '--long'],
179    EXEC_OPTS,
180    function (er, c, out) {
181      t.ifError(er, 'npm ls ran without issue')
182      t.equal(c, 0, 'ls ran without raising error code')
183      t.has(
184        out,
185        /.*test-package-with-one-dep@0\.0\.0/,
186        'output contains test-package-with-one-dep'
187      )
188      t.has(
189        out,
190        /.*test-package@0\.0\.0/,
191        'output not contains test-package'
192      )
193      t.end()
194    }
195  )
196})
197