summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/git-npmignore.js
blob: 6a703f0cf6f8f1c3cfa081126b525c8648618ca1 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var cat = require("graceful-fs").writeFileSync
var exec = require("child_process").exec
var readdir = require("graceful-fs").readdirSync
var resolve = require("path").resolve

var mkdirp = require("mkdirp")
var rimraf = require("rimraf")
var test = require("tap").test
var tmpdir = require("osenv").tmpdir
var which = require("which")

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

var pkg = resolve(__dirname, "git-npmignore")
var dep = resolve(pkg, "deps", "gitch")
var packname = "gitch-1.0.0.tgz"
var packed = resolve(pkg, packname)
var modules = resolve(pkg, "node_modules")
var installed = resolve(modules, "gitch")
var expected = [
  "a.js",
  "package.json",
  ".npmignore"
].sort()

var EXEC_OPTS = {
  cwd : pkg
}

test("setup", function (t) {
  setup(function (er) {
    t.ifError(er, "setup ran OK")

    t.end()
  })
})

test("npm pack directly from directory", function (t) {
  packInstallTest(dep, t)
})

test("npm pack via git", function (t) {
  packInstallTest("git+file://"+dep, t)
})

test("cleanup", function (t) {
  cleanup()

  t.end()
})

function packInstallTest (spec, t) {
  common.npm(
    [
      "--loglevel", "silent",
      "pack", spec
    ],
    EXEC_OPTS,
    function (err, code, stdout, stderr) {
      t.ifError(err,  "npm pack ran without error")
      t.notOk(code,   "npm pack exited cleanly")
      t.notOk(stderr, "npm pack ran silently")
      t.equal(stdout.trim(), packname, "got expected package name")

      common.npm(
        [
          "--loglevel", "silent",
          "install", packed
        ],
        EXEC_OPTS,
        function (err, code, stdout, stderr) {
          t.ifError(err,  "npm install ran without error")
          t.notOk(code,   "npm install exited cleanly")
          t.notOk(stderr, "npm install ran silently")

          var actual = readdir(installed).sort()
          t.same(actual, expected, "no unexpected files in packed directory")

          rimraf(packed, function () {
            t.end()
          })
        }
      )
    }
  )
}

var gitignore = "node_modules/\n"
var npmignore = "t.js\n"

var a = "console.log('hi');"
var t = "require('tap').test(function (t) { t.pass('I am a test!'); t.end(); });"
var fixture = {
  "name" : "gitch",
  "version" : "1.0.0",
  "private" : true,
  "main" : "a.js"
}

function cleanup () {
  process.chdir(tmpdir())
  rimraf.sync(pkg)
}

function setup (cb) {
  cleanup()

  mkdirp.sync(modules)
  mkdirp.sync(dep)

  process.chdir(dep)

  cat(resolve(dep, ".npmignore"), npmignore)
  cat(resolve(dep, ".gitignore"), gitignore)
  cat(resolve(dep, "a.js"), a)
  cat(resolve(dep, "t.js"), t)
  cat(resolve(dep, "package.json"), JSON.stringify(fixture))

  common.npm(
    [
      "--loglevel", "silent",
      "cache", "clean"
    ],
    EXEC_OPTS,
    function (er, code, _, stderr) {
      if (er) return cb(er)
      if (code) return cb(new Error("npm cache nonzero exit: "+code))
      if (stderr) return cb(new Error("npm cache clean error: "+stderr))

      which("git", function found (er, git) {
        if (er) return cb(er)

        exec(git+" init", init)

        function init (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error("git init error: "+stderr))

          exec(git+" config user.name 'Phantom Faker'", user)
        }

        function user (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error("git config error: "+stderr))

          exec(git+" config user.email nope@not.real", email)
        }

        function email (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error("git config error: "+stderr))

          exec(git+" add .", addAll)
        }

        function addAll (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error("git add . error: "+stderr))

          exec(git+" commit -m boot", commit)
        }

        function commit (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error("git commit error: "+stderr))

          cb()
        }
      })
    }
  )
}