1+ // IoPath also supports equality with String, so we need to ignore unrelated type equality checks in some tests
2+ // ignore_for_file: unrelated_type_equality_checks
3+
4+ import 'dart:io' ;
5+
16import 'package:test/test.dart' ;
27import 'package:grumpy/grumpy.dart' ;
38import 'package:grumpy_io/grumpy_io.dart' ;
@@ -32,7 +37,7 @@ void main() {
3237 final path1 = const IoPath ('test' );
3338 final path2 = const IoPath ('file' );
3439 final combined = path1 + path2;
35- expect (combined.value, 'tesfile ' );
40+ expect (combined.value, 'testfile ' );
3641 });
3742
3843 test ('IoPath / operator' , () {
@@ -41,138 +46,173 @@ void main() {
4146 final combined = path1 / path2;
4247 expect (combined.value, 'test/file' );
4348 });
44- });
45-
46- group ('FsMetadata' , () {});
47- });
48-
49- group ('Services' , () {
50- group ('FileSystemService' , () {
51- final fs = FileSystemService ();
52-
53- test ('writeBytes() creates a file' , () async {
54- final path = const IoPath ('test_file.txt' );
55- final content = 'Hello, World!' ;
56- final result = await fs.writeBytes (
57- path,
58- content.convert.toUtf8Bytes (),
59- );
60- expect (result.isOk, true );
61- final exists = await fs.exists (path);
62- expect (exists.isOk, true );
63- expect (exists.valueOrNull, true );
64- });
65-
66- test ('exists() checks if a file exists' , () async {
67- final path = const IoPath ('test_file.txt' );
68- final exists = await fs.exists (path);
69- expect (exists.isOk, true );
70- expect (exists.valueOrNull, true );
71- });
72-
73- test ('readBytes() reads a file' , () async {
74- final path = const IoPath ('test_file.txt' );
75- final bytes = await fs.readBytes (path);
76-
77- expect (bytes.isOk, true );
78- final content = bytes.valueOrNull! .convert.toUtf8String ();
79- expect (content, 'Hello, World!' );
80- });
81-
82- test ('move() moves a file' , () async {
83- final source = const IoPath ('test_file.txt' );
84- final destination = const IoPath ('moved_test_file.txt' );
85-
86- final result = await fs.move (source, destination);
87- expect (result.isOk, true );
88-
89- final existsSource = await fs.exists (source);
90- final existsDestination = await fs.exists (destination);
91- expect (existsSource.isOk, true );
92- expect (existsSource.valueOrNull, false );
93- expect (existsDestination.isOk, true );
94- expect (existsDestination.valueOrNull, true );
95- });
96-
97- test ('copy() copies a file' , () async {
98- final source = const IoPath ('moved_test_file.txt' );
99- final destination = const IoPath ('copied_test_file.txt' );
10049
101- final result = await fs.copy (source, destination);
102- expect (result.isOk, true );
103-
104- final existsSource = await fs.exists (source);
105- final existsDestination = await fs.exists (destination);
106- expect (existsSource.isOk, true );
107- expect (existsSource.valueOrNull, true );
108- expect (existsDestination.isOk, true );
109- expect (existsDestination.valueOrNull, true );
50+ test ('IoPath == operator (IoPath)' , () {
51+ final path1 = const IoPath ('test' );
52+ final path2 = const IoPath ('test' );
53+ final path3 = const IoPath ('other' );
54+ expect (path1 == path2, true );
55+ expect (path1 == path3, false );
11056 });
11157
112- test ('createDirectory() creates a directory' , () async {
113- final path = const IoPath ('test_directory' );
114-
115- final result = await fs.createDirectory (path);
116- expect (result.isOk, true );
117-
118- final exists = await fs.exists (path);
119- expect (exists.isOk, true );
120- expect (exists.valueOrNull, true );
58+ test ('IoPath == operator (String)' , () {
59+ final path1 = const IoPath ('test' );
60+ expect (path1 == 'test' , true );
61+ expect (path1 == 'other' , false );
12162 });
12263
123- test ('list() lists contents of a directory' , () async {
124- final path = const IoPath ('test_directory' );
125- final copy = const IoPath ('copied_test_file.txt' );
126- final moved = const IoPath ('moved_test_file.txt' );
127-
128- final result1 = await fs.move (copy, path / copy);
129- final result2 = await fs.move (moved, path / moved);
130- expect (result1.isOk, true );
131- expect (result2.isOk, true );
132-
133- final contents = await fs.list (path);
134- expect (contents.isOk, true );
135- expect (contents.valueOrNull, contains (copy));
136- expect (contents.valueOrNull, contains (moved));
64+ test ('IoPath == operator (other types)' , () {
65+ final path1 = const IoPath ('test' );
66+ expect (path1 == 123 , false );
13767 });
68+ });
13869
139- test ('delete() deletes a file' , () async {
140- final path = const IoPath ('test_file.txt' );
141- final content = 'Hello, World!' ;
142-
143- final writeResult = await fs.writeBytes (
144- path,
145- content.convert.toUtf8Bytes (),
146- );
147- expect (writeResult.isOk, true );
148-
149- final deleteResult = await fs.delete (path);
150- expect (deleteResult.isOk, true );
151-
152- final exists = await fs.exists (path);
153- expect (exists.isOk, true );
154- expect (exists.valueOrNull, false );
155- });
70+ group ('FsMetadata' , () {});
71+ });
15672
157- test ('delete() recursively deletes a directory' , () async {
158- final path = const IoPath ('test_directory' );
159- final copy = path / const IoPath ('copied_test_file.txt' );
160- final moved = path / const IoPath ('moved_test_file.txt' );
161-
162- final result = await fs.delete (path, recursive: true );
163- expect (result.isOk, true );
164-
165- final exists = await fs.exists (path);
166- final copyExits = await fs.exists (copy);
167- final movedExists = await fs.exists (moved);
168- expect (exists.isOk, true );
169- expect (exists.valueOrNull, false );
170- expect (copyExits.isOk, true );
171- expect (copyExits.valueOrNull, false );
172- expect (movedExists.isOk, true );
173- expect (movedExists.valueOrNull, false );
174- });
175- });
73+ group ('Services' , () {
74+ group (
75+ 'DefaultFileSystemService' ,
76+ () {
77+ final fs = DefaultFileSystemService ();
78+
79+ final file0 = File ('build/test/files/file0.txt' );
80+ final file1 = File ('build/test/files/file1.txt' );
81+ final file2 = File ('build/test/files/file2.txt' );
82+
83+ final testPath = const IoPath ('build/test/files' );
84+ final filePath0 = testPath / const IoPath ('file0.txt' );
85+ final filePath1 = testPath / const IoPath ('file1.txt' );
86+ final filePath2 = testPath / const IoPath ('file2.txt' );
87+
88+ final Map <File , String ?> requiredFiles = {
89+ file0: 'Hello, World!' ,
90+ file1: null ,
91+ file2: null ,
92+ };
93+
94+ setUp (() async {
95+ Directory ('build/test/files' ).createSync (recursive: true );
96+ for (final entry in requiredFiles.entries) {
97+ if (entry.value != null ) {
98+ await entry.key.writeAsString (entry.value! );
99+ } else {
100+ entry.key.createSync ();
101+ }
102+ }
103+ });
104+
105+ tearDown (() async {
106+ final directory = Directory ('build/test/files' );
107+
108+ if (! directory.existsSync ()) {
109+ return ;
110+ }
111+ directory.deleteSync (recursive: true );
112+ });
113+
114+ test ('writeBytes() creates a file' , () async {
115+ final path = testPath / const IoPath ('file3.txt' );
116+ final content = 'Hello, World!' ;
117+ final result = await fs.writeBytes (
118+ path,
119+ content.convert.toUtf8Bytes (),
120+ );
121+ expect (result.isOk, true );
122+
123+ final file = File (path.value);
124+
125+ expect (file.existsSync (), true );
126+ expect (file.readAsStringSync (), 'Hello, World!' );
127+ });
128+
129+ test ('exists() checks if a file exists' , () async {
130+ final exists = await fs.exists (filePath0);
131+ expect (exists.isOk, true );
132+ expect (exists.valueOrNull, true );
133+ });
134+
135+ test ('readBytes() reads a file' , () async {
136+ final bytes = await fs.readBytes (filePath0);
137+
138+ expect (bytes.isOk, true );
139+ final content = bytes.valueOrNull! .convert.toUtf8String ();
140+ expect (content, requiredFiles[file0]);
141+ });
142+
143+ test ('move() moves a file' , () async {
144+ final source = filePath0;
145+ final destination = testPath / const IoPath ('moved_file0.txt' );
146+
147+ final result = await fs.move (source, destination);
148+ expect (result.isOk, true );
149+
150+ final sourceFile = file0;
151+ final destinationFile = File (destination.value);
152+
153+ expect (sourceFile.existsSync (), false );
154+ expect (destinationFile.existsSync (), true );
155+ expect (destinationFile.readAsStringSync (), requiredFiles[file0]);
156+ });
157+
158+ test ('copy() copies a file' , () async {
159+ final source = filePath0;
160+ final destination = testPath / const IoPath ('copied_file0.txt' );
161+
162+ final result = await fs.copy (source, destination);
163+ expect (result.isOk, true );
164+
165+ final sourceFile = file0;
166+ final destinationFile = File (destination.value);
167+
168+ expect (sourceFile.existsSync (), true );
169+ expect (sourceFile.readAsStringSync (), requiredFiles[file0]);
170+ expect (destinationFile.existsSync (), true );
171+ expect (destinationFile.readAsStringSync (), requiredFiles[file0]);
172+ });
173+
174+ test ('createDirectory() creates a directory' , () async {
175+ final path = testPath / const IoPath ('test_directory' );
176+
177+ final result = await fs.createDirectory (path);
178+ expect (result.isOk, true );
179+
180+ final directory = Directory (path.value);
181+ expect (directory.existsSync (), true );
182+ });
183+
184+ test ('list() lists contents of a directory' , () async {
185+ final contents = await fs.list (testPath);
186+ expect (contents.isOk, true );
187+ expect (contents.valueOrNull, contains (filePath0));
188+ expect (contents.valueOrNull, contains (filePath1));
189+ expect (contents.valueOrNull, contains (filePath2));
190+ });
191+
192+ test ('delete() deletes a file' , () async {
193+ final deleteResult = await fs.delete (filePath0);
194+ expect (deleteResult.isOk, true );
195+
196+ expect (file0.existsSync (), false );
197+ });
198+
199+ test ('delete() recursively deletes a directory' , () async {
200+ final result = await fs.delete (testPath, recursive: true );
201+ expect (result.isOk, true );
202+
203+ final directory = Directory (testPath.value);
204+ expect (directory.existsSync (), false );
205+ });
206+ },
207+ onPlatform: {
208+ 'windows' : const Timeout .factor (2 ),
209+ 'linux' : const Timeout .factor (2 ),
210+ 'mac-os' : const Timeout .factor (2 ),
211+ 'browser' : const Skip (
212+ 'File system operations are not supported on the web' ,
213+ ),
214+ },
215+ );
176216 });
177217 });
178218}
0 commit comments