OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library reader_test; | |
6 | |
7 import 'dart:io'; | |
8 import '../../../pkg/unittest/lib/unittest.dart' | |
9 import '../../archive/archive.dart'; | |
10 | |
11 final String dataPath = "utils/tests/archive/data"; | |
12 | |
13 main() { | |
14 test('reading a .tar.gz file', () { | |
15 var asyncDone = expectAsync0(() {}); | |
16 | |
17 var reader = new ArchiveReader(); | |
18 reader.format.tar = true; | |
19 reader.filter.gzip = true; | |
20 | |
21 var future = reader.openFilename("$dataPath/test-archive.tar.gz") | |
22 .then((input) { | |
23 var log = <String>[]; | |
24 input.onEntry = (entry) => guardAsync(() { | |
25 log.add("Entry: ${entry.pathname}"); | |
26 var stream = new StringInputStream(entry.openInputStream()); | |
27 stream.onData = () => log.add("Contents: ${stream.read().trim()}"); | |
28 stream.onClosed = () => log.add("Closed: ${entry.pathname}"); | |
29 }); | |
30 input.onError = registerException; | |
31 | |
32 input.onClosed = () => guardAsync(() { | |
33 expect(log, orderedEquals([ | |
34 "Entry: filename1", | |
35 "Contents: contents 1", | |
36 "Closed: filename1", | |
37 | |
38 "Entry: filename2", | |
39 "Contents: contents 2", | |
40 "Closed: filename2", | |
41 | |
42 "Entry: filename3", | |
43 "Contents: contents 3", | |
44 "Closed: filename3", | |
45 ])); | |
46 }, asyncDone); | |
47 }); | |
48 | |
49 expect(future, completes); | |
50 }); | |
51 | |
52 test('reading a .tar.gz file with readAll', () { | |
53 var reader = new ArchiveReader(); | |
54 reader.format.tar = true; | |
55 reader.filter.gzip = true; | |
56 | |
57 var future = reader.openFilename("$dataPath/test-archive.tar.gz") | |
58 .then((input) => input.readAll()) | |
59 .then((entries) { | |
60 entries = entries | |
61 .map((entry) => [entry.pathname, entry.contents.trim()]) | |
62 .toList(); | |
63 expect(entries[0], orderedEquals(["filename1", "contents 1"])); | |
64 expect(entries[1], orderedEquals(["filename2", "contents 2"])); | |
65 expect(entries[2], orderedEquals(["filename3", "contents 3"])); | |
66 }); | |
67 | |
68 expect(future, completes); | |
69 }); | |
70 | |
71 test('reading an in-memory .tar.gz', () { | |
72 var asyncDone = expectAsync0(() {}); | |
73 | |
74 var reader = new ArchiveReader(); | |
75 reader.format.tar = true; | |
76 reader.filter.gzip = true; | |
77 | |
78 var future = new File("$dataPath/test-archive.tar.gz").readAsBytes() | |
79 .then((bytes) => reader.openData(bytes)) | |
80 .then((input) { | |
81 var log = <String>[]; | |
82 input.onEntry = (entry) => guardAsync(() { | |
83 log.add("Entry: ${entry.pathname}"); | |
84 var stream = new StringInputStream(entry.openInputStream()); | |
85 stream.onData = () => log.add("Contents: ${stream.read().trim()}"); | |
86 stream.onClosed = () => log.add("Closed: ${entry.pathname}"); | |
87 }); | |
88 input.onError = registerException; | |
89 | |
90 input.onClosed = () => guardAsync(() { | |
91 expect(log, orderedEquals([ | |
92 "Entry: filename1", | |
93 "Contents: contents 1", | |
94 "Closed: filename1", | |
95 | |
96 "Entry: filename2", | |
97 "Contents: contents 2", | |
98 "Closed: filename2", | |
99 | |
100 "Entry: filename3", | |
101 "Contents: contents 3", | |
102 "Closed: filename3", | |
103 ])); | |
104 }, asyncDone); | |
105 }); | |
106 | |
107 expect(future, completes); | |
108 }); | |
109 | |
110 test("closing entries before they're read", () { | |
111 var asyncDone = expectAsync0(() {}); | |
112 | |
113 var reader = new ArchiveReader(); | |
114 reader.format.tar = true; | |
115 reader.filter.gzip = true; | |
116 | |
117 var future = reader.openFilename("$dataPath/test-archive.tar.gz") | |
118 .then((input) { | |
119 var log = <String>[]; | |
120 input.onEntry = (entry) => guardAsync(() { | |
121 log.add("Entry: ${entry.pathname}"); | |
122 var underlyingStream = entry.openInputStream(); | |
123 var stream = new StringInputStream(underlyingStream); | |
124 stream.onData = () => log.add("Contents: ${stream.read().trim()}"); | |
125 stream.onClosed = () => log.add("Closed: ${entry.pathname}"); | |
126 underlyingStream.close(); | |
127 }); | |
128 input.onError = registerException; | |
129 | |
130 input.onClosed = () => guardAsync(() { | |
131 expect(log, orderedEquals([ | |
132 "Entry: filename1", | |
133 "Closed: filename1", | |
134 | |
135 "Entry: filename2", | |
136 "Closed: filename2", | |
137 | |
138 "Entry: filename3", | |
139 "Closed: filename3", | |
140 ])); | |
141 }, asyncDone); | |
142 }); | |
143 | |
144 expect(future, completes); | |
145 }); | |
146 | |
147 test("closing an archive stream before it's finished", () { | |
148 var asyncDone = expectAsync0(() {}); | |
149 | |
150 var reader = new ArchiveReader(); | |
151 reader.format.tar = true; | |
152 reader.filter.gzip = true; | |
153 | |
154 var future = reader.openFilename("$dataPath/test-archive.tar.gz") | |
155 .then((input) { | |
156 var count = 0; | |
157 | |
158 var log = <String>[]; | |
159 input.onEntry = (entry) => guardAsync(() { | |
160 count += 1; | |
161 | |
162 log.add("Entry: ${entry.pathname}"); | |
163 var underlyingStream = entry.openInputStream(); | |
164 var stream = new StringInputStream(underlyingStream); | |
165 stream.onData = () => log.add("Contents: ${stream.read().trim()}"); | |
166 stream.onClosed = () => log.add("Closed: ${entry.pathname}"); | |
167 | |
168 if (count == 2) { | |
169 input.close(); | |
170 expect(input.closed); | |
171 } | |
172 }); | |
173 input.onError = registerException; | |
174 | |
175 input.onClosed = () { | |
176 expect(log, orderedEquals([ | |
177 "Entry: filename1", | |
178 "Contents: contents 1", | |
179 "Closed: filename1", | |
180 | |
181 "Entry: filename2", | |
182 "Closed: filename2", | |
183 ])); | |
184 asyncDone(); | |
185 }; | |
186 }); | |
187 | |
188 expect(future, completes); | |
189 }); | |
190 | |
191 test("opening a non-existent archive", () { | |
192 var reader = new ArchiveReader(); | |
193 reader.format.tar = true; | |
194 reader.filter.gzip = true; | |
195 | |
196 expect(reader.openFilename("$dataPath/non-existent.tar.gz"), | |
197 throwsA((e) => e is ArchiveException)); | |
198 }); | |
199 } | |
200 | |
OLD | NEW |