3e73e2fed4
Avoid constructing file URIs and paths directly from the host platform when analyzer code is operating on an injected ResourceProvider. Those direct conversions use the process path context, which is wrong when the provider is using a Windows path context on another host platform. Route file URI conversions through ResourceProvider, FileSource, File, and PathContext APIs instead. This keeps file names, document links, analysis options includes, navigation targets, and relative paths aligned with the active provider. Update affected tests and shared test utilities to use provider-backed URIs and paths, so they exercise the same path semantics as production code. Change-Id: Iaf7751d028273a5fa72a04d8ca60e51aee700594 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/499481 Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
134 lines
4.5 KiB
Dart
134 lines
4.5 KiB
Dart
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:analyzer/file_system/file_system.dart';
|
|
import 'package:analyzer/source/file_source.dart';
|
|
import 'package:analyzer/src/generated/utilities_dart.dart';
|
|
import 'package:analyzer/src/utilities/extensions/source.dart';
|
|
import 'package:analyzer_testing/resource_provider_mixin.dart';
|
|
import 'package:path/path.dart' as path;
|
|
import 'package:test/test.dart';
|
|
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
|
|
|
main() {
|
|
defineReflectiveSuite(() {
|
|
defineReflectiveTests(FileSourceTest);
|
|
});
|
|
}
|
|
|
|
@reflectiveTest
|
|
class FileSourceTest with ResourceProviderMixin {
|
|
bool get _isWindowsPathContext =>
|
|
resourceProvider.pathContext.style == path.windows.style;
|
|
|
|
void test_contents() {
|
|
File file1 = getFile("/foo.txt");
|
|
file1.writeAsStringSync('test');
|
|
FileSource source1 = FileSource(file1);
|
|
expect(source1.contents.data, 'test');
|
|
expect(source1.stringContents, 'test');
|
|
}
|
|
|
|
void test_equals_false_differentFiles() {
|
|
File file1 = getFile("/does/not/exist1.dart");
|
|
File file2 = getFile("/does/not/exist2.dart");
|
|
FileSource source1 = FileSource(file1);
|
|
FileSource source2 = FileSource(file2);
|
|
expect(source1 == source2, isFalse);
|
|
}
|
|
|
|
void test_equals_false_null() {
|
|
File file = getFile("/does/not/exist1.dart");
|
|
FileSource source1 = FileSource(file);
|
|
expect(source1, isNotNull);
|
|
}
|
|
|
|
void test_equals_true() {
|
|
File file1 = getFile("/does/not/exist.dart");
|
|
File file2 = getFile("/does/not/exist.dart");
|
|
FileSource source1 = FileSource(file1);
|
|
FileSource source2 = FileSource(file2);
|
|
expect(source1 == source2, isTrue);
|
|
}
|
|
|
|
void test_getFullName() {
|
|
File file = getFile("/does/not/exist.dart");
|
|
FileSource source = FileSource(file);
|
|
expect(source.fullName, file.path);
|
|
}
|
|
|
|
void test_getShortName() {
|
|
File file = getFile("/does/not/exist.dart");
|
|
FileSource source = FileSource(file);
|
|
expect(source.shortName, "exist.dart");
|
|
}
|
|
|
|
void test_hashCode() {
|
|
File file1 = getFile("/does/not/exist.dart");
|
|
File file2 = getFile("/does/not/exist.dart");
|
|
FileSource source1 = FileSource(file1);
|
|
FileSource source2 = FileSource(file2);
|
|
expect(source2.hashCode, source1.hashCode);
|
|
}
|
|
|
|
void test_issue14500() {
|
|
// see https://code.google.com/p/dart/issues/detail?id=14500
|
|
FileSource source = FileSource(getFile("/some/packages/foo:bar.dart"));
|
|
expect(source, isNotNull);
|
|
expect(source.exists(), isFalse);
|
|
}
|
|
|
|
void test_resolveRelative_file_fileName() {
|
|
if (_isWindowsPathContext) {
|
|
// On Windows, the URI that is produced includes a drive letter,
|
|
// which I believe is not consistent across all machines that might run
|
|
// this test.
|
|
return;
|
|
}
|
|
File file = getFile("/a/b/test.dart");
|
|
FileSource source = FileSource(file);
|
|
expect(source, isNotNull);
|
|
Uri relative = resolveRelativeUri(source.uri, Uri.parse("lib.dart"));
|
|
expect(relative, isNotNull);
|
|
expect(relative.toString(), "file:///a/b/lib.dart");
|
|
}
|
|
|
|
void test_resolveRelative_file_filePath() {
|
|
if (_isWindowsPathContext) {
|
|
// On Windows, the URI that is produced includes a drive letter,
|
|
// which I believe is not consistent across all machines that might run
|
|
// this test.
|
|
return;
|
|
}
|
|
File file = getFile("/a/b/test.dart");
|
|
FileSource source = FileSource(file);
|
|
expect(source, isNotNull);
|
|
Uri relative = resolveRelativeUri(source.uri, Uri.parse("c/lib.dart"));
|
|
expect(relative, isNotNull);
|
|
expect(relative.toString(), "file:///a/b/c/lib.dart");
|
|
}
|
|
|
|
void test_resolveRelative_file_filePathWithParent() {
|
|
if (_isWindowsPathContext) {
|
|
// On Windows, the URI that is produced includes a drive letter, which I
|
|
// believe is not consistent across all machines that might run this test.
|
|
return;
|
|
}
|
|
File file = getFile("/a/b/test.dart");
|
|
FileSource source = FileSource(file);
|
|
expect(source, isNotNull);
|
|
Uri relative = resolveRelativeUri(source.uri, Uri.parse("../c/lib.dart"));
|
|
expect(relative, isNotNull);
|
|
expect(relative.toString(), "file:///a/c/lib.dart");
|
|
}
|
|
|
|
void test_system() {
|
|
File file = getFile("/does/not/exist.dart");
|
|
FileSource source = FileSource(file, Uri.parse("dart:core"));
|
|
expect(source, isNotNull);
|
|
expect(source.fullName, file.path);
|
|
expect(source.uri.toString(), 'dart:core');
|
|
}
|
|
}
|