summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/pack-scoped.js
blob: 05a77a0f0fc9224fb81a5f0eea6ff3a52f9f9d32 (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
// verify that prepublish runs on pack and publish
var test = require("tap").test
var fs = require("graceful-fs")
var join = require("path").join
var mkdirp = require("mkdirp")
var rimraf = require("rimraf")
var path = require("path")

var pkg      = join(__dirname, "scoped_package")
var manifest = join(pkg, "package.json")
var tmp      = join(pkg, "tmp")
var cache    = join(pkg, "cache")

var data = {
  name    : "@scope/generic-package",
  version : "90000.100001.5"
}

test("setup", function (t) {
  var n = 0

  mkdirp(pkg,   then())
  mkdirp(cache, then())
  mkdirp(tmp,   then())

  function then () {
    n++
    return function (er) {
      if (er) throw er
      if (--n === 0) next()
    }
  }

  function next () {
    fs.writeFile(manifest, JSON.stringify(data), "ascii", done)
  }

  function done (er) {
    if (er) throw er

    t.pass("setup done")
    t.end()
  }
})

test("test", function (t) {
  var spawn = require("child_process").spawn
  var node = process.execPath
  var npm = path.resolve(__dirname, "../../cli.js")
  var env = {
    "npm_config_cache"  : cache,
    "npm_config_tmp"    : tmp,
    "npm_config_prefix" : pkg,
    "npm_config_global" : "false"
  }

  for (var i in process.env) {
    if (!/^npm_config_/.test(i)) env[i] = process.env[i]
  }

  var child = spawn(node, [npm, "pack"], {cwd : pkg, env : env})

  child.stdout.setEncoding("utf8")
  child.stderr.on("data", onerr)
  child.stdout.on("data", ondata)
  child.on("close", onend)

  var c = "", e = ""
  function ondata (chunk) { c += chunk }
  function onerr  (chunk) { e += chunk }

  function onend () {
    if (e) {
      throw new Error("got stderr data: " + JSON.stringify("" + e))
    }
    c = c.trim()
    var regex = new RegExp("scope-generic-package-90000.100001.5.tgz", "ig")

    t.ok(c.match(regex), "found package")
    t.end()
  }
})

test("cleanup", function (t) {
  rimraf(pkg, function (er) {
    if (er) throw er

    t.pass("cleaned up")
    t.end()
  })
})