npm: Upgrade to 1.3.19
[platform/upstream/nodejs.git] / deps / npm / test / tap / url-dependencies.js
1 var test = require("tap").test
2 var rimraf = require("rimraf")
3
4 var mr = require("npm-registry-mock")
5
6 var spawn = require("child_process").spawn
7 var npm = require.resolve("../../bin/npm-cli.js")
8 var node = process.execPath
9 var pkg = "./url-dependencies"
10
11 var mockRoutes = {
12   "get": {
13     "/underscore/-/underscore-1.3.1.tgz": [200]
14   }
15 }
16
17 test("url-dependencies: download first time", function(t) {
18   rimraf.sync(__dirname + "/url-dependencies/node_modules")
19
20   performInstall(function(output){
21     if(!tarballWasFetched(output)){
22       t.fail("Tarball was not fetched")
23     }else{
24       t.pass("Tarball was fetched")
25     }
26     t.end()
27   })
28 })
29
30 test("url-dependencies: do not download subsequent times", function(t) {
31   rimraf.sync(__dirname + "/url-dependencies/node_modules")
32
33   performInstall(function(){
34     performInstall(function(output){
35       if(tarballWasFetched(output)){
36         t.fail("Tarball was fetched second time around")
37       }else{
38         t.pass("Tarball was not fetched")
39       }
40       t.end()
41     })
42   })
43 })
44
45 function tarballWasFetched(output){
46   return output.indexOf("http GET http://localhost:1337/underscore/-/underscore-1.3.1.tgz") > -1
47 }
48
49 function performInstall (cb) {
50   mr({port: 1337, mocks: mockRoutes}, function(s){
51     var output = ""
52       , child = spawn(node, [npm, "install"], {
53           cwd: pkg,
54           env: {
55             npm_config_registry: "http://localhost:1337",
56             npm_config_cache_lock_stale: 1000,
57             npm_config_cache_lock_wait: 1000,
58             HOME: process.env.HOME,
59             Path: process.env.PATH,
60             PATH: process.env.PATH
61           }
62         })
63
64     child.stderr.on("data", function(data){
65       output += data.toString()
66     })
67     child.on("close", function () {
68       s.close()
69       cb(output)
70     })
71   })
72 }