maybe-github.js 1.81 KB
Newer Older
Nicolas Widart's avatar
Nicolas Widart committed
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
require("../common-tap.js")
var test = require("tap").test
var npm = require("../../lib/npm.js")

// this is the narrowest way to replace a function in the module cache
var found = true
var remoteGitPath = require.resolve("../../lib/cache/add-remote-git.js")
require("module")._cache[remoteGitPath] = {
  id: remoteGitPath,
  exports: function stub(_, __, cb) {
    if (found) {
      cb(null, {})
    }
    else {
      cb(new Error("not on filesystem"))
    }
  }
}

// only load maybeGithub now, so it gets the stub from cache
var maybeGithub = require("../../lib/cache/maybe-github.js")

test("should throw with no parameters", function (t) {
  t.plan(1)

  t.throws(function () {
    maybeGithub()
  }, "throws when called without parameters")
})

test("should throw with wrong parameter types", function (t) {
  t.plan(2)

  t.throws(function () {
    maybeGithub({}, function () {})
  }, "expects only a package name")

  t.throws(function () {
    maybeGithub("npm/xxx-noexist", "ham")
  }, "is always async")
})

test("should find an existing package on Github", function (t) {
  found = true
  npm.load({}, function (error) {
    t.notOk(error, "bootstrapping succeeds")
    t.doesNotThrow(function () {
      maybeGithub("npm/npm", function (error, data) {
        t.notOk(error, "no issues in looking things up")
        t.ok(data, "received metadata from Github")
        t.end()
      })
    })
  })
})

test("shouldn't find a nonexistent package on Github", function (t) {
  found = false
  npm.load({}, function () {
    t.doesNotThrow(function () {
      maybeGithub("npm/xxx-noexist", function (error, data) {
        t.equal(
          error.message,
          "not on filesystem",
          "passed through original error message"
        )
        t.notOk(data, "didn't pass any metadata")
        t.end()
      })
    })
  })
})