summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/libnpmdiff/test/index.js
blob: 88b474c111f156a899aa3966a333faf0cc1e01ac (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
const { resolve } = require('path')

const t = require('tap')

const diff = require('../index.js')

const normalizePath = p => p
  .replace(/\\+/g, '/')
  .replace(/\r\n/g, '\n')

t.cleanSnapshot = (str) => normalizePath(str)
  .replace(normalizePath(process.execPath), 'node')

const json = (obj) => `${JSON.stringify(obj, null, 2)}\n`

t.test('compare two diff specs', async t => {
  const path = t.testdir({
    a1: {
      'package.json': json({
        name: 'a',
        version: '1.0.0',
      }),
      'index.js': 'module.exports =\n  "a1"\n',
    },
    a2: {
      'package.json': json({
        name: 'a',
        version: '2.0.0',
      }),
      'index.js': 'module.exports =\n  "a2"\n',
    },
  })

  const a = `file:${resolve(path, 'a1')}`
  const b = `file:${resolve(path, 'a2')}`

  t.resolveMatchSnapshot(diff([a, b], {}), 'should output expected diff')
})

t.test('using single arg', async t => {
  await t.rejects(
    diff(['abbrev@1.0.3']),
    /libnpmdiff needs two arguments to compare/,
    'should throw EDIFFARGS error'
  )
})

t.test('too many args', async t => {
  const args = ['abbrev@1.0.3', 'abbrev@1.0.4', 'abbrev@1.0.5']
  await t.rejects(
    diff(args),
    /libnpmdiff needs two arguments to compare/,
    'should output diff against cwd files'
  )
})

t.test('folder in node_modules', async t => {
  const path = t.testdir({
    node_modules: {
      a: {
        'package.json': json({
          name: 'a',
          version: '1.0.0',
          scripts: {
            prepare: `${process.execPath} prepare.js`,
          },
        }),
        'prepare.js': 'throw new Error("ERR")',
        node_modules: {
          b: {
            'package.json': json({
              name: 'b',
              version: '2.0.0',
              scripts: {
                prepare: `${process.execPath} prepare.js`,
              },
            }),
            'prepare.js': 'throw new Error("ERR")',
          },
        },
      },
    },
    packages: {
      a: {
        'package.json': json({
          name: 'a',
          version: '1.0.1',
          scripts: {
            prepare: `${process.execPath} prepare.js`,
          },
        }),
        'prepare.js': '',
      },
      b: {
        'package.json': json({
          name: 'b',
          version: '2.0.1',
          scripts: {
            prepare: `${process.execPath} prepare.js`,
          },
        }),
        'prepare.js': '',
      },
    },
    'package.json': json({
      name: 'my-project',
      version: '1.0.0',
    }),
  })

  t.test('top-level, absolute path', async t => {
    t.resolveMatchSnapshot(diff([
      `file:${resolve(path, 'node_modules/a')}`,
      `file:${resolve(path, 'packages/a')}`,
    ], { where: path }), 'should output expected diff')
  })
  t.test('top-level, relative path', async t => {
    const _cwd = process.cwd()
    process.chdir(path)
    t.teardown(() => {
      process.chdir(_cwd)
    })

    t.resolveMatchSnapshot(diff([
      'file:./node_modules/a',
      'file:./packages/a',
    ], { where: path }), 'should output expected diff')
  })
  t.test('nested, absolute path', async t => {
    t.resolveMatchSnapshot(diff([
      `file:${resolve(path, 'node_modules/a/node_modules/b')}`,
      `file:${resolve(path, 'packages/b')}`,
    ], { where: path}), 'should output expected diff')
  })
  t.test('nested, relative path', async t => {
    const _cwd = process.cwd()
    process.chdir(path)
    t.teardown(() => {
      process.chdir(_cwd)
    })

    t.resolveMatchSnapshot(diff([
      'file:./node_modules/a/node_modules/b',
      'file:./packages/b',
    ], { where: path }), 'should output expected diff')
  })
})