Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #46 from dart-lang/sdk_ext_fixes_tests
Browse files Browse the repository at this point in the history
Add some tests for sdk extensions and ".packages" support.
  • Loading branch information
stereotype441 committed Aug 12, 2015
2 parents 4281dbf + d441e97 commit 51e47dc
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.buildlog
.DS_Store
.idea
.packages
/.packages
.project
.pub/
.settings/
Expand Down
2 changes: 2 additions & 0 deletions test/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import 'driver_test.dart' as driver;
import 'error_test.dart' as error;
import 'options_test.dart' as options;
import 'reporter_test.dart' as reporter;
import 'sdk_ext_test.dart' as sdk_ext;
import 'strong_mode_test.dart' as strong_mode;

main() {
driver.main();
error.main();
options.main();
reporter.main();
sdk_ext.main();
strong_mode.main();
}
10 changes: 10 additions & 0 deletions test/data/no_packages_file/sdk_ext_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2015, 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 'dart:foo';

main() {
// import of 'dart:foo' should resolve to a file which defines bar().
bar();
}
3 changes: 3 additions & 0 deletions test/data/package_with_sdk_extension/lib/_sdkext
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dart:foo": "foo.dart"
}
5 changes: 5 additions & 0 deletions test/data/package_with_sdk_extension/lib/foo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2015, 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.

void bar() {}
1 change: 1 addition & 0 deletions test/data/packages_file/.packages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package_with_sdk_extension:../package_with_sdk_extension/lib/
10 changes: 10 additions & 0 deletions test/data/packages_file/sdk_ext_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2015, 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 'dart:foo';

main() {
// import of 'dart:foo' should resolve to a file which defines bar().
bar();
}
60 changes: 60 additions & 0 deletions test/driver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

library analyzer_cli.test.driver;

import 'dart:io';

import 'package:analyzer/plugin/options.dart';
import 'package:analyzer_cli/src/driver.dart';
import 'package:path/path.dart' as path;
import 'package:plugin/plugin.dart';
import 'package:test/test.dart';
import 'package:yaml/src/yaml_node.dart';
Expand All @@ -26,6 +29,63 @@ main() {
expect(processor.exception, isNull);
});
});

group('in temp directory', () {
StringSink savedOutSink, savedErrorSink;
int savedExitCode;
Directory savedCurrentDirectory;
Directory tempDir;
setUp(() {
savedOutSink = outSink;
savedErrorSink = errorSink;
savedExitCode = exitCode;
outSink = new StringBuffer();
errorSink = new StringBuffer();
savedCurrentDirectory = Directory.current;
tempDir = Directory.systemTemp.createTempSync('analyzer_');
});
tearDown(() {
outSink = savedOutSink;
errorSink = savedErrorSink;
exitCode = savedExitCode;
Directory.current = savedCurrentDirectory;
tempDir.deleteSync(recursive: true);
});

test('packages folder', () {
Directory.current = tempDir;
new File(path.join(tempDir.path, 'test.dart')).writeAsStringSync('''
import 'package:foo/bar.dart';
main() {
baz();
}
''');
Directory packagesDir =
new Directory(path.join(tempDir.path, 'packages'));
packagesDir.createSync();
Directory fooDir = new Directory(path.join(packagesDir.path, 'foo'));
fooDir.createSync();
new File(path.join(fooDir.path, 'bar.dart')).writeAsStringSync('''
void baz() {}
''');
new Driver().start(['test.dart']);
expect(exitCode, 0);
});

test('no package resolution', () {
Directory.current = tempDir;
new File(path.join(tempDir.path, 'test.dart')).writeAsStringSync('''
import 'package:path/path.dart';
main() {}
''');
new Driver().start(['test.dart']);
expect(exitCode, 3);
String stdout = outSink.toString();
expect(stdout, contains('[error] Target of URI does not exist'));
expect(stdout, contains('1 error found.'));
expect(errorSink.toString(), '');
});
});
});
}

