OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.io_test; |
| 16 |
| 17 import 'dart:async'; |
| 18 import 'dart:convert'; |
| 19 import 'dart:io'; |
| 20 |
| 21 import 'package:path/path.dart' as path; |
| 22 import 'package:quiver/io.dart'; |
| 23 import 'package:test/test.dart'; |
| 24 |
| 25 main() { |
| 26 group('byteStreamToString', () { |
| 27 test('should decode UTF8 text by default', () { |
| 28 var string = '箙、靫'; |
| 29 var encoded = UTF8.encoder.convert(string); |
| 30 var data = [encoded.sublist(0, 3), encoded.sublist(3)]; |
| 31 var stream = new Stream.fromIterable(data); |
| 32 byteStreamToString(stream).then((decoded) { |
| 33 expect(decoded, string); |
| 34 }); |
| 35 }); |
| 36 |
| 37 test('should decode text with the specified encoding', () { |
| 38 var string = 'blåbærgrød'; |
| 39 var encoded = LATIN1.encoder.convert(string); |
| 40 var data = [encoded.sublist(0, 4), encoded.sublist(4)]; |
| 41 var stream = new Stream.fromIterable(data); |
| 42 byteStreamToString(stream, encoding: LATIN1).then((decoded) { |
| 43 expect(decoded, string); |
| 44 }); |
| 45 }); |
| 46 }); |
| 47 |
| 48 group('visitDirectory', () { |
| 49 var testPath; |
| 50 var testDir; |
| 51 |
| 52 setUp(() { |
| 53 testDir = Directory.systemTemp.createTempSync(); |
| 54 testPath = testDir.path; |
| 55 }); |
| 56 |
| 57 tearDown(() { |
| 58 if (testDir.existsSync()) testDir.deleteSync(recursive: true); |
| 59 }); |
| 60 |
| 61 /* |
| 62 * Tests listing 7 cases of files, directories and links: |
| 63 * 1. A file |
| 64 * 2. A directory |
| 65 * 3. A file in a directory |
| 66 * 4. A link to a file |
| 67 * 5. A link to a directory |
| 68 * 6 A file in a directory, reached by a link to that directory |
| 69 * 7. A broken link |
| 70 */ |
| 71 test('should handle symlinks', () { |
| 72 new File(path.join(testPath, 'file_target')).createSync(); |
| 73 new Directory(path.join(testPath, 'dir_target')).createSync(); |
| 74 new File(path.join(testPath, 'dir_target/file')).createSync(); |
| 75 new Link(path.join(testPath, 'file_link')).createSync('file_target'); |
| 76 new Link(path.join(testPath, 'dir_link')).createSync('dir_target'); |
| 77 new Link(path.join(testPath, 'broken_link')).createSync('broken_target'); |
| 78 |
| 79 var results = []; |
| 80 |
| 81 return visitDirectory(testDir, (FileSystemEntity e) { |
| 82 if (e is File) { |
| 83 results.add("file: ${e.path}"); |
| 84 } else if (e is Directory) { |
| 85 results.add("dir: ${e.path}"); |
| 86 } else if (e is Link) { |
| 87 results.add("link: ${e.path}, ${e.targetSync()}"); |
| 88 } else { |
| 89 throw "bad"; |
| 90 } |
| 91 return new Future.value(true); |
| 92 }).then((_) { |
| 93 var expectation = [ |
| 94 "file: $testPath/file_target", |
| 95 "dir: $testPath/dir_target", |
| 96 "file: $testPath/dir_target/file", |
| 97 "link: $testPath/file_link, file_target", |
| 98 "link: $testPath/dir_link, dir_target", |
| 99 "file: $testPath/dir_link/file", |
| 100 "link: $testPath/broken_link, broken_target", |
| 101 ]; |
| 102 expect(results, unorderedEquals(expectation)); |
| 103 }); |
| 104 }); |
| 105 |
| 106 test('should conditionally recurse sub-directories', () { |
| 107 new Directory(path.join(testPath, 'dir')).createSync(); |
| 108 new File(path.join(testPath, 'dir/file')).createSync(); |
| 109 new Directory(path.join(testPath, 'dir2')).createSync(); |
| 110 new File(path.join(testPath, 'dir2/file')).createSync(); |
| 111 |
| 112 var files = []; |
| 113 return visitDirectory(testDir, (e) { |
| 114 files.add(e); |
| 115 return new Future.value(!e.path.endsWith('dir2')); |
| 116 }).then((_) { |
| 117 expect(files.map((e) => e.path), unorderedEquals( |
| 118 ["$testPath/dir", "$testPath/dir/file", "$testPath/dir2",])); |
| 119 }); |
| 120 }); |
| 121 |
| 122 test('should not infinitely recurse on symlink cycles', () { |
| 123 var dir = new Directory(path.join(testPath, 'dir'))..createSync(); |
| 124 new Link(path.join(testPath, 'dir/link')).createSync('../dir'); |
| 125 var files = []; |
| 126 return visitDirectory(dir, (e) { |
| 127 files.add(e); |
| 128 return new Future.value(true); |
| 129 }).then((_) { |
| 130 expect(files.length, 1); |
| 131 expect(files.first.targetSync(), '../dir'); |
| 132 }); |
| 133 }); |
| 134 }); |
| 135 } |
OLD | NEW |