Skip to content

Commit 2f625ce

Browse files
authored
Add the ability to test a FileSystem without using dart:io explicitly (#254)
1 parent 56cf33d commit 2f625ce

File tree

4 files changed

+264
-176
lines changed

4 files changed

+264
-176
lines changed

pkgs/io_file/test/create_directory_test.dart

Lines changed: 185 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -13,185 +13,194 @@ import 'package:test/test.dart';
1313
import 'package:win32/win32.dart' as win32;
1414

1515
import 'errors.dart' as errors;
16+
import 'file_system_file_utils.dart' hide fileUtils;
1617
import 'test_utils.dart';
1718

19+
void tests(FileUtils utils, FileSystem fs) {
20+
late String tmp;
21+
late String cwd;
22+
23+
setUp(() {
24+
tmp = createTemp('createDirectory');
25+
cwd = fs.currentDirectory;
26+
fs.currentDirectory = tmp;
27+
});
28+
29+
tearDown(() {
30+
fs.currentDirectory = cwd;
31+
deleteTemp(tmp);
32+
});
33+
34+
test('success', () {
35+
final path = '$tmp/dir';
36+
37+
fs.createDirectory(path);
38+
expect(utils.isDirectory(path), isTrue);
39+
});
40+
41+
test('absolute path, long directory name', () {
42+
// On Windows:
43+
// When using an API to create a directory, the specified path cannot be
44+
// so long that you cannot append an 8.3 file name (that is, the directory
45+
// name cannot exceed MAX_PATH minus 12).
46+
final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
47+
final path = p.join(tmp, dirname);
48+
49+
fs.createDirectory(path);
50+
expect(utils.isDirectory(path), isTrue);
51+
});
52+
53+
test('absolute path, too long directory name', () {
54+
final path = p.join(tmp, 'd' * 256);
55+
56+
expect(
57+
() => fs.createDirectory(path),
58+
throwsA(
59+
isA<IOFileException>()
60+
.having((e) => e.path1, 'path1', path)
61+
.having(
62+
(e) => e.errorCode,
63+
'errorCode',
64+
io.Platform.isWindows
65+
? win32.ERROR_INVALID_NAME
66+
: errors.enametoolong,
67+
),
68+
),
69+
);
70+
});
71+
72+
test('relative path, long directory name', () {
73+
// On Windows:
74+
// When using an API to create a directory, the specified path cannot be
75+
// so long that you cannot append an 8.3 file name (that is, the directory
76+
// name cannot exceed MAX_PATH minus 12).
77+
final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
78+
fs.createDirectory(path);
79+
80+
expect(utils.isDirectory(path), isTrue);
81+
});
82+
83+
test('relative path, too long directory name', () {
84+
final path = 'd' * 256;
85+
86+
expect(
87+
() => fs.createDirectory(path),
88+
throwsA(
89+
isA<IOFileException>()
90+
.having((e) => e.path1, 'path1', path)
91+
.having(
92+
(e) => e.errorCode,
93+
'errorCode',
94+
io.Platform.isWindows
95+
? win32.ERROR_INVALID_NAME
96+
: errors.enametoolong,
97+
),
98+
),
99+
);
100+
});
101+
102+
test('create in non-existent directory', () {
103+
final path = '$tmp/foo/dir';
104+
105+
expect(
106+
() => fs.createDirectory(path),
107+
throwsA(
108+
isA<PathNotFoundException>()
109+
.having((e) => e.path1, 'path', path)
110+
.having(
111+
(e) => e.errorCode,
112+
'errorCode',
113+
io.Platform.isWindows
114+
? win32.ERROR_PATH_NOT_FOUND
115+
: errors.enoent,
116+
),
117+
),
118+
);
119+
});
120+
121+
test('create over existing directory', () {
122+
final path = '$tmp/dir';
123+
io.Directory(path).createSync();
124+
125+
expect(
126+
() => fs.createDirectory(path),
127+
throwsA(
128+
isA<PathExistsException>()
129+
.having((e) => e.path1, 'path1', path)
130+
.having(
131+
(e) => e.errorCode,
132+
'errorCode',
133+
io.Platform.isWindows
134+
? win32.ERROR_ALREADY_EXISTS
135+
: errors.eexist,
136+
),
137+
),
138+
);
139+
});
140+
141+
test('create "."', () {
142+
const path = '.';
143+
expect(
144+
() => fs.createDirectory(path),
145+
throwsA(
146+
isA<PathExistsException>()
147+
.having((e) => e.path1, 'path1', path)
148+
.having(
149+
(e) => e.errorCode,
150+
'errorCode',
151+
io.Platform.isWindows
152+
? win32.ERROR_ALREADY_EXISTS
153+
: errors.eexist,
154+
),
155+
),
156+
);
157+
});
158+
159+
test('create ".."', () {
160+
const path = '..';
161+
expect(
162+
() => fs.createDirectory(path),
163+
throwsA(
164+
isA<PathExistsException>()
165+
.having((e) => e.path1, 'path1', path)
166+
.having(
167+
(e) => e.errorCode,
168+
'errorCode',
169+
io.Platform.isWindows
170+
? win32.ERROR_ALREADY_EXISTS
171+
: errors.eexist,
172+
),
173+
),
174+
);
175+
});
176+
177+
test('create over existing file', () {
178+
final path = '$tmp/file';
179+
io.File(path).createSync();
180+
181+
expect(
182+
() => fs.createDirectory(path),
183+
throwsA(
184+
isA<PathExistsException>()
185+
.having((e) => e.path1, 'path1', path)
186+
.having(
187+
(e) => e.errorCode,
188+
'errorCode',
189+
io.Platform.isWindows
190+
? win32.ERROR_ALREADY_EXISTS
191+
: errors.eexist,
192+
),
193+
),
194+
);
195+
});
196+
}
197+
18198
void main() {
19199
group('createDirectory', () {
20-
late String tmp;
21-
late String cwd;
22-
23-
setUp(() {
24-
tmp = createTemp('createDirectory');
25-
cwd = fileSystem.currentDirectory;
26-
fileSystem.currentDirectory = tmp;
27-
});
28-
29-
tearDown(() {
30-
fileSystem.currentDirectory = cwd;
31-
deleteTemp(tmp);
32-
});
33-
34-
test('success', () {
35-
final path = '$tmp/dir';
36-
37-
fileSystem.createDirectory(path);
38-
expect(io.FileSystemEntity.isDirectorySync(path), isTrue);
39-
});
40-
41-
test('absolute path, long directory name', () {
42-
// On Windows:
43-
// When using an API to create a directory, the specified path cannot be
44-
// so long that you cannot append an 8.3 file name (that is, the directory
45-
// name cannot exceed MAX_PATH minus 12).
46-
final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
47-
final path = p.join(tmp, dirname);
48-
49-
fileSystem.createDirectory(path);
50-
expect(io.FileSystemEntity.isDirectorySync(path), isTrue);
51-
});
52-
53-
test('absolute path, too long directory name', () {
54-
final path = p.join(tmp, 'd' * 256);
55-
56-
expect(
57-
() => fileSystem.createDirectory(path),
58-
throwsA(
59-
isA<IOFileException>()
60-
.having((e) => e.path1, 'path1', path)
61-
.having(
62-
(e) => e.errorCode,
63-
'errorCode',
64-
io.Platform.isWindows
65-
? win32.ERROR_INVALID_NAME
66-
: errors.enametoolong,
67-
),
68-
),
69-
);
70-
});
71-
72-
test('relative path, long directory name', () {
73-
// On Windows:
74-
// When using an API to create a directory, the specified path cannot be
75-
// so long that you cannot append an 8.3 file name (that is, the directory
76-
// name cannot exceed MAX_PATH minus 12).
77-
final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
78-
fileSystem.createDirectory(path);
79-
80-
expect(io.FileSystemEntity.isDirectorySync(path), isTrue);
81-
});
82-
83-
test('relative path, too long directory name', () {
84-
final path = 'd' * 256;
85-
86-
expect(
87-
() => fileSystem.createDirectory(path),
88-
throwsA(
89-
isA<IOFileException>()
90-
.having((e) => e.path1, 'path1', path)
91-
.having(
92-
(e) => e.errorCode,
93-
'errorCode',
94-
io.Platform.isWindows
95-
? win32.ERROR_INVALID_NAME
96-
: errors.enametoolong,
97-
),
98-
),
99-
);
100-
});
101-
102-
test('create in non-existent directory', () {
103-
final path = '$tmp/foo/dir';
104-
105-
expect(
106-
() => fileSystem.createDirectory(path),
107-
throwsA(
108-
isA<PathNotFoundException>()
109-
.having((e) => e.path1, 'path', path)
110-
.having(
111-
(e) => e.errorCode,
112-
'errorCode',
113-
io.Platform.isWindows
114-
? win32.ERROR_PATH_NOT_FOUND
115-
: errors.enoent,
116-
),
117-
),
118-
);
119-
});
120-
121-
test('create over existing directory', () {
122-
final path = '$tmp/dir';
123-
io.Directory(path).createSync();
124-
125-
expect(
126-
() => fileSystem.createDirectory(path),
127-
throwsA(
128-
isA<PathExistsException>()
129-
.having((e) => e.path1, 'path1', path)
130-
.having(
131-
(e) => e.errorCode,
132-
'errorCode',
133-
io.Platform.isWindows
134-
? win32.ERROR_ALREADY_EXISTS
135-
: errors.eexist,
136-
),
137-
),
138-
);
139-
});
140-
141-
test('create "."', () {
142-
const path = '.';
143-
expect(
144-
() => fileSystem.createDirectory(path),
145-
throwsA(
146-
isA<PathExistsException>()
147-
.having((e) => e.path1, 'path1', path)
148-
.having(
149-
(e) => e.errorCode,
150-
'errorCode',
151-
io.Platform.isWindows
152-
? win32.ERROR_ALREADY_EXISTS
153-
: errors.eexist,
154-
),
155-
),
156-
);
157-
});
158-
159-
test('create ".."', () {
160-
const path = '..';
161-
expect(
162-
() => fileSystem.createDirectory(path),
163-
throwsA(
164-
isA<PathExistsException>()
165-
.having((e) => e.path1, 'path1', path)
166-
.having(
167-
(e) => e.errorCode,
168-
'errorCode',
169-
io.Platform.isWindows
170-
? win32.ERROR_ALREADY_EXISTS
171-
: errors.eexist,
172-
),
173-
),
174-
);
175-
});
176-
177-
test('create over existing file', () {
178-
final path = '$tmp/file';
179-
io.File(path).createSync();
180-
181-
expect(
182-
() => fileSystem.createDirectory(path),
183-
throwsA(
184-
isA<PathExistsException>()
185-
.having((e) => e.path1, 'path1', path)
186-
.having(
187-
(e) => e.errorCode,
188-
'errorCode',
189-
io.Platform.isWindows
190-
? win32.ERROR_ALREADY_EXISTS
191-
: errors.eexist,
192-
),
193-
),
194-
);
195-
});
200+
group('dart:io verification', () => tests(fileUtils(), fileSystem));
201+
group(
202+
'self verification',
203+
() => tests(FileSystemFileUtils(fileSystem), fileSystem),
204+
);
196205
});
197206
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'dart:io';
2+
3+
import 'test_utils.dart';
4+
5+
/// File utilities implemented using `dart:io`.
6+
///
7+
/// Used to verify `package:io_file` behavior against an external reference.
8+
class DartIOFileUtils implements FileUtils {
9+
@override
10+
String createTestDirectory(String testName) =>
11+
Directory.systemTemp.createTempSync(testName).absolute.path;
12+
13+
@override
14+
void deleteDirectoryTree(String path) =>
15+
Directory(path).deleteSync(recursive: true);
16+
17+
@override
18+
bool isDirectory(String path) => FileSystemEntity.isDirectorySync(path);
19+
20+
@override
21+
void createDirectory(String path) => Directory(path).createSync();
22+
23+
@override
24+
void createTextFile(String path, String s) => File(path).writeAsStringSync(s);
25+
}
26+
27+
FileUtils fileUtils() => DartIOFileUtils();

0 commit comments

Comments
 (0)