summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/node-gyp/node_modules/tar/test/extract-move.js
blob: 45400cd9bb818d1c9b1320618360598307ad1efb (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
// Set the umask, so that it works the same everywhere.
process.umask(parseInt('22', 8))

var tap = require("tap")
  , tar = require("../tar.js")
  , fs = require("fs")
  , gfs = require("graceful-fs")
  , path = require("path")
  , file = path.resolve(__dirname, "fixtures/dir.tar")
  , target = path.resolve(__dirname, "tmp/extract-test")
  , index = 0
  , fstream = require("fstream")
  , rimraf = require("rimraf")
  , mkdirp = require("mkdirp")

  , ee = 0
  , expectEntries = [
      {
        "path" : "dir/",
        "mode" : "750",
        "type" : "5",
        "depth" : undefined,
        "size" : 0,
        "linkpath" : "",
        "nlink" : undefined,
        "dev" : undefined,
        "ino" : undefined
      },
      {
        "path" : "dir/sub/",
        "mode" : "750",
        "type" : "5",
        "depth" : undefined,
        "size" : 0,
        "linkpath" : "",
        "nlink" : undefined,
        "dev" : undefined,
        "ino" : undefined
      } ]

function slow (fs, method, t1, t2) {
  var orig = fs[method]
  if (!orig) return null
  fs[method] = function () {
    var args = [].slice.call(arguments)
    console.error("slow", method, args[0])
    var cb = args.pop()

    setTimeout(function () {
      orig.apply(fs, args.concat(function(er, data) {
        setTimeout(function() {
          cb(er, data)
        }, t2)
      }))
    }, t1)
  }
}

// Make sure we get the graceful-fs that fstream is using.
var gfs2
try {
  gfs2 = require("fstream/node_modules/graceful-fs")
} catch (er) {}

var slowMethods = ["chown", "chmod", "utimes", "lutimes"]
slowMethods.forEach(function (method) {
  var t1 = 500
  var t2 = 0
  slow(fs, method, t1, t2)
  slow(gfs, method, t1, t2)
  if (gfs2) {
    slow(gfs2, method, t1, t2)
  }
})



// The extract class basically just pipes the input
// to a Reader, and then to a fstream.DirWriter

// So, this is as much a test of fstream.Reader and fstream.Writer
// as it is of tar.Extract, but it sort of makes sense.

tap.test("preclean", function (t) {
  rimraf.sync(target)
  /mkdirp.sync(target)
  t.pass("cleaned!")
  t.end()
})

tap.test("extract test", function (t) {
  var extract = tar.Extract(target)
  var inp = fs.createReadStream(file)

  // give it a weird buffer size to try to break in odd places
  inp.bufferSize = 1234

  inp.pipe(extract)

  extract.on("end", function () {
    rimraf.sync(target)

    t.equal(ee, expectEntries.length, "should see "+ee+" entries")

    // should get no more entries after end
    extract.removeAllListeners("entry")
    extract.on("entry", function (e) {
      t.fail("Should not get entries after end!")
    })

    t.end()
  })


  extract.on("entry", function (entry) {
    var found =
      { path: entry.path
      , mode: entry.props.mode.toString(8)
      , type: entry.props.type
      , depth: entry.props.depth
      , size: entry.props.size
      , linkpath: entry.props.linkpath
      , nlink: entry.props.nlink
      , dev: entry.props.dev
      , ino: entry.props.ino
      }

    var wanted = expectEntries[ee ++]

    t.equivalent(found, wanted, "tar entry " + ee + " " + wanted.path)
  })
})