Move path ("pathos") to pkg/.
Review URL: https://codereview.chromium.org//11647003
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/path@16297 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/lib/path.dart b/lib/path.dart
new file mode 100644
index 0000000..593396d
--- /dev/null
+++ b/lib/path.dart
@@ -0,0 +1,651 @@
+// Copyright (c) 2012, 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.
+
+/// A comprehensive, cross-platform path manipulation library.
+library path;
+
+import 'dart:io' as io;
+
+/// An internal builder for the current OS so we can provide a straight
+/// functional interface and not require users to create one.
+final _builder = new Builder();
+
+/// Gets the path to the current working directory.
+String get current => new io.Directory.current().path;
+
+/// Gets the path separator for the current platform. On Mac and Linux, this
+/// is `/`. On Windows, it's `\`.
+String get separator => _builder.separator;
+
+/// Converts [path] to an absolute path by resolving it relative to the current
+/// working directory. If [path] is already an absolute path, just returns it.
+///
+/// path.absolute('foo/bar.txt'); // -> /your/current/dir/foo/bar.txt
+String absolute(String path) => join(current, path);
+
+/// Gets the part of [path] after the last separator.
+///
+/// path.basename('path/to/foo.dart'); // -> 'foo.dart'
+/// path.basename('path/to'); // -> 'to'
+String basename(String path) => _builder.basename(path);
+
+/// Gets the part of [path] after the last separator, and without any trailing
+/// file extension.
+///
+/// path.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo'
+String basenameWithoutExtension(String path) =>
+ _builder.basenameWithoutExtension(path);
+
+/// Gets the part of [path] before the last separator.
+///
+/// path.dirname('path/to/foo.dart'); // -> 'path/to'
+/// path.dirname('path/to'); // -> 'to'
+String dirname(String path) => _builder.dirname(path);
+
+/// Gets the file extension of [path]: the portion of [basename] from the last
+/// `.` to the end (including the `.` itself).
+///
+/// path.extension('path/to/foo.dart'); // -> '.dart'
+/// path.extension('path/to/foo'); // -> ''
+/// path.extension('path.to/foo'); // -> ''
+/// path.extension('path/to/foo.dart.js'); // -> '.js'
+///
+/// If the file name starts with a `.`, then that is not considered the
+/// extension:
+///
+/// path.extension('~/.bashrc'); // -> ''
+/// path.extension('~/.notes.txt'); // -> '.txt'
+String extension(String path) => _builder.extension(path);
+
+// TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed.
+/// Returns the root of [path], if it's absolute, or the empty string if it's
+/// relative.
+///
+/// // Unix
+/// path.rootPrefix('path/to/foo'); // -> ''
+/// path.rootPrefix('/path/to/foo'); // -> '/'
+///
+/// // Windows
+/// path.rootPrefix(r'path\to\foo'); // -> ''
+/// path.rootPrefix(r'C:\path\to\foo'); // -> r'C:\'
+String rootPrefix(String path) => _builder.rootPrefix(path);
+
+/// Returns `true` if [path] is an absolute path and `false` if it is a
+/// relative path. On POSIX systems, absolute paths start with a `/` (forward
+/// slash). On Windows, an absolute path starts with `\\`, or a drive letter
+/// followed by `:/` or `:\`.
+bool isAbsolute(String path) => _builder.isAbsolute(path);
+
+/// Returns `true` if [path] is a relative path and `false` if it is absolute.
+/// On POSIX systems, absolute paths start with a `/` (forward slash). On
+/// Windows, an absolute path starts with `\\`, or a drive letter followed by
+/// `:/` or `:\`.
+bool isRelative(String path) => _builder.isRelative(path);
+
+/// Joins the given path parts into a single path using the current platform's
+/// [separator]. Example:
+///
+/// path.join('path', 'to', 'foo'); // -> 'path/to/foo'
+///
+/// If any part ends in a path separator, then a redundant separator will not
+/// be added:
+///
+/// path.join('path/', 'to', 'foo'); // -> 'path/to/foo
+///
+/// If a part is an absolute path, then anything before that will be ignored:
+///
+/// path.join('path', '/to', 'foo'); // -> '/to/foo'
+String join(String part1, [String part2, String part3, String part4,
+ String part5, String part6, String part7, String part8]) =>
+ _builder.join(part1, part2, part3, part4, part5, part6, part7, part8);
+
+// TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed.
+/// Splits [path] into its components using the current platform's [separator].
+///
+/// path.split('path/to/foo'); // -> ['path', 'to', 'foo']
+///
+/// The path will *not* be normalized before splitting.
+///
+/// path.split('path/../foo'); // -> ['path', '..', 'foo']
+///
+/// If [path] is absolute, the root directory will be the first element in the
+/// array. Example:
+///
+/// // Unix
+/// path.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo']
+///
+/// // Windows
+/// path.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo']
+List<String> split(String path) => _builder.split(path);
+
+/// Normalizes [path], simplifying it by handling `..`, and `.`, and
+/// removing redundant path separators whenever possible.
+///
+/// path.normalize('path/./to/..//file.text'); // -> 'path/file.txt'
+String normalize(String path) => _builder.normalize(path);
+
+/// Attempts to convert [path] to an equivalent relative path from the current
+/// directory.
+///
+/// // Given current directory is /root/path:
+/// path.relative('/root/path/a/b.dart'); // -> 'a/b.dart'
+/// path.relative('/root/other.dart'); // -> '../other.dart'
+///
+/// If the [from] argument is passed, [path] is made relative to that instead.
+///
+/// path.relative('/root/path/a/b.dart',
+/// from: '/root/path'); // -> 'a/b.dart'
+/// path.relative('/root/other.dart',
+/// from: '/root/path'); // -> '../other.dart'
+///
+/// Since there is no relative path from one drive letter to another on Windows,
+/// this will return an absolute path in that case.
+///
+/// path.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other'
+String relative(String path, {String from}) =>
+ _builder.relative(path, from: from);
+
+/// Removes a trailing extension from the last part of [path].
+///
+/// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo'
+String withoutExtension(String path) => _builder.withoutExtension(path);
+
+/// An instantiable class for manipulating paths. Unlike the top-level
+/// functions, this lets you explicitly select what platform the paths will use.
+class Builder {
+ /// Creates a new path builder for the given style and root directory.
+ ///
+ /// If [style] is omitted, it uses the host operating system's path style. If
+ /// [root] is omitted, it defaults to the current working directory. If [root]
+ /// is relative, it is considered relative to the current working directory.
+ factory Builder({Style style, String root}) {
+ if (style == null) {
+ if (io.Platform.operatingSystem == 'windows') {
+ style = Style.windows;
+ } else {
+ style = Style.posix;
+ }
+ }
+
+ if (root == null) root = current;
+
+ return new Builder._(style, root);
+ }
+
+ Builder._(this.style, this.root);
+
+ /// The style of path that this builder works with.
+ final Style style;
+
+ /// The root directory that relative paths will be relative to.
+ final String root;
+
+ /// Gets the path separator for the builder's [style]. On Mac and Linux,
+ /// this is `/`. On Windows, it's `\`.
+ String get separator => style.separator;
+
+ /// Gets the part of [path] after the last separator on the builder's
+ /// platform.
+ ///
+ /// builder.basename('path/to/foo.dart'); // -> 'foo.dart'
+ /// builder.basename('path/to'); // -> 'to'
+ String basename(String path) => _parse(path).basename;
+
+ /// Gets the part of [path] after the last separator on the builder's
+ /// platform, and without any trailing file extension.
+ ///
+ /// builder.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo'
+ String basenameWithoutExtension(String path) =>
+ _parse(path).basenameWithoutExtension;
+
+ /// Gets the part of [path] before the last separator.
+ ///
+ /// builder.dirname('path/to/foo.dart'); // -> 'path/to'
+ /// builder.dirname('path/to'); // -> 'to'
+ String dirname(String path) {
+ var parsed = _parse(path);
+ if (parsed.parts.isEmpty) return parsed.root == null ? '.' : parsed.root;
+ if (!parsed.hasTrailingSeparator) {
+ if (parsed.parts.length == 1) {
+ return parsed.root == null ? '.' : parsed.root;
+ }
+ parsed.parts.removeLast();
+ parsed.separators.removeLast();
+ }
+ parsed.separators[parsed.separators.length - 1] = '';
+ return parsed.toString();
+ }
+
+ /// Gets the file extension of [path]: the portion of [basename] from the last
+ /// `.` to the end (including the `.` itself).
+ ///
+ /// builder.extension('path/to/foo.dart'); // -> '.dart'
+ /// builder.extension('path/to/foo'); // -> ''
+ /// builder.extension('path.to/foo'); // -> ''
+ /// builder.extension('path/to/foo.dart.js'); // -> '.js'
+ ///
+ /// If the file name starts with a `.`, then it is not considered an
+ /// extension:
+ ///
+ /// builder.extension('~/.bashrc'); // -> ''
+ /// builder.extension('~/.notes.txt'); // -> '.txt'
+ String extension(String path) => _parse(path).extension;
+
+ // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed.
+ /// Returns the root of [path], if it's absolute, or an empty string if it's
+ /// relative.
+ ///
+ /// // Unix
+ /// builder.rootPrefix('path/to/foo'); // -> ''
+ /// builder.rootPrefix('/path/to/foo'); // -> '/'
+ ///
+ /// // Windows
+ /// builder.rootPrefix(r'path\to\foo'); // -> ''
+ /// builder.rootPrefix(r'C:\path\to\foo'); // -> r'C:\'
+ String rootPrefix(String path) {
+ var root = _parse(path).root;
+ return root == null ? '' : root;
+ }
+
+ /// Returns `true` if [path] is an absolute path and `false` if it is a
+ /// relative path. On POSIX systems, absolute paths start with a `/` (forward
+ /// slash). On Windows, an absolute path starts with `\\`, or a drive letter
+ /// followed by `:/` or `:\`.
+ bool isAbsolute(String path) => _parse(path).isAbsolute;
+
+ /// Returns `true` if [path] is a relative path and `false` if it is absolute.
+ /// On POSIX systems, absolute paths start with a `/` (forward slash). On
+ /// Windows, an absolute path starts with `\\`, or a drive letter followed by
+ /// `:/` or `:\`.
+ bool isRelative(String path) => !isAbsolute(path);
+
+ /// Joins the given path parts into a single path. Example:
+ ///
+ /// builder.join('path', 'to', 'foo'); // -> 'path/to/foo'
+ ///
+ /// If any part ends in a path separator, then a redundant separator will not
+ /// be added:
+ ///
+ /// builder.join('path/', 'to', 'foo'); // -> 'path/to/foo
+ ///
+ /// If a part is an absolute path, then anything before that will be ignored:
+ ///
+ /// builder.join('path', '/to', 'foo'); // -> '/to/foo'
+ ///
+ String join(String part1, [String part2, String part3, String part4,
+ String part5, String part6, String part7, String part8]) {
+ var buffer = new StringBuffer();
+ var needsSeparator = false;
+
+ var parts = [part1, part2, part3, part4, part5, part6, part7, part8];
+ for (var i = 1; i < parts.length; i++) {
+ if (parts[i] != null && parts[i - 1] == null) {
+ throw new ArgumentError("join(): part ${i - 1} was null, but part $i "
+ "was not.");
+ }
+ }
+
+ for (var part in parts) {
+ if (part == null) continue;
+
+ if (this.isAbsolute(part)) {
+ // An absolute path discards everything before it.
+ buffer.clear();
+ buffer.add(part);
+ } else {
+ if (part.length > 0 && style.separatorPattern.hasMatch(part[0])) {
+ // The part starts with a separator, so we don't need to add one.
+ } else if (needsSeparator) {
+ buffer.add(separator);
+ }
+
+ buffer.add(part);
+ }
+
+ // Unless this part ends with a separator, we'll need to add one before
+ // the next part.
+ needsSeparator = part.length > 0 &&
+ !style.separatorPattern.hasMatch(part[part.length - 1]);
+ }
+
+ return buffer.toString();
+ }
+
+ // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed.
+ /// Splits [path] into its components using the current platform's
+ /// [separator]. Example:
+ ///
+ /// builder.split('path/to/foo'); // -> ['path', 'to', 'foo']
+ ///
+ /// The path will *not* be normalized before splitting.
+ ///
+ /// builder.split('path/../foo'); // -> ['path', '..', 'foo']
+ ///
+ /// If [path] is absolute, the root directory will be the first element in the
+ /// array. Example:
+ ///
+ /// // Unix
+ /// builder.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo']
+ ///
+ /// // Windows
+ /// builder.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo']
+ List<String> split(String path) {
+ var parsed = _parse(path);
+ // Filter out empty parts that exist due to multiple separators in a row.
+ parsed.parts = parsed.parts.filter((part) => part != '');
+ if (parsed.root != null) parsed.parts.insertRange(0, 1, parsed.root);
+ return parsed.parts;
+ }
+
+ /// Normalizes [path], simplifying it by handling `..`, and `.`, and
+ /// removing redundant path separators whenever possible.
+ ///
+ /// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt'
+ String normalize(String path) {
+ if (path == '') return path;
+
+ var parsed = _parse(path);
+ parsed.normalize();
+ return parsed.toString();
+ }
+
+ /// Creates a new path by appending the given path parts to the [root].
+ /// Equivalent to [join()] with [root] as the first argument. Example:
+ ///
+ /// var builder = new Builder(root: 'root');
+ /// builder.resolve('path', 'to', 'foo'); // -> 'root/path/to/foo'
+ String resolve(String part1, [String part2, String part3, String part4,
+ String part5, String part6, String part7]) {
+ if (!?part2) return join(root, part1);
+ if (!?part3) return join(root, part1, part2);
+ if (!?part4) return join(root, part1, part2, part3);
+ if (!?part5) return join(root, part1, part2, part3, part4);
+ if (!?part6) return join(root, part1, part2, part3, part4, part5);
+ if (!?part7) return join(root, part1, part2, part3, part4, part5, part6);
+ return join(root, part1, part2, part3, part4, part5, part6, part7);
+ }
+
+ /// Attempts to convert [path] to an equivalent relative path relative to
+ /// [root].
+ ///
+ /// var builder = new Builder(root: '/root/path');
+ /// builder.relative('/root/path/a/b.dart'); // -> 'a/b.dart'
+ /// builder.relative('/root/other.dart'); // -> '../other.dart'
+ ///
+ /// If the [from] argument is passed, [path] is made relative to that instead.
+ ///
+ /// builder.relative('/root/path/a/b.dart',
+ /// from: '/root/path'); // -> 'a/b.dart'
+ /// builder.relative('/root/other.dart',
+ /// from: '/root/path'); // -> '../other.dart'
+ ///
+ /// Since there is no relative path from one drive letter to another on
+ /// Windows, this will return an absolute path in that case.
+ ///
+ /// builder.relative(r'D:\other', from: r'C:\other'); // -> 'D:\other'
+ ///
+ /// This will also return an absolute path if an absolute [path] is passed to
+ /// a builder with a relative [root].
+ ///
+ /// var builder = new Builder(r'some/relative/path');
+ /// builder.relative(r'/absolute/path'); // -> '/absolute/path'
+ String relative(String path, {String from}) {
+ if (path == '') return '.';
+
+ from = from == null ? root : this.join(root, from);
+
+ // We can't determine the path from a relative path to an absolute path.
+ if (this.isRelative(from) && this.isAbsolute(path)) {
+ return this.normalize(path);
+ }
+
+ // If the given path is relative, resolve it relative to the root of the
+ // builder.
+ if (this.isRelative(path)) path = this.resolve(path);
+
+ // If the path is still relative and `from` is absolute, we're unable to
+ // find a path from `from` to `path`.
+ if (this.isRelative(path) && this.isAbsolute(from)) {
+ throw new ArgumentError('Unable to find a path to "$path" from "$from".');
+ }
+
+ var fromParsed = _parse(from)..normalize();
+ var pathParsed = _parse(path)..normalize();
+
+ // If the root prefixes don't match (for example, different drive letters
+ // on Windows), then there is no relative path, so just return the absolute
+ // one.
+ // TODO(rnystrom): Drive letters are case-insentive on Windows. Should
+ // handle "C:\" and "c:\" being the same root.
+ if (fromParsed.root != pathParsed.root) return pathParsed.toString();
+
+ // Strip off their common prefix.
+ while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 &&
+ fromParsed.parts[0] == pathParsed.parts[0]) {
+ fromParsed.parts.removeAt(0);
+ fromParsed.separators.removeAt(0);
+ pathParsed.parts.removeAt(0);
+ pathParsed.separators.removeAt(0);
+ }
+
+ // If there are any directories left in the root path, we need to walk up
+ // out of them.
+ pathParsed.parts.insertRange(0, fromParsed.parts.length, '..');
+ pathParsed.separators.insertRange(0, fromParsed.parts.length,
+ style.separator);
+
+ // Corner case: the paths completely collapsed.
+ if (pathParsed.parts.length == 0) return '.';
+
+ // Make it relative.
+ pathParsed.root = '';
+ pathParsed.removeTrailingSeparator();
+
+ return pathParsed.toString();
+ }
+
+ /// Removes a trailing extension from the last part of [path].
+ ///
+ /// builder.withoutExtension('path/to/foo.dart'); // -> 'path/to/foo'
+ String withoutExtension(String path) {
+ var parsed = _parse(path);
+ if (parsed.hasTrailingSeparator) return parsed.toString();
+
+ if (!parsed.parts.isEmpty) {
+ parsed.parts[parsed.parts.length - 1] = parsed.basenameWithoutExtension;
+ }
+
+ return parsed.toString();
+ }
+
+ _ParsedPath _parse(String path) {
+ var before = path;
+
+ // Remove the root prefix, if any.
+ var root = style.getRoot(path);
+ if (root != null) path = path.substring(root.length);
+
+ // Split the parts on path separators.
+ var parts = [];
+ var separators = [];
+ var start = 0;
+ for (var match in style.separatorPattern.allMatches(path)) {
+ parts.add(path.substring(start, match.start));
+ separators.add(match[0]);
+ start = match.end;
+ }
+
+ // Add the final part, if any.
+ if (start < path.length) {
+ parts.add(path.substring(start));
+ separators.add('');
+ }
+
+ return new _ParsedPath(style, root, parts, separators);
+ }
+}
+
+/// An enum type describing a "flavor" of path.
+class Style {
+ /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths
+ /// start with "/". Used by UNIX, Linux, Mac OS X, and others.
+ static final posix = new Style._('posix', '/', '/', '/');
+
+ /// Windows paths use "\" (backslash) as separators. Absolute paths start with
+ /// a drive letter followed by a colon (example, "C:") or two backslashes
+ /// ("\\") for UNC paths.
+ // TODO(rnystrom): The UNC root prefix should include the drive name too, not
+ // just the "\\".
+ static final windows = new Style._('windows', '\\', r'[/\\]',
+ r'\\\\|[a-zA-Z]:[/\\]');
+
+ Style._(this.name, this.separator, String separatorPattern,
+ String rootPattern)
+ : separatorPattern = new RegExp(separatorPattern),
+ _rootPattern = new RegExp('^$rootPattern');
+
+ /// The name of this path style. Will be "posix" or "windows".
+ final String name;
+
+ /// The path separator for this style. On POSIX, this is `/`. On Windows,
+ /// it's `\`.
+ final String separator;
+
+ /// The [Pattern] that can be used to match a separator for a path in this
+ /// style. Windows allows both "/" and "\" as path separators even though
+ /// "\" is the canonical one.
+ final Pattern separatorPattern;
+
+ /// The [Pattern] that can be used to match the root prefix of an absolute
+ /// path in this style.
+ final Pattern _rootPattern;
+
+ /// Gets the root prefix of [path] if path is absolute. If [path] is relative,
+ /// returns `null`.
+ String getRoot(String path) {
+ var match = _rootPattern.firstMatch(path);
+ if (match == null) return null;
+ return match[0];
+ }
+
+ String toString() => name;
+}
+
+// TODO(rnystrom): Make this public?
+class _ParsedPath {
+ /// The [Style] that was used to parse this path.
+ Style style;
+
+ /// The absolute root portion of the path, or `null` if the path is relative.
+ /// On POSIX systems, this will be `null` or "/". On Windows, it can be
+ /// `null`, "//" for a UNC path, or something like "C:\" for paths with drive
+ /// letters.
+ String root;
+
+ /// The path-separated parts of the path. All but the last will be
+ /// directories.
+ List<String> parts;
+
+ /// The path separators following each part. The last one will be an empty
+ /// string unless the path ends with a trailing separator.
+ List<String> separators;
+
+ /// The file extension of the last part, or "" if it doesn't have one.
+ String get extension => _splitExtension()[1];
+
+ /// `true` if the path ends with a trailing separator.
+ bool get hasTrailingSeparator {
+ if (separators.length == 0) return false;
+ return separators[separators.length - 1] != '';
+ }
+
+ /// `true` if this is an absolute path.
+ bool get isAbsolute => root != null;
+
+ _ParsedPath(this.style, this.root, this.parts, this.separators);
+
+ String get basename {
+ if (parts.length == 0) return extension;
+ if (hasTrailingSeparator) return '';
+ return parts.last;
+ }
+
+ String get basenameWithoutExtension => _splitExtension()[0];
+
+ void removeTrailingSeparator() {
+ if (separators.length > 0) {
+ separators[separators.length - 1] = '';
+ }
+ }
+
+ void normalize() {
+ // Handle '.', '..', and empty parts.
+ var leadingDoubles = 0;
+ var newParts = [];
+ for (var part in parts) {
+ if (part == '.' || part == '') {
+ // Do nothing. Ignore it.
+ } else if (part == '..') {
+ // Pop the last part off.
+ if (newParts.length > 0) {
+ newParts.removeLast();
+ } else {
+ // Backed out past the beginning, so preserve the "..".
+ leadingDoubles++;
+ }
+ } else {
+ newParts.add(part);
+ }
+ }
+
+ // A relative path can back out from the start directory.
+ if (!isAbsolute) {
+ newParts.insertRange(0, leadingDoubles, '..');
+ }
+
+ // If we collapsed down to nothing, do ".".
+ if (newParts.length == 0 && !isAbsolute) {
+ newParts.add('.');
+ }
+
+ // Canonicalize separators.
+ var newSeparators = [];
+ newSeparators.insertRange(0, newParts.length, style.separator);
+
+ parts = newParts;
+ separators = newSeparators;
+
+ removeTrailingSeparator();
+ }
+
+ String toString() {
+ var builder = new StringBuffer();
+ if (root != null) builder.add(root);
+ for (var i = 0; i < parts.length; i++) {
+ builder.add(parts[i]);
+ builder.add(separators[i]);
+ }
+
+ return builder.toString();
+ }
+
+ /// Splits the last part of the path into a two-element list. The first is
+ /// the name of the file without any extension. The second is the extension
+ /// or "" if it has none.
+ List<String> _splitExtension() {
+ if (parts.isEmpty) return ['', ''];
+ if (hasTrailingSeparator) return ['', ''];
+
+ var file = parts.last;
+ if (file == '..') return ['..', ''];
+
+ var lastDot = file.lastIndexOf('.');
+
+ // If there is no dot, or it's the first character, like '.bashrc', it
+ // doesn't count.
+ if (lastDot <= 0) return [file, ''];
+
+ return [file.substring(0, lastDot), file.substring(lastDot)];
+ }
+}
diff --git a/pubspec.yaml b/pubspec.yaml
new file mode 100644
index 0000000..3372302
--- /dev/null
+++ b/pubspec.yaml
@@ -0,0 +1,10 @@
+name: pathos
+author: "Dart Team <misc@dartlang.org>"
+homepage: http://www.dartlang.org
+description: >
+ A string-based path manipulation library. All of the path operations you know
+ and love, with solid support on both Windows and POSIX (Linux and Mac OS X)
+ machines.
+
+ Currently only runs on the standalone VM, but will run in a browser as soon as
+ configuration-specific code is supported by Dart.
diff --git a/test/path_posix_test.dart b/test/path_posix_test.dart
new file mode 100644
index 0000000..f929c2a
--- /dev/null
+++ b/test/path_posix_test.dart
@@ -0,0 +1,362 @@
+// Copyright (c) 2012, 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.
+
+library path_test;
+
+import 'dart:io' as io;
+
+// TODO(rnystrom): Use "package:" path when #7491 is fixed.
+import '../../unittest/lib/unittest.dart';
+import '../lib/path.dart' as path;
+
+main() {
+ var builder = new path.Builder(style: path.Style.posix, root: '/root/path');
+
+ if (new path.Builder().style == path.Style.posix) {
+ group('absolute', () {
+ expect(path.absolute('a/b.txt'), path.join(path.current, 'a/b.txt'));
+ expect(path.absolute('/a/b.txt'), '/a/b.txt');
+ });
+ }
+
+ test('separator', () {
+ expect(builder.separator, '/');
+ });
+
+ test('extension', () {
+ expect(builder.extension(''), '');
+ expect(builder.extension('foo.dart'), '.dart');
+ expect(builder.extension('foo.dart.js'), '.js');
+ expect(builder.extension('a.b/c'), '');
+ expect(builder.extension('a.b/c.d'), '.d');
+ expect(builder.extension('~/.bashrc'), '');
+ expect(builder.extension(r'a.b\c'), r'.b\c');
+ });
+
+ test('rootPrefix', () {
+ expect(builder.rootPrefix(''), '');
+ expect(builder.rootPrefix('a'), '');
+ expect(builder.rootPrefix('a/b'), '');
+ expect(builder.rootPrefix('/a/c'), '/');
+ expect(builder.rootPrefix('/'), '/');
+ });
+
+ test('dirname', () {
+ expect(builder.dirname(''), '.');
+ expect(builder.dirname('a'), '.');
+ expect(builder.dirname('a/b'), 'a');
+ expect(builder.dirname('a/b/c'), 'a/b');
+ expect(builder.dirname('a/b.c'), 'a');
+ expect(builder.dirname('a/'), 'a');
+ expect(builder.dirname('a/.'), 'a');
+ expect(builder.dirname(r'a\b/c'), r'a\b');
+ expect(builder.dirname('/a'), '/');
+ expect(builder.dirname('/'), '/');
+ expect(builder.dirname('a/b/'), 'a/b');
+ expect(builder.dirname(r'a/b\c'), 'a');
+ expect(builder.dirname('a//'), 'a/');
+ });
+
+ test('basename', () {
+ expect(builder.basename(''), '');
+ expect(builder.basename('a'), 'a');
+ expect(builder.basename('a/b'), 'b');
+ expect(builder.basename('a/b/c'), 'c');
+ expect(builder.basename('a/b.c'), 'b.c');
+ expect(builder.basename('a/'), '');
+ expect(builder.basename('a/.'), '.');
+ expect(builder.basename(r'a\b/c'), 'c');
+ expect(builder.basename('/a'), 'a');
+ // TODO(nweiz): this should actually return '/'
+ expect(builder.basename('/'), '');
+ expect(builder.basename('a/b/'), '');
+ expect(builder.basename(r'a/b\c'), r'b\c');
+ expect(builder.basename('a//'), '');
+ });
+
+ test('basenameWithoutExtension', () {
+ expect(builder.basenameWithoutExtension(''), '');
+ expect(builder.basenameWithoutExtension('a'), 'a');
+ expect(builder.basenameWithoutExtension('a/b'), 'b');
+ expect(builder.basenameWithoutExtension('a/b/c'), 'c');
+ expect(builder.basenameWithoutExtension('a/b.c'), 'b');
+ expect(builder.basenameWithoutExtension('a/'), '');
+ expect(builder.basenameWithoutExtension('a/.'), '.');
+ expect(builder.basenameWithoutExtension(r'a/b\c'), r'b\c');
+ expect(builder.basenameWithoutExtension('a/.bashrc'), '.bashrc');
+ expect(builder.basenameWithoutExtension('a/b/c.d.e'), 'c.d');
+ });
+
+ test('isAbsolute', () {
+ expect(builder.isAbsolute(''), false);
+ expect(builder.isAbsolute('a'), false);
+ expect(builder.isAbsolute('a/b'), false);
+ expect(builder.isAbsolute('/a'), true);
+ expect(builder.isAbsolute('/a/b'), true);
+ expect(builder.isAbsolute('~'), false);
+ expect(builder.isAbsolute('.'), false);
+ expect(builder.isAbsolute('../a'), false);
+ expect(builder.isAbsolute('C:/a'), false);
+ expect(builder.isAbsolute(r'C:\a'), false);
+ expect(builder.isAbsolute(r'\\a'), false);
+ });
+
+ test('isRelative', () {
+ expect(builder.isRelative(''), true);
+ expect(builder.isRelative('a'), true);
+ expect(builder.isRelative('a/b'), true);
+ expect(builder.isRelative('/a'), false);
+ expect(builder.isRelative('/a/b'), false);
+ expect(builder.isRelative('~'), true);
+ expect(builder.isRelative('.'), true);
+ expect(builder.isRelative('../a'), true);
+ expect(builder.isRelative('C:/a'), true);
+ expect(builder.isRelative(r'C:\a'), true);
+ expect(builder.isRelative(r'\\a'), true);
+ });
+
+ group('join', () {
+ test('allows up to eight parts', () {
+ expect(builder.join('a'), 'a');
+ expect(builder.join('a', 'b'), 'a/b');
+ expect(builder.join('a', 'b', 'c'), 'a/b/c');
+ expect(builder.join('a', 'b', 'c', 'd'), 'a/b/c/d');
+ expect(builder.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e');
+ expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f');
+ expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g');
+ expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'),
+ 'a/b/c/d/e/f/g/h');
+ });
+
+ test('does not add separator if a part ends in one', () {
+ expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d');
+ expect(builder.join('a\\', 'b'), r'a\/b');
+ });
+
+ test('ignores parts before an absolute path', () {
+ expect(builder.join('a', '/', 'b', 'c'), '/b/c');
+ expect(builder.join('a', '/b', '/c', 'd'), '/c/d');
+ expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d');
+ expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d');
+ });
+
+ test('ignores trailing nulls', () {
+ expect(builder.join('a', null), equals('a'));
+ expect(builder.join('a', 'b', 'c', null, null), equals('a/b/c'));
+ });
+
+ test('disallows intermediate nulls', () {
+ expect(() => builder.join('a', null, 'b'), throwsArgumentError);
+ expect(() => builder.join(null, 'a'), throwsArgumentError);
+ });
+ });
+
+ group('split', () {
+ test('simple cases', () {
+ expect(builder.split(''), []);
+ expect(builder.split('.'), ['.']);
+ expect(builder.split('..'), ['..']);
+ expect(builder.split('foo'), equals(['foo']));
+ expect(builder.split('foo/bar.txt'), equals(['foo', 'bar.txt']));
+ expect(builder.split('foo/bar/baz'), equals(['foo', 'bar', 'baz']));
+ expect(builder.split('foo/../bar/./baz'),
+ equals(['foo', '..', 'bar', '.', 'baz']));
+ expect(builder.split('foo//bar///baz'), equals(['foo', 'bar', 'baz']));
+ expect(builder.split('foo/\\/baz'), equals(['foo', '\\', 'baz']));
+ expect(builder.split('.'), equals(['.']));
+ expect(builder.split(''), equals([]));
+ expect(builder.split('foo/'), equals(['foo']));
+ expect(builder.split('//'), equals(['/']));
+ });
+
+ test('includes the root for absolute paths', () {
+ expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz']));
+ expect(builder.split('/'), equals(['/']));
+ });
+ });
+
+ group('normalize', () {
+ test('simple cases', () {
+ expect(builder.normalize(''), '');
+ expect(builder.normalize('.'), '.');
+ expect(builder.normalize('..'), '..');
+ expect(builder.normalize('a'), 'a');
+ expect(builder.normalize('/'), '/');
+ expect(builder.normalize(r'\'), r'\');
+ });
+
+ test('collapses redundant separators', () {
+ expect(builder.normalize(r'a/b/c'), r'a/b/c');
+ expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d');
+ });
+
+ test('does not collapse separators for other platform', () {
+ expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c');
+ });
+
+ test('eliminates "." parts', () {
+ expect(builder.normalize('./'), '.');
+ expect(builder.normalize('/.'), '/');
+ expect(builder.normalize('/./'), '/');
+ expect(builder.normalize('./.'), '.');
+ expect(builder.normalize('a/./b'), 'a/b');
+ expect(builder.normalize('a/.b/c'), 'a/.b/c');
+ expect(builder.normalize('a/././b/./c'), 'a/b/c');
+ expect(builder.normalize('././a'), 'a');
+ expect(builder.normalize('a/./.'), 'a');
+ });
+
+ test('eliminates ".." parts', () {
+ expect(builder.normalize('..'), '..');
+ expect(builder.normalize('../'), '..');
+ expect(builder.normalize('../../..'), '../../..');
+ expect(builder.normalize('../../../'), '../../..');
+ expect(builder.normalize('/..'), '/');
+ expect(builder.normalize('/../../..'), '/');
+ expect(builder.normalize('/../../../a'), '/a');
+ expect(builder.normalize('a/..'), '.');
+ expect(builder.normalize('a/b/..'), 'a');
+ expect(builder.normalize('a/../b'), 'b');
+ expect(builder.normalize('a/./../b'), 'b');
+ expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d');
+ expect(builder.normalize('a/b/../../../../c'), '../../c');
+ });
+
+ test('does not walk before root on absolute paths', () {
+ expect(builder.normalize('..'), '..');
+ expect(builder.normalize('../'), '..');
+ expect(builder.normalize('/..'), '/');
+ expect(builder.normalize('a/..'), '.');
+ expect(builder.normalize('a/b/..'), 'a');
+ expect(builder.normalize('a/../b'), 'b');
+ expect(builder.normalize('a/./../b'), 'b');
+ expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d');
+ expect(builder.normalize('a/b/../../../../c'), '../../c');
+ });
+
+ test('removes trailing separators', () {
+ expect(builder.normalize('./'), '.');
+ expect(builder.normalize('.//'), '.');
+ expect(builder.normalize('a/'), 'a');
+ expect(builder.normalize('a/b/'), 'a/b');
+ expect(builder.normalize('a/b///'), 'a/b');
+ });
+ });
+
+ group('relative', () {
+ group('from absolute root', () {
+ test('given absolute path in root', () {
+ expect(builder.relative('/'), '../..');
+ expect(builder.relative('/root'), '..');
+ expect(builder.relative('/root/path'), '.');
+ expect(builder.relative('/root/path/a'), 'a');
+ expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt');
+ expect(builder.relative('/root/a/b.txt'), '../a/b.txt');
+ });
+
+ test('given absolute path outside of root', () {
+ expect(builder.relative('/a/b'), '../../a/b');
+ expect(builder.relative('/root/path/a'), 'a');
+ expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt');
+ expect(builder.relative('/root/a/b.txt'), '../a/b.txt');
+ });
+
+ test('given relative path', () {
+ // The path is considered relative to the root, so it basically just
+ // normalizes.
+ expect(builder.relative(''), '.');
+ expect(builder.relative('.'), '.');
+ expect(builder.relative('a'), 'a');
+ expect(builder.relative('a/b.txt'), 'a/b.txt');
+ expect(builder.relative('../a/b.txt'), '../a/b.txt');
+ expect(builder.relative('a/./b/../c.txt'), 'a/c.txt');
+ });
+ });
+
+ group('from relative root', () {
+ var r = new path.Builder(style: path.Style.posix, root: 'foo/bar');
+
+ test('given absolute path', () {
+ expect(r.relative('/'), equals('/'));
+ expect(r.relative('/a/b'), equals('/a/b'));
+ });
+
+ test('given relative path', () {
+ // The path is considered relative to the root, so it basically just
+ // normalizes.
+ expect(r.relative(''), '.');
+ expect(r.relative('.'), '.');
+ expect(r.relative('..'), '..');
+ expect(r.relative('a'), 'a');
+ expect(r.relative('a/b.txt'), 'a/b.txt');
+ expect(r.relative('../a/b.txt'), '../a/b.txt');
+ expect(r.relative('a/./b/../c.txt'), 'a/c.txt');
+ });
+ });
+
+ test('from a root with extension', () {
+ var r = new path.Builder(style: path.Style.posix, root: '/dir.ext');
+ expect(r.relative('/dir.ext/file'), 'file');
+ });
+
+ test('with a root parameter', () {
+ expect(builder.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
+ expect(builder.relative('..', from: '/foo/bar'), equals('../../root'));
+ expect(builder.relative('/foo/bar/baz', from: 'foo/bar'),
+ equals('../../../../foo/bar/baz'));
+ expect(builder.relative('..', from: 'foo/bar'), equals('../../..'));
+ });
+
+ test('with a root parameter and a relative root', () {
+ var r = new path.Builder(style: path.Style.posix, root: 'relative/root');
+ expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
+ expect(() => r.relative('..', from: '/foo/bar'), throwsArgumentError);
+ expect(r.relative('/foo/bar/baz', from: 'foo/bar'),
+ equals('/foo/bar/baz'));
+ expect(r.relative('..', from: 'foo/bar'), equals('../../..'));
+ });
+ });
+
+ group('resolve', () {
+ test('allows up to seven parts', () {
+ expect(builder.resolve('a'), '/root/path/a');
+ expect(builder.resolve('a', 'b'), '/root/path/a/b');
+ expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c');
+ expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d');
+ expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e');
+ expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'),
+ '/root/path/a/b/c/d/e/f');
+ expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'),
+ '/root/path/a/b/c/d/e/f/g');
+ });
+
+ test('does not add separator if a part ends in one', () {
+ expect(builder.resolve('a/', 'b', 'c/', 'd'), '/root/path/a/b/c/d');
+ expect(builder.resolve(r'a\', 'b'), r'/root/path/a\/b');
+ });
+
+ test('ignores parts before an absolute path', () {
+ expect(builder.resolve('a', '/b', '/c', 'd'), '/c/d');
+ expect(builder.resolve('a', r'c:\b', 'c', 'd'), r'/root/path/a/c:\b/c/d');
+ expect(builder.resolve('a', r'\\b', 'c', 'd'), r'/root/path/a/\\b/c/d');
+ });
+ });
+
+ test('withoutExtension', () {
+ expect(builder.withoutExtension(''), '');
+ expect(builder.withoutExtension('a'), 'a');
+ expect(builder.withoutExtension('.a'), '.a');
+ expect(builder.withoutExtension('a.b'), 'a');
+ expect(builder.withoutExtension('a/b.c'), 'a/b');
+ expect(builder.withoutExtension('a/b.c.d'), 'a/b.c');
+ expect(builder.withoutExtension('a/'), 'a/');
+ expect(builder.withoutExtension('a/b/'), 'a/b/');
+ expect(builder.withoutExtension('a/.'), 'a/.');
+ expect(builder.withoutExtension('a/.b'), 'a/.b');
+ expect(builder.withoutExtension('a.b/c'), 'a.b/c');
+ expect(builder.withoutExtension(r'a.b\c'), r'a');
+ expect(builder.withoutExtension(r'a/b\c'), r'a/b\c');
+ expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c');
+ });
+}
diff --git a/test/path_test.dart b/test/path_test.dart
new file mode 100644
index 0000000..bd099ef
--- /dev/null
+++ b/test/path_test.dart
@@ -0,0 +1,60 @@
+// Copyright (c) 2012, 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.
+
+library all_test;
+
+import 'dart:io' as io;
+
+// TODO(rnystrom): Use "package:" path when #7491 is fixed.
+import '../../unittest/lib/unittest.dart';
+import '../lib/path.dart' as path;
+
+main() {
+ group('path.Style', () {
+ test('name', () {
+ expect(path.Style.posix.name, 'posix');
+ expect(path.Style.windows.name, 'windows');
+ });
+
+ test('separator', () {
+ expect(path.Style.posix.separator, '/');
+ expect(path.Style.windows.separator, '\\');
+ });
+
+ test('toString()', () {
+ expect(path.Style.posix.toString(), 'posix');
+ expect(path.Style.windows.toString(), 'windows');
+ });
+ });
+
+ group('new Builder()', () {
+ test('uses the given root directory', () {
+ var builder = new path.Builder(root: '/a/b/c');
+ expect(builder.root, '/a/b/c');
+ });
+
+ test('uses the given style', () {
+ var builder = new path.Builder(style: path.Style.windows);
+ expect(builder.style, path.Style.windows);
+ });
+
+ test('uses the current working directory if root is omitted', () {
+ var builder = new path.Builder();
+ expect(builder.root, new io.Directory.current().path);
+ });
+
+ test('uses the host OS if style is omitted', () {
+ var builder = new path.Builder();
+ if (io.Platform.operatingSystem == 'windows') {
+ expect(builder.style, path.Style.windows);
+ } else {
+ expect(builder.style, path.Style.posix);
+ }
+ });
+ });
+
+ test('current', () {
+ expect(path.current, new io.Directory.current().path);
+ });
+}
diff --git a/test/path_windows_test.dart b/test/path_windows_test.dart
new file mode 100644
index 0000000..a94d126
--- /dev/null
+++ b/test/path_windows_test.dart
@@ -0,0 +1,396 @@
+// Copyright (c) 2012, 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.
+
+library path_test;
+
+import 'dart:io' as io;
+
+// TODO(rnystrom): Use "package:" path when #7491 is fixed.
+import '../../unittest/lib/unittest.dart';
+import '../lib/path.dart' as path;
+
+main() {
+ var builder = new path.Builder(style: path.Style.windows,
+ root: r'C:\root\path');
+
+ if (new path.Builder().style == path.Style.windows) {
+ group('absolute', () {
+ expect(path.absolute(r'a\b.txt'), path.join(path.current, r'a\b.txt'));
+ expect(path.absolute(r'C:\a\b.txt'), r'C:\a\b.txt');
+ expect(path.absolute(r'\\a\b.txt'), r'\\a\b.txt');
+ });
+ }
+
+ group('separator', () {
+ expect(builder.separator, '\\');
+ });
+
+ test('extension', () {
+ expect(builder.extension(''), '');
+ expect(builder.extension('foo.dart'), '.dart');
+ expect(builder.extension('foo.dart.js'), '.js');
+ expect(builder.extension(r'a.b\c'), '');
+ expect(builder.extension('a.b/c.d'), '.d');
+ expect(builder.extension(r'~\.bashrc'), '');
+ expect(builder.extension(r'a.b/c'), r'');
+ });
+
+ test('rootPrefix', () {
+ expect(builder.rootPrefix(''), '');
+ expect(builder.rootPrefix('a'), '');
+ expect(builder.rootPrefix(r'a\b'), '');
+ expect(builder.rootPrefix(r'C:\a\c'), r'C:\');
+ expect(builder.rootPrefix('C:\\'), r'C:\');
+ expect(builder.rootPrefix('C:/'), 'C:/');
+
+ // TODO(nweiz): enable this once issue 7323 is fixed.
+ // expect(builder.rootPrefix(r'\\server\a\b'), r'\\server\');
+ });
+
+ test('dirname', () {
+ expect(builder.dirname(r''), '.');
+ expect(builder.dirname(r'a'), '.');
+ expect(builder.dirname(r'a\b'), 'a');
+ expect(builder.dirname(r'a\b\c'), r'a\b');
+ expect(builder.dirname(r'a\b.c'), 'a');
+ expect(builder.dirname(r'a\'), 'a');
+ expect(builder.dirname('a/'), 'a');
+ expect(builder.dirname(r'a\.'), 'a');
+ expect(builder.dirname(r'a\b/c'), r'a\b');
+ expect(builder.dirname(r'C:\a'), r'C:\');
+ expect(builder.dirname('C:\\'), r'C:\');
+ expect(builder.dirname(r'a\b\'), r'a\b');
+ expect(builder.dirname(r'a/b\c'), 'a/b');
+ expect(builder.dirname(r'a\\'), r'a\');
+ });
+
+ test('basename', () {
+ expect(builder.basename(r''), '');
+ expect(builder.basename(r'a'), 'a');
+ expect(builder.basename(r'a\b'), 'b');
+ expect(builder.basename(r'a\b\c'), 'c');
+ expect(builder.basename(r'a\b.c'), 'b.c');
+ expect(builder.basename(r'a\'), '');
+ expect(builder.basename(r'a/'), '');
+ expect(builder.basename(r'a\.'), '.');
+ expect(builder.basename(r'a\b/c'), r'c');
+ expect(builder.basename(r'C:\a'), 'a');
+ // TODO(nweiz): this should actually return 'C:\'
+ expect(builder.basename(r'C:\'), '');
+ expect(builder.basename(r'a\b\'), '');
+ expect(builder.basename(r'a/b\c'), 'c');
+ expect(builder.basename(r'a\\'), '');
+ });
+
+ test('basenameWithoutExtension', () {
+ expect(builder.basenameWithoutExtension(''), '');
+ expect(builder.basenameWithoutExtension('a'), 'a');
+ expect(builder.basenameWithoutExtension(r'a\b'), 'b');
+ expect(builder.basenameWithoutExtension(r'a\b\c'), 'c');
+ expect(builder.basenameWithoutExtension(r'a\b.c'), 'b');
+ expect(builder.basenameWithoutExtension(r'a\'), '');
+ expect(builder.basenameWithoutExtension(r'a\.'), '.');
+ expect(builder.basenameWithoutExtension(r'a\b/c'), r'c');
+ expect(builder.basenameWithoutExtension(r'a\.bashrc'), '.bashrc');
+ expect(builder.basenameWithoutExtension(r'a\b\c.d.e'), 'c.d');
+ });
+
+ test('isAbsolute', () {
+ expect(builder.isAbsolute(''), false);
+ expect(builder.isAbsolute('a'), false);
+ expect(builder.isAbsolute(r'a\b'), false);
+ expect(builder.isAbsolute(r'\a'), false);
+ expect(builder.isAbsolute(r'\a\b'), false);
+ expect(builder.isAbsolute('~'), false);
+ expect(builder.isAbsolute('.'), false);
+ expect(builder.isAbsolute(r'..\a'), false);
+ expect(builder.isAbsolute(r'a:/a\b'), true);
+ expect(builder.isAbsolute(r'D:/a/b'), true);
+ expect(builder.isAbsolute(r'c:\'), true);
+ expect(builder.isAbsolute(r'B:\'), true);
+ expect(builder.isAbsolute(r'c:\a'), true);
+ expect(builder.isAbsolute(r'C:\a'), true);
+ expect(builder.isAbsolute(r'\\a'), true);
+ expect(builder.isAbsolute(r'\\'), true);
+ });
+
+ test('isRelative', () {
+ expect(builder.isRelative(''), true);
+ expect(builder.isRelative('a'), true);
+ expect(builder.isRelative(r'a\b'), true);
+ expect(builder.isRelative(r'\a'), true);
+ expect(builder.isRelative(r'\a\b'), true);
+ expect(builder.isRelative('~'), true);
+ expect(builder.isRelative('.'), true);
+ expect(builder.isRelative(r'..\a'), true);
+ expect(builder.isRelative(r'a:/a\b'), false);
+ expect(builder.isRelative(r'D:/a/b'), false);
+ expect(builder.isRelative(r'c:\'), false);
+ expect(builder.isRelative(r'B:\'), false);
+ expect(builder.isRelative(r'c:\a'), false);
+ expect(builder.isRelative(r'C:\a'), false);
+ expect(builder.isRelative(r'\\a'), false);
+ expect(builder.isRelative(r'\\'), false);
+ });
+
+ group('join', () {
+ test('allows up to eight parts', () {
+ expect(builder.join('a'), 'a');
+ expect(builder.join('a', 'b'), r'a\b');
+ expect(builder.join('a', 'b', 'c'), r'a\b\c');
+ expect(builder.join('a', 'b', 'c', 'd'), r'a\b\c\d');
+ expect(builder.join('a', 'b', 'c', 'd', 'e'), r'a\b\c\d\e');
+ expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), r'a\b\c\d\e\f');
+ expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), r'a\b\c\d\e\f\g');
+ expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'),
+ r'a\b\c\d\e\f\g\h');
+ });
+
+ test('does not add separator if a part ends or begins in one', () {
+ expect(builder.join(r'a\', 'b', r'c\', 'd'), r'a\b\c\d');
+ expect(builder.join('a/', 'b'), r'a/b');
+ expect(builder.join('a', '/b'), 'a/b');
+ expect(builder.join('a', r'\b'), r'a\b');
+ });
+
+ test('ignores parts before an absolute path', () {
+ expect(builder.join('a', '/b', '/c', 'd'), r'a/b/c\d');
+ expect(builder.join('a', r'c:\b', 'c', 'd'), r'c:\b\c\d');
+ expect(builder.join('a', r'\\b', r'\\c', 'd'), r'\\c\d');
+ });
+
+ test('ignores trailing nulls', () {
+ expect(builder.join('a', null), equals('a'));
+ expect(builder.join('a', 'b', 'c', null, null), equals(r'a\b\c'));
+ });
+
+ test('disallows intermediate nulls', () {
+ expect(() => builder.join('a', null, 'b'), throwsArgumentError);
+ expect(() => builder.join(null, 'a'), throwsArgumentError);
+ });
+ });
+
+ group('split', () {
+ test('simple cases', () {
+ expect(builder.split(''), []);
+ expect(builder.split('.'), ['.']);
+ expect(builder.split('..'), ['..']);
+ expect(builder.split('foo'), equals(['foo']));
+ expect(builder.split(r'foo\bar.txt'), equals(['foo', 'bar.txt']));
+ expect(builder.split(r'foo\bar/baz'), equals(['foo', 'bar', 'baz']));
+ expect(builder.split(r'foo\..\bar\.\baz'),
+ equals(['foo', '..', 'bar', '.', 'baz']));
+ expect(builder.split(r'foo\\bar\\\baz'), equals(['foo', 'bar', 'baz']));
+ expect(builder.split(r'foo\/\baz'), equals(['foo', 'baz']));
+ expect(builder.split('.'), equals(['.']));
+ expect(builder.split(''), equals([]));
+ expect(builder.split('foo/'), equals(['foo']));
+ expect(builder.split(r'C:\'), equals([r'C:\']));
+ });
+
+ test('includes the root for absolute paths', () {
+ expect(builder.split(r'C:\foo\bar\baz'),
+ equals([r'C:\', 'foo', 'bar', 'baz']));
+ expect(builder.split(r'C:\\'), equals([r'C:\']));
+
+ // TODO(nweiz): enable these once issue 7323 is fixed.
+ // expect(builder.split(r'\\server\foo\bar\baz'),
+ // equals([r'\\server\', 'foo', 'bar', 'baz']));
+ // expect(builder.split(r'\\server\'), equals([r'\\server\']));
+ });
+ });
+
+ group('normalize', () {
+ test('simple cases', () {
+ expect(builder.normalize(''), '');
+ expect(builder.normalize('.'), '.');
+ expect(builder.normalize('..'), '..');
+ expect(builder.normalize('a'), 'a');
+ expect(builder.normalize('C:/'), r'C:/');
+ expect(builder.normalize(r'C:\'), r'C:\');
+ expect(builder.normalize(r'\\'), r'\\');
+ });
+
+ test('collapses redundant separators', () {
+ expect(builder.normalize(r'a\b\c'), r'a\b\c');
+ expect(builder.normalize(r'a\\b\\\c\\\\d'), r'a\b\c\d');
+ });
+
+ test('eliminates "." parts', () {
+ expect(builder.normalize(r'.\'), '.');
+ expect(builder.normalize(r'c:\.'), r'c:\');
+ expect(builder.normalize(r'B:\.\'), r'B:\');
+ expect(builder.normalize(r'\\.'), r'\\');
+ expect(builder.normalize(r'\\.\'), r'\\');
+ expect(builder.normalize(r'.\.'), '.');
+ expect(builder.normalize(r'a\.\b'), r'a\b');
+ expect(builder.normalize(r'a\.b\c'), r'a\.b\c');
+ expect(builder.normalize(r'a\./.\b\.\c'), r'a\b\c');
+ expect(builder.normalize(r'.\./a'), 'a');
+ expect(builder.normalize(r'a/.\.'), 'a');
+ });
+
+ test('eliminates ".." parts', () {
+ expect(builder.normalize('..'), '..');
+ expect(builder.normalize(r'..\'), '..');
+ expect(builder.normalize(r'..\..\..'), r'..\..\..');
+ expect(builder.normalize(r'../..\..\'), r'..\..\..');
+ // TODO(rnystrom): Is this how Python handles absolute paths on Windows?
+ expect(builder.normalize(r'\\..'), r'\\');
+ expect(builder.normalize(r'\\..\..\..'), r'\\');
+ expect(builder.normalize(r'\\..\../..\a'), r'\\a');
+ expect(builder.normalize(r'c:\..'), r'c:\');
+ expect(builder.normalize(r'A:/..\..\..'), r'A:/');
+ expect(builder.normalize(r'b:\..\..\..\a'), r'b:\a');
+ expect(builder.normalize(r'a\..'), '.');
+ expect(builder.normalize(r'a\b\..'), 'a');
+ expect(builder.normalize(r'a\..\b'), 'b');
+ expect(builder.normalize(r'a\.\..\b'), 'b');
+ expect(builder.normalize(r'a\b\c\..\..\d\e\..'), r'a\d');
+ expect(builder.normalize(r'a\b\..\..\..\..\c'), r'..\..\c');
+ });
+
+ test('removes trailing separators', () {
+ expect(builder.normalize(r'.\'), '.');
+ expect(builder.normalize(r'.\\'), '.');
+ expect(builder.normalize(r'a/'), 'a');
+ expect(builder.normalize(r'a\b\'), r'a\b');
+ expect(builder.normalize(r'a\b\\\'), r'a\b');
+ });
+
+ test('normalizes separators', () {
+ expect(builder.normalize(r'a/b\c'), r'a\b\c');
+ });
+ });
+
+ group('relative', () {
+ group('from absolute root', () {
+ test('given absolute path in root', () {
+ expect(builder.relative(r'C:\'), r'..\..');
+ expect(builder.relative(r'C:\root'), '..');
+ expect(builder.relative(r'C:\root\path'), '.');
+ expect(builder.relative(r'C:\root\path\a'), 'a');
+ expect(builder.relative(r'C:\root\path\a\b.txt'), r'a\b.txt');
+ expect(builder.relative(r'C:\root\a\b.txt'), r'..\a\b.txt');
+ });
+
+ test('given absolute path outside of root', () {
+ expect(builder.relative(r'C:\a\b'), r'..\..\a\b');
+ expect(builder.relative(r'C:\root\path\a'), 'a');
+ expect(builder.relative(r'C:\root\path\a\b.txt'), r'a\b.txt');
+ expect(builder.relative(r'C:\root\a\b.txt'), r'..\a\b.txt');
+ });
+
+ test('given absolute path on different drive', () {
+ expect(builder.relative(r'D:\a\b'), r'D:\a\b');
+ });
+
+ test('given relative path', () {
+ // The path is considered relative to the root, so it basically just
+ // normalizes.
+ expect(builder.relative(''), '.');
+ expect(builder.relative('.'), '.');
+ expect(builder.relative('a'), 'a');
+ expect(builder.relative(r'a\b.txt'), r'a\b.txt');
+ expect(builder.relative(r'..\a\b.txt'), r'..\a\b.txt');
+ expect(builder.relative(r'a\.\b\..\c.txt'), r'a\c.txt');
+ });
+ });
+
+ group('from relative root', () {
+ var r = new path.Builder(style: path.Style.windows, root: r'foo\bar');
+
+ test('given absolute path', () {
+ expect(r.relative(r'C:\'), equals(r'C:\'));
+ expect(r.relative(r'C:\a\b'), equals(r'C:\a\b'));
+ });
+
+ test('given relative path', () {
+ // The path is considered relative to the root, so it basically just
+ // normalizes.
+ expect(r.relative(''), '.');
+ expect(r.relative('.'), '.');
+ expect(r.relative('..'), '..');
+ expect(r.relative('a'), 'a');
+ expect(r.relative(r'a\b.txt'), r'a\b.txt');
+ expect(r.relative(r'..\a/b.txt'), r'..\a\b.txt');
+ expect(r.relative(r'a\./b\../c.txt'), r'a\c.txt');
+ });
+ });
+
+ test('from a root with extension', () {
+ var r = new path.Builder(style: path.Style.windows, root: r'C:\dir.ext');
+ expect(r.relative(r'C:\dir.ext\file'), 'file');
+ });
+
+ test('with a root parameter', () {
+ expect(builder.relative(r'C:\foo\bar\baz', from: r'C:\foo\bar'),
+ equals('baz'));
+ expect(builder.relative('..', from: r'C:\foo\bar'),
+ equals(r'..\..\root'));
+ expect(builder.relative('..', from: r'D:\foo\bar'), equals(r'C:\root'));
+ expect(builder.relative(r'C:\foo\bar\baz', from: r'foo\bar'),
+ equals(r'..\..\..\..\foo\bar\baz'));
+ expect(builder.relative('..', from: r'foo\bar'), equals(r'..\..\..'));
+ });
+
+ test('with a root parameter and a relative root', () {
+ var r = new path.Builder(style: path.Style.windows, root: r'relative\root');
+ expect(r.relative(r'C:\foo\bar\baz', from: r'C:\foo\bar'), equals('baz'));
+ expect(() => r.relative('..', from: r'C:\foo\bar'), throwsArgumentError);
+ expect(r.relative(r'C:\foo\bar\baz', from: r'foo\bar'),
+ equals(r'C:\foo\bar\baz'));
+ expect(r.relative('..', from: r'foo\bar'), equals(r'..\..\..'));
+ });
+
+ test('given absolute with different root prefix', () {
+ expect(builder.relative(r'D:\a\b'), r'D:\a\b');
+ expect(builder.relative(r'\\a\b'), r'\\a\b');
+ });
+ });
+
+ group('resolve', () {
+ test('allows up to seven parts', () {
+ expect(builder.resolve('a'), r'C:\root\path\a');
+ expect(builder.resolve('a', 'b'), r'C:\root\path\a\b');
+ expect(builder.resolve('a', 'b', 'c'), r'C:\root\path\a\b\c');
+ expect(builder.resolve('a', 'b', 'c', 'd'), r'C:\root\path\a\b\c\d');
+ expect(builder.resolve('a', 'b', 'c', 'd', 'e'),
+ r'C:\root\path\a\b\c\d\e');
+ expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'),
+ r'C:\root\path\a\b\c\d\e\f');
+ expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'),
+ r'C:\root\path\a\b\c\d\e\f\g');
+ });
+
+ test('does not add separator if a part ends in one', () {
+ expect(builder.resolve(r'a\', 'b', r'c\', 'd'), r'C:\root\path\a\b\c\d');
+ expect(builder.resolve('a/', 'b'), r'C:\root\path\a/b');
+ });
+
+ test('ignores parts before an absolute path', () {
+ expect(builder.resolve('a', '/b', '/c', 'd'), r'C:\root\path\a/b/c\d');
+ expect(builder.resolve('a', r'c:\b', 'c', 'd'), r'c:\b\c\d');
+ expect(builder.resolve('a', r'\\b', r'\\c', 'd'), r'\\c\d');
+ });
+ });
+
+ test('withoutExtension', () {
+ expect(builder.withoutExtension(''), '');
+ expect(builder.withoutExtension('a'), 'a');
+ expect(builder.withoutExtension('.a'), '.a');
+ expect(builder.withoutExtension('a.b'), 'a');
+ expect(builder.withoutExtension(r'a\b.c'), r'a\b');
+ expect(builder.withoutExtension(r'a\b.c.d'), r'a\b.c');
+ expect(builder.withoutExtension(r'a\'), r'a\');
+ expect(builder.withoutExtension(r'a\b\'), r'a\b\');
+ expect(builder.withoutExtension(r'a\.'), r'a\.');
+ expect(builder.withoutExtension(r'a\.b'), r'a\.b');
+ expect(builder.withoutExtension(r'a.b\c'), r'a.b\c');
+ expect(builder.withoutExtension(r'a/b.c/d'), r'a/b.c/d');
+ expect(builder.withoutExtension(r'a\b/c'), r'a\b/c');
+ expect(builder.withoutExtension(r'a\b/c.d'), r'a\b/c');
+ expect(builder.withoutExtension(r'a.b/c'), r'a.b/c');
+ });
+}