Expand Down
53 changes: 53 additions & 0 deletions test/sdk_ext_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2015, 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.

/// Test that sdk extensions are properly detected in various scenarios.
library analyzer_cli.test.sdk_ext;

import 'dart:io';

import 'package:analyzer_cli/src/driver.dart' show Driver, errorSink, outSink;
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import 'utils.dart';

main() {
group('Sdk extensions', () {
StringSink savedOutSink, savedErrorSink;
int savedExitCode;
Directory savedCurrentDirectory;
setUp(() {
savedOutSink = outSink;
savedErrorSink = errorSink;
savedExitCode = exitCode;
outSink = new StringBuffer();
errorSink = new StringBuffer();
savedCurrentDirectory = Directory.current;
});
tearDown(() {
outSink = savedOutSink;
errorSink = savedErrorSink;
exitCode = savedExitCode;
Directory.current = savedCurrentDirectory;
});

test('--packages option supplied', () async {
var testDir = path.join(testDirectory, 'data', 'no_packages_file');
Directory.current = new Directory(testDir);
var packagesPath = path.join('..', 'packages_file', '.packages');
new Driver().start(['--packages', packagesPath, 'sdk_ext_user.dart']);

expect(exitCode, 0);
});

test('.packages file present', () async {
var testDir = path.join(testDirectory, 'data', 'packages_file');
Directory.current = new Directory(testDir);
new Driver().start(['sdk_ext_user.dart']);

expect(exitCode, 0);
});
});
}
12 changes: 3 additions & 9 deletions test/strong_mode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
library analyzer_cli.test.strong_mode;

import 'dart:io';
import 'dart:mirrors';

import 'package:analyzer_cli/src/driver.dart' show Driver, errorSink, outSink;
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import 'utils.dart';

/// End-to-end test for --strong checking.
///
/// Most StrongChecker tests are in dev_compiler/test/checker/*_test.dart, but
Expand Down Expand Up @@ -47,11 +49,3 @@ void main() {
});
});
}

/// Gets the test directory in a way that works with
/// package:test and package:unittest.
/// See <https://github.com/dart-lang/test/issues/110> for more info.
final String testDirectory =
path.dirname((reflectClass(_TestUtils).owner as LibraryMirror).uri.path);

class _TestUtils {}
13 changes: 11 additions & 2 deletions test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
library analyzer_cli.test.utils;

import 'dart:io';
import 'dart:mirrors';

import 'package:analyzer/analyzer.dart';
import 'package:path/path.dart' as pathos;
Expand All @@ -24,8 +25,8 @@ String errorsForFile(String contents) {
try {
parseDartFile(path);
} on AnalyzerErrorGroup catch (e) {
return e.toString().replaceAllMapped(new RegExp(
r"^(Error on line \d+ of )((?:[A-Z]+:)?[^:]+): .*$",
return e.toString().replaceAllMapped(
new RegExp(r"^(Error on line \d+ of )((?:[A-Z]+:)?[^:]+): .*$",
multiLine: true),
(match) => match[1] + pathos.basename(match[2]) + ': ...');
}
Expand All @@ -45,3 +46,11 @@ dynamic withTempDir(fn(String path)) {
new Directory(tempDir).deleteSync(recursive: true);
}
}

/// Gets the test directory in a way that works with
/// package:test and package:unittest.
/// See <https://github.com/dart-lang/test/issues/110> for more info.
final String testDirectory = pathos.dirname(
pathos.fromUri((reflectClass(_TestUtils).owner as LibraryMirror).uri));

class _TestUtils {}
4 changes: 3 additions & 1 deletion tool/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ dartanalyzer --fatal-warnings \
test/all.dart

# Run the tests.
pub run test
# Note: the "-j1" is necessary because some tests temporarily change the
# working directory, and the working directory state is shared across isolates.
pub run test -j1

# Install dart_coveralls; gather and send coverage data.
if [ "$COVERALLS_TOKEN" ]; then
Expand Down

0 comments on commit 51e47dc

Please sign in to comment.