<commit_before>var fs = require('graceful-fs')
var path = require('path')
var osenv = require('osenv')
var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')

var pkg = path.resolve(__dirname, 'update-symlink')
var originalLog

var fakeRoot = path.join(__dirname, 'fakeRoot')
var OPTS = {
  env: {
    'npm_config_prefix': fakeRoot,
    'npm_config_registry': common.registry
  }
}

var jsonLocal = {
  name: 'my-local-package',
  description: 'fixture',
  version: '1.0.0',
  dependencies: {
    'async': '*',
    'underscore': '*'
  }
}

test('setup', function (t) {
  cleanup()
  originalLog = console.log
  mkdirp.sync(pkg)
  mr({ port: common.port }, function (er, s) {
    common.npm(['install', '-g', 'underscore@1.3.1'], OPTS, function (err, c, out) {
      t.ifError(err, 'global install did not error')
      process.chdir(pkg)
      fs.writeFileSync(
        path.join(pkg, 'package.json'),
        JSON.stringify(jsonLocal, null, 2)
      )
      common.npm(['link', 'underscore'], OPTS, function (err, c, out) {
        t.ifError(err, 'link did not error')
        common.npm(['install', 'async@0.2.9'], OPTS, function (err, c, out) {
          t.ifError(err, 'local install did not error')
          common.npm(['ls'], OPTS, function (err, c, out, stderr) {
            t.ifError(err)
            t.equal(c, 0)
            t.equal(stderr, '', 'got expected stderr')
            t.has(out, /async@0.2.9/, 'installed ok')
            t.has(out, /underscore@1.3.1/, 'creates local link ok')
            s.close()
            t.end()
          })
        })
      })
    })
  })
})

test('when update is called linked packages should be excluded', function (t) {
  console.log = function () {}
  mr({ port: common.port }, function (er, s) {
    common.npm(['update'], OPTS, function (err, c, out, stderr) {
      t.ifError(err)
      t.has(out, /async@0.2.10/, 'updated ok')
      t.doesNotHave(out, /underscore/, 'linked package not updated')
      t.doesNotHave(stderr, /ERR!/, 'no errors in stderr')
      s.close()
      t.end()
    })
  })
})

test('cleanup', function (t) {
  common.npm(['rm', 'underscore', 'async'], OPTS, function (err, code) {
    t.ifError(err, 'npm removed the linked package without error')
    t.equal(code, 0, 'cleanup in local ok')
    process.chdir(osenv.tmpdir())
    common.npm(['rm', '-g', 'underscore'], OPTS, function (err, code) {
      t.ifError(err, 'npm removed the global package without error')
      t.equal(code, 0, 'cleanup in global ok')

      console.log = originalLog
      cleanup()
      t.end()
    })
  })
})

function cleanup () {
  rimraf.sync(pkg)
  rimraf.sync(fakeRoot)
}<commit_msg>Add test for warning (versions differ) and no warning (versions same).<commit_after>var fs = require('graceful-fs')
var path = require('path')
var osenv = require('osenv')
var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')

var pkg = path.resolve(__dirname, 'update-symlink')
var originalLog

var fakeRoot = path.join(__dirname, 'fakeRoot')
var OPTS = {
  env: {
    'npm_config_prefix': fakeRoot,
    'npm_config_registry': common.registry
  }
}

var jsonLocal = {
  name: 'my-local-package',
  description: 'fixture',
  version: '1.0.0',
  dependencies: {
    'async': '*',
    'underscore': '*'
  }
}

test('setup', function (t) {
  cleanup()
  originalLog = console.log
  mkdirp.sync(pkg)
  mr({ port: common.port }, function (er, s) {
    common.npm(['install', '-g', 'underscore@1.3.1'], OPTS, function (err, c, out) {
      t.ifError(err, 'global install did not error')
      process.chdir(pkg)
      fs.writeFileSync(
        path.join(pkg, 'package.json'),
        JSON.stringify(jsonLocal, null, 2)
      )
      common.npm(['link', 'underscore'], OPTS, function (err, c, out) {
        t.ifError(err, 'link did not error')
        common.npm(['install', 'async@0.2.9'], OPTS, function (err, c, out) {
          t.ifError(err, 'local install did not error')
          common.npm(['ls'], OPTS, function (err, c, out, stderr) {
            t.ifError(err)
            t.equal(c, 0)
            t.equal(stderr, '', 'got expected stderr')
            t.has(out, /async@0.2.9/, 'installed ok')
            t.has(out, /underscore@1.3.1/, 'creates local link ok')
            s.close()
            t.end()
          })
        })
      })
    })
  })
})

test('when update is called linked packages should be excluded', function (t) {
  console.log = function () {}
  mr({ port: common.port }, function (er, s) {
    common.npm(['update'], OPTS, function (err, c, out, stderr) {
      t.ifError(err)
      t.has(out, /async@0.2.10/, 'updated ok')
      t.doesNotHave(out, /underscore/, 'linked package not updated')
      t.doesNotHave(stderr, /ERR!/, 'no errors in stderr')
      s.close()
      t.end()
    })
  })
})

test('when install is called and the package already exists as a link, outputs a warning if the requested version is not the same as the linked one', function (t) {
  console.log = function () {}
  mr({ port: common.port }, function (er, s) {
    common.npm(['install','underscore'], OPTS, function (err, c, out, stderr) {
      t.ifError(err)
      t.doesNotHave(out, /underscore/, 'linked package not updated')
      t.has(stderr, /underscore/, 'warning output relating to linked package')
      t.doesNotHave(stderr, /ERR!/, 'no errors in stderr')
      s.close()
      t.end()
    })
  })
})

test('when install is called and the package already exists as a link, does not warn if the requested version is same as the linked one', function (t) {
  console.log = function () {}
  mr({ port: common.port }, function (er, s) {
    common.npm(['install','underscore@1.3.1'], OPTS, function (err, c, out, stderr) {
      t.ifError(err)
      t.doesNotHave(out, /underscore/, 'linked package not updated')
      t.doesNotHave(stderr, /underscore/, 'no warning or error relating to linked package')
      t.doesNotHave(stderr, /ERR!/, 'no errors in stderr')
      s.close()
      t.end()
    })
  })
})

test('cleanup', function (t) {
  common.npm(['rm', 'underscore', 'async'], OPTS, function (err, code) {
    t.ifError(err, 'npm removed the linked package without error')
    t.equal(code, 0, 'cleanup in local ok')
    process.chdir(osenv.tmpdir())
    common.npm(['rm', '-g', 'underscore'], OPTS, function (err, code) {
      t.ifError(err, 'npm removed the global package without error')
      t.equal(code, 0, 'cleanup in global ok')

      console.log = originalLog
      cleanup()
      t.end()
    })
  })
})

function cleanup () {
  rimraf.sync(pkg)
  rimraf.sync(fakeRoot)
}
{'fraction_non_alphanumeric': 0.16970588235294118, 'fraction_numerical': 0.0072058823529411765, 'mean_word_length': 2.8642045454545455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '785250', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2275, 'n_words': 616}