• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var common = require('../common-tap.js')
2var fs = require('fs')
3var path = require('path')
4
5var mkdirp = require('mkdirp')
6var test = require('tap').test
7
8var npm = require('../../lib/npm.js')
9
10var pkg = common.pkg
11var subDirectory = path.resolve(pkg, 'sub-directory')
12var packagePath = path.resolve(pkg, 'package.json')
13var shrinkwrapPath = path.resolve(pkg, 'npm-shrinkwrap.json')
14var cache = common.cache
15
16var json = { name: 'cat', version: '0.1.2' }
17
18test('npm version <semver> from a subdirectory', function (t) {
19  setup()
20  npmLoad()
21
22  function npmLoad () {
23    npm.load({ cache: cache }, function () {
24      common.makeGitRepo({
25        path: pkg,
26        added: ['package.json', 'npm-shrinkwrap.json']
27      }, version)
28    })
29  }
30
31  function version (er, stdout, stderr) {
32    t.ifError(er, 'git repo initialized without issue')
33    t.notOk(stderr, 'no error output')
34    npm.config.set('sign-git-tag', false)
35    npm.commands.version(['patch'], checkVersion)
36  }
37
38  function checkVersion (er) {
39    var newShrinkwrap = JSON.parse(fs.readFileSync(shrinkwrapPath))
40    t.is(newShrinkwrap.version, '0.1.3', 'shrinkwrap has right version')
41    var newPackage = JSON.parse(fs.readFileSync(packagePath))
42    t.is(newPackage.version, '0.1.3', 'package.json has right version')
43    var git = require('../../lib/utils/git.js')
44    t.ifError(er, 'version command ran without error')
45    git.whichAndExec(
46      ['log'],
47      { cwd: pkg, env: process.env },
48      checkCommit
49    )
50  }
51
52  function checkCommit (er, log, stderr) {
53    t.ifError(er, 'git log ran without issue')
54    t.notOk(stderr, 'no error output')
55    t.ok(log.match(/0\.1\.3/g), 'commited from subdirectory')
56    t.end()
57  }
58})
59
60function setup () {
61  mkdirp.sync(subDirectory)
62  process.chdir(subDirectory)
63  fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8')
64  fs.writeFileSync(shrinkwrapPath, JSON.stringify(json), 'utf8')
65}
66