Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemoryFile.readAsLines behaves differently from File #142

Closed
jamesderlin opened this issue Jun 17, 2020 · 2 comments · Fixed by #147
Closed

MemoryFile.readAsLines behaves differently from File #142

jamesderlin opened this issue Jun 17, 2020 · 2 comments · Fixed by #147

Comments

@jamesderlin
Copy link
Collaborator

MemoryFile.readAsLines/readAsLinesSync behave differently than normal File when there is a trailing newline in the file:

import 'package:file/file.dart';
import 'package:file/memory.dart';

void main() {
  final fs = MemoryFileSystem();
  final f = fs.file('test.txt');
  f.writeAsStringSync('\n');
  final lines = f.readAsLinesSync();
  print('${lines.length}'); // Prints 2.
}

If using LocalFileSystem or dart:io's File instead, readAsLinesSync() will return a List<String> with only one element.

readAsBytes indicates that the file was written correctly, so the problem is in readAsLinesSync.

@jamesderlin
Copy link
Collaborator Author

Yeah, readAsLinesSync just does a naive str.split('\n'). To match dart:io File, it probably should strip a final newline first.

@jamesderlin
Copy link
Collaborator Author

I am working on a fix and a test.

dnfield pushed a commit that referenced this issue Jun 23, 2020
* Fix MemoryFile.readAsLines to behave more like File.readAsLines

Fix `MemoryFile.readAsLines` to behave more like `File.readAsLines`.
A final newline should not add an empty string as the last element of
the returned `List`.

Fixes #142.

* Make RecordingFile.readAsLines/readAsLinesSync always record a final newline

Fixes #146.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant