Skip to content

Commit 14983f0

Browse files
author
danrubel
committed
extract resource path test utils
[email protected] Review URL: https://codereview.chromium.org/1777913003 .
1 parent 00b6f98 commit 14983f0

File tree

2 files changed

+131
-113
lines changed

2 files changed

+131
-113
lines changed

pkg/analyzer/test/resource_utils.dart

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
library analyzer.test.resource_utils;
6+
7+
import 'dart:core' hide Resource;
8+
9+
import 'package:analyzer/file_system/file_system.dart';
10+
import 'package:analyzer/file_system/memory_file_system.dart';
11+
import 'package:analyzer/src/util/absolute_path.dart';
12+
import 'package:path/path.dart' as path;
13+
import 'package:unittest/unittest.dart';
14+
15+
16+
bool get isWindows => path.Style.platform == path.Style.windows;
17+
18+
/**
19+
* Assert that the given path is posix and absolute.
20+
*/
21+
void expectAbsolutePosixPath(String posixPath) {
22+
expect(posixPath, startsWith('/'),
23+
reason: 'Expected absolute posix path, but found $posixPath');
24+
}
25+
26+
/**
27+
* Assert that the given path is posix.
28+
*/
29+
void expectPosixPath(String posixPath) {
30+
expect(posixPath.indexOf('\\'), -1,
31+
reason: 'Expected posix path, but found $posixPath');
32+
}
33+
34+
/**
35+
* Translate the given posixPath to a path appropriate for the
36+
* platform on which the tests are executing.
37+
*/
38+
String posixToOSPath(String posixPath) {
39+
expectPosixPath(posixPath);
40+
if (isWindows) {
41+
String windowsPath = posixPath.replaceAll('/', '\\');
42+
if (posixPath.startsWith('/')) {
43+
return 'C:$windowsPath';
44+
}
45+
return windowsPath;
46+
}
47+
return posixPath;
48+
}
49+
50+
/**
51+
* Translate the given posixPath to a file URI appropriate for the
52+
* platform on which the tests are executing.
53+
*/
54+
String posixToOSFileUri(String posixPath) {
55+
expectPosixPath(posixPath);
56+
return isWindows ? 'file:///C:$posixPath' : 'file://$posixPath';
57+
}
58+
59+
/**
60+
* A convenience utility for setting up a test [MemoryResourceProvider].
61+
* All supplied paths are assumed to be in [path.posix] format
62+
* and are automatically translated to [path.context].
63+
*
64+
* This class intentionally does not implement [ResourceProvider]
65+
* directly or indirectly so that it cannot be used as a resource provider.
66+
* We do not want functionality under test to interact with a resource provider
67+
* that automatically translates paths.
68+
*/
69+
class TestPathTranslator {
70+
final MemoryResourceProvider _provider;
71+
72+
TestPathTranslator(this._provider);
73+
74+
Resource getResource(String posixPath) =>
75+
_provider.getResource(posixToOSPath(posixPath));
76+
77+
File newFile(String posixPath, String content) =>
78+
_provider.newFile(posixToOSPath(posixPath), content);
79+
80+
Folder newFolder(String posixPath) =>
81+
_provider.newFolder(posixToOSPath(posixPath));
82+
}
83+
84+
/**
85+
* A resource provider for testing that asserts that any supplied paths
86+
* are appropriate for the OS platform on which the tests are running.
87+
*/
88+
class TestResourceProvider implements ResourceProvider {
89+
final ResourceProvider _provider;
90+
91+
TestResourceProvider(this._provider) {
92+
expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/');
93+
}
94+
95+
@override
96+
AbsolutePathContext get absolutePathContext => _provider.absolutePathContext;
97+
98+
@override
99+
File getFile(String path) => _provider.getFile(_assertPath(path));
100+
101+
@override
102+
Folder getFolder(String path) => _provider.getFolder(_assertPath(path));
103+
104+
@override
105+
Resource getResource(String path) => _provider.getResource(_assertPath(path));
106+
107+
@override
108+
Folder getStateLocation(String pluginId) =>
109+
_provider.getStateLocation(pluginId);
110+
111+
@override
112+
path.Context get pathContext => _provider.pathContext;
113+
114+
/**
115+
* Assert that the given path is valid for the OS platform on which the
116+
* tests are running.
117+
*/
118+
String _assertPath(String path) {
119+
if (isWindows) {
120+
if (path.contains('/')) {
121+
fail('Expected windows path, but found: $path');
122+
}
123+
} else {
124+
if (path.contains('\\')) {
125+
fail('Expected posix path, but found: $path');
126+
}
127+
}
128+
return path;
129+
}
130+
}

pkg/analyzer/test/source/embedder_test.dart

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import 'package:analyzer/file_system/file_system.dart';
1010
import 'package:analyzer/file_system/memory_file_system.dart';
1111
import 'package:analyzer/source/embedder.dart';
1212
import 'package:analyzer/src/generated/source.dart';
13-
import 'package:analyzer/src/util/absolute_path.dart';
1413
import 'package:path/path.dart' as path;
1514
import 'package:unittest/unittest.dart';
1615

16+
import '../resource_utils.dart';
1717
import '../utils.dart';
1818

1919
main() {
@@ -163,115 +163,3 @@ clearResourceProvider() {
163163
resourceProvider = null;
164164
pathTranslator = null;
165165
}
166-
167-
// TODO(danrubel) if this approach works well for running tests
168-
// in a platform specific way, then move all of the following functionality
169-
// into a separate test utility library.
170-
171-
bool get isWindows => path.Style.platform == path.Style.windows;
172-
173-
/**
174-
* Assert that the given path is posix.
175-
*/
176-
void expectAbsolutePosixPath(String posixPath) {
177-
expect(posixPath, startsWith('/'),
178-
reason: 'Expected absolute posix path, but found $posixPath');
179-
}
180-
181-
/**
182-
* Translate the given posixPath to a path appropriate for the
183-
* platform on which the tests are executing.
184-
*/
185-
String posixToOSPath(String posixPath) {
186-
expectAbsolutePosixPath(posixPath);
187-
if (isWindows) {
188-
String windowsPath = posixPath.replaceAll('/', '\\');
189-
if (posixPath.startsWith('/')) {
190-
return 'C:$windowsPath';
191-
}
192-
return windowsPath;
193-
}
194-
return posixPath;
195-
}
196-
197-
/**
198-
* Translate the given posixPath to a file URI appropriate for the
199-
* platform on which the tests are executing.
200-
*/
201-
String posixToOSFileUri(String posixPath) {
202-
expectAbsolutePosixPath(posixPath);
203-
return isWindows ? 'file:///C:$posixPath' : 'file://$posixPath';
204-
}
205-
206-
/**
207-
* A convenience utility for setting up a test [MemoryResourceProvider].
208-
* All supplied paths are assumed to be in [path.posix] format
209-
* and are automatically translated to [path.context].
210-
*
211-
* This class intentionally does not implement [ResourceProvider]
212-
* directly or indirectly so that it cannot be used as a resource provider.
213-
* We do not want functionality under test to interact with a resource provider
214-
* that automatically translates paths.
215-
*/
216-
class TestPathTranslator {
217-
final MemoryResourceProvider _provider;
218-
219-
TestPathTranslator(this._provider);
220-
221-
Resource getResource(String posixPath) =>
222-
_provider.getResource(posixToOSPath(posixPath));
223-
224-
File newFile(String posixPath, String content) =>
225-
_provider.newFile(posixToOSPath(posixPath), content);
226-
227-
Folder newFolder(String posixPath) =>
228-
_provider.newFolder(posixToOSPath(posixPath));
229-
}
230-
231-
/**
232-
* A resource provider for testing that asserts that any supplied paths
233-
* are appropriate for the OS platform on which the tests are running.
234-
*/
235-
class TestResourceProvider implements ResourceProvider {
236-
final ResourceProvider _provider;
237-
238-
TestResourceProvider(this._provider) {
239-
expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/');
240-
}
241-
242-
@override
243-
AbsolutePathContext get absolutePathContext => _provider.absolutePathContext;
244-
245-
@override
246-
File getFile(String path) => _provider.getFile(_assertPath(path));
247-
248-
@override
249-
Folder getFolder(String path) => _provider.getFolder(_assertPath(path));
250-
251-
@override
252-
Resource getResource(String path) => _provider.getResource(_assertPath(path));
253-
254-
@override
255-
Folder getStateLocation(String pluginId) =>
256-
_provider.getStateLocation(pluginId);
257-
258-
@override
259-
path.Context get pathContext => _provider.pathContext;
260-
261-
/**
262-
* Assert that the given path is valid for the OS platform on which the
263-
* tests are running.
264-
*/
265-
String _assertPath(String path) {
266-
if (isWindows) {
267-
if (path.contains('/')) {
268-
fail('Expected windows path, but found: $path');
269-
}
270-
} else {
271-
if (path.contains('\\')) {
272-
fail('Expected posix path, but found: $path');
273-
}
274-
}
275-
return path;
276-
}
277-
}

0 commit comments

Comments
 (0)