Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9556a95

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Fix string->bytes encoding in overlay_file_system
Change-Id: I2777c09908172c282f59cb2bf0ed5bd208442dd4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103920 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent fc17a90 commit 9556a95

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

pkg/analyzer/lib/file_system/overlay_file_system.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:convert';
67
import 'dart:core';
78
import 'dart:typed_data';
89

@@ -234,7 +235,7 @@ class _OverlayFile extends _OverlayResource implements File {
234235
Uint8List readAsBytesSync() {
235236
String content = _provider._getOverlayContent(path);
236237
if (content != null) {
237-
return Uint8List.fromList(content.codeUnits);
238+
return utf8.encode(content) as Uint8List;
238239
}
239240
return _file.readAsBytesSync();
240241
}

pkg/analyzer/test/file_system/overlay_file_system_test.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ class FileTest extends OverlayTestSupport {
179179
expect(file.readAsBytesSync(), <int>[98, 98, 98]);
180180
}
181181

182+
test_readAsBytesSync_existing_withOverlay_utf8() {
183+
// Strings should be encoded as UTF8 when they're written, so when we read
184+
// them back as bytes we should see the UTF8-encoded version of the string.
185+
String overlayContent = '\u00e5'; // latin small letter a with ring above
186+
File file =
187+
_file(exists: true, withOverlay: true, overlayContent: overlayContent);
188+
expect(file.readAsBytesSync(), <int>[0xc3, 0xa5]);
189+
}
190+
182191
test_readAsBytesSync_notExisting_withoutOverlay() {
183192
File file = _file(exists: false);
184193
expect(() => file.readAsBytesSync(), throwsA(_isFileSystemException));
@@ -785,7 +794,8 @@ class OverlayTestSupport {
785794
{@required bool exists,
786795
String content,
787796
String path,
788-
bool withOverlay = false}) {
797+
bool withOverlay = false,
798+
String overlayContent = 'bbb'}) {
789799
if (path == null) {
790800
path = defaultFilePath;
791801
} else {
@@ -795,7 +805,7 @@ class OverlayTestSupport {
795805
baseProvider.newFile(path, content ?? 'a');
796806
}
797807
if (withOverlay) {
798-
provider.setOverlay(path, content: 'bbb', modificationStamp: 42);
808+
provider.setOverlay(path, content: overlayContent, modificationStamp: 42);
799809
}
800810
return provider.getFile(path);
801811
}

0 commit comments

Comments
 (0)