Welcome to mirror list, hosted at ThFree Co, Russian Federation.

version-git-not-clean.js « tap « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a942be3e86088a5b6f7e90f5b585d84747d3412f (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
var common = require('../common-tap.js')
var test = require('tap').test
var npm = require('../../')
var osenv = require('osenv')
var path = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var which = require('which')
var spawn = require('child_process').spawn

var pkg = path.resolve(__dirname, 'version-git-not-clean')
var cache = path.resolve(pkg, 'cache')

test('npm version <semver> with working directory not clean', function (t) {
  setup()
  npm.load({ cache: cache, registry: common.registry, prefix: pkg }, function () {
    which('git', function (err, git) {
      t.ifError(err, 'git found')

      function addPackageJSON (_cb) {
        var data = JSON.stringify({ name: 'blah', version: '0.1.2' })
        fs.writeFile('package.json', data, function () {
          var child = spawn(git, ['add', 'package.json'])
          child.on('exit', function () {
            var child2 = spawn(git, ['commit', 'package.json', '-m', 'init'])
            var out = ''
            child2.stdout.on('data', function (d) {
              out += d.toString()
            })
            child2.on('exit', function () {
              return _cb(out)
            })
          })
        })
      }

      common.makeGitRepo({path: pkg}, function () {
        addPackageJSON(function () {
          var data = JSON.stringify({ name: 'blah', version: '0.1.3' })
          fs.writeFile('package.json', data, function () {
            npm.commands.version(['patch'], function (err) {
              if (!err) {
                t.fail('should fail on non-clean working directory')
              } else {
                t.ok(err.message.match(/Git working directory not clean./))
                t.ok(err.message.match(/M package.json/))
              }
              t.end()
            })
          })
        })
      })
    })
  })
})

test('npm version <semver> --force with working directory not clean', function (t) {
  npm.load({ cache: cache, registry: common.registry, prefix: pkg }, function () {
    common.npm(
      [
        '--force',
        'version',
        'patch'
      ],
      {cwd: pkg, env: {PATH: process.env.PATH}},
      function (err, code, stdout, stderr) {
        t.ifError(err, 'npm version ran without issue')
        t.notOk(code, 'exited with a non-error code')
        var errorLines = stderr.trim().split('\n')
          .map(function (line) {
            return line.trim()
          })
          .filter(function (line) {
            return !line.indexOf('using --force')
          })
        t.notOk(errorLines.length, 'no error output')
        t.end()
      })
  })
})

test('cleanup', function (t) {
  // windows fix for locked files
  process.chdir(osenv.tmpdir())

  rimraf.sync(pkg)
  t.end()
})

function setup () {
  mkdirp.sync(pkg)
  mkdirp.sync(cache)
  process.chdir(pkg)
}