Initial commit
[yaffs-website] / node_modules / tar / test / extract-move.js
1 // Set the umask, so that it works the same everywhere.
2 process.umask(parseInt('22', 8))
3
4 var tap = require("tap")
5   , tar = require("../tar.js")
6   , fs = require("fs")
7   , gfs = require("graceful-fs")
8   , path = require("path")
9   , file = path.resolve(__dirname, "fixtures/dir.tar")
10   , target = path.resolve(__dirname, "tmp/extract-test")
11   , index = 0
12   , fstream = require("fstream")
13   , rimraf = require("rimraf")
14   , mkdirp = require("mkdirp")
15
16   , ee = 0
17   , expectEntries = [
18       {
19         "path" : "dir/",
20         "mode" : "750",
21         "type" : "5",
22         "depth" : undefined,
23         "size" : 0,
24         "linkpath" : "",
25         "nlink" : undefined,
26         "dev" : undefined,
27         "ino" : undefined
28       },
29       {
30         "path" : "dir/sub/",
31         "mode" : "750",
32         "type" : "5",
33         "depth" : undefined,
34         "size" : 0,
35         "linkpath" : "",
36         "nlink" : undefined,
37         "dev" : undefined,
38         "ino" : undefined
39       } ]
40
41 function slow (fs, method, t1, t2) {
42   var orig = fs[method]
43   if (!orig) return null
44   fs[method] = function () {
45     var args = [].slice.call(arguments)
46     console.error("slow", method, args[0])
47     var cb = args.pop()
48
49     setTimeout(function () {
50       orig.apply(fs, args.concat(function(er, data) {
51         setTimeout(function() {
52           cb(er, data)
53         }, t2)
54       }))
55     }, t1)
56   }
57 }
58
59 // Make sure we get the graceful-fs that fstream is using.
60 var gfs2
61 try {
62   gfs2 = require("fstream/node_modules/graceful-fs")
63 } catch (er) {}
64
65 var slowMethods = ["chown", "chmod", "utimes", "lutimes"]
66 slowMethods.forEach(function (method) {
67   var t1 = 500
68   var t2 = 0
69   slow(fs, method, t1, t2)
70   slow(gfs, method, t1, t2)
71   if (gfs2) {
72     slow(gfs2, method, t1, t2)
73   }
74 })
75
76
77
78 // The extract class basically just pipes the input
79 // to a Reader, and then to a fstream.DirWriter
80
81 // So, this is as much a test of fstream.Reader and fstream.Writer
82 // as it is of tar.Extract, but it sort of makes sense.
83
84 tap.test("preclean", function (t) {
85   rimraf.sync(target)
86   /mkdirp.sync(target)
87   t.pass("cleaned!")
88   t.end()
89 })
90
91 tap.test("extract test", function (t) {
92   var extract = tar.Extract(target)
93   var inp = fs.createReadStream(file)
94
95   // give it a weird buffer size to try to break in odd places
96   inp.bufferSize = 1234
97
98   inp.pipe(extract)
99
100   extract.on("end", function () {
101     rimraf.sync(target)
102
103     t.equal(ee, expectEntries.length, "should see "+ee+" entries")
104
105     // should get no more entries after end
106     extract.removeAllListeners("entry")
107     extract.on("entry", function (e) {
108       t.fail("Should not get entries after end!")
109     })
110
111     t.end()
112   })
113
114
115   extract.on("entry", function (entry) {
116     var found =
117       { path: entry.path
118       , mode: entry.props.mode.toString(8)
119       , type: entry.props.type
120       , depth: entry.props.depth
121       , size: entry.props.size
122       , linkpath: entry.props.linkpath
123       , nlink: entry.props.nlink
124       , dev: entry.props.dev
125       , ino: entry.props.ino
126       }
127
128     var wanted = expectEntries[ee ++]
129
130     t.equivalent(found, wanted, "tar entry " + ee + " " + wanted.path)
131   })
132 })