diff --git a/test/validate_templates.dart b/test/validate_templates.dart new file mode 100644 index 00000000..6b928098 --- /dev/null +++ b/test/validate_templates.dart @@ -0,0 +1,101 @@ +// Copyright (c) 2014, Google Inc. 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. + +@TestOn('vm') + +// This is explicitly not named with _test.dart extension so it is not run as +// part of the normal test process +library stagehand.test.validate_templates; + +import 'dart:io'; + +import 'package:grinder/grinder.dart'; +import 'package:path/path.dart' as path; +import 'package:stagehand/stagehand.dart' as stagehand; +import 'package:test/test.dart'; +import 'package:yaml/yaml.dart' as yaml; + +void main() { + Directory dir; + + setUp(() async { + dir = await Directory.systemTemp.createTemp('stagehand.test.'); + }); + + tearDown(() async { + if (dir != null) { + await dir.delete(recursive: true); + } + }); + + for (stagehand.Generator generator in stagehand.generators) { + test(generator.id, () { + _testGenerator(generator, dir); + }); + } +} + +void _testGenerator(stagehand.Generator generator, Directory tempDir) { + Dart.run(path.join(path.current, 'bin/stagehand.dart'), + arguments: ['--mock-analytics', generator.id], + workingDirectory: tempDir.path); + + var pubspecPath = path.join(tempDir.path, 'pubspec.yaml'); + var pubspecFile = new File(pubspecPath); + + if (!pubspecFile.existsSync()) { + throw 'A pubspec much be defined!'; + } + + run('pub', arguments: ['get'], workingDirectory: tempDir.path); + + var filePath = path.join(tempDir.path, generator.entrypoint.path); + + if (path.extension(filePath) != '.dart' || + !FileSystemEntity.isFileSync(filePath)) { + var parent = new Directory(path.dirname(filePath)); + + var file = _listSync(parent).firstWhere((f) => f.path.endsWith('.dart'), + orElse: () => null); + + if (file == null) { + filePath = null; + } else { + filePath = file.path; + } + } + + // Run the analyzer. + if (filePath != null) { + String packagesDir = path.join(tempDir.path, 'packages'); + + // TODO: We should be able to pass a cwd into `analyzePath`. + Analyzer.analyze(filePath, + fatalWarnings: true, packageRoot: new Directory(packagesDir)); + } + + // + // Run package tests, if test is included + // + var pubspecContent = yaml.loadYaml(pubspecFile.readAsStringSync()); + var devDeps = pubspecContent['dev_dependencies']; + if (devDeps != null) { + if (devDeps.containsKey('test')) { + run('pub', arguments: ['run', 'test'], workingDirectory: tempDir.path); + } + } +} + +/** + * Return the list of children for the given directory. This list is normalized + * (by sorting on the file path) in order to prevent large merge diffs in the + * generated template data files. + */ +List _listSync(Directory dir, + {bool recursive: false, bool followLinks: true}) { + List results = + dir.listSync(recursive: recursive, followLinks: followLinks); + results.sort((entity1, entity2) => entity1.path.compareTo(entity2.path)); + return results; +} diff --git a/tool/grind.dart b/tool/grind.dart index a6b4565b..905cecc7 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -9,7 +9,6 @@ import 'package:ghpages_generator/ghpages_generator.dart' as ghpages; import 'package:grinder/grinder.dart'; import 'package:path/path.dart' as path; import 'package:stagehand/stagehand.dart' as stagehand; -import 'package:yaml/yaml.dart' as yaml; final RegExp _binaryFileTypes = new RegExp( r'\.(jpe?g|png|gif|ico|svg|ttf|eot|woff|woff2)$', caseSensitive: false); @@ -61,61 +60,7 @@ void updateGhPages() { @Task('Run each generator and analyze the output') void test() { - for (stagehand.Generator generator in stagehand.generators) { - Directory dir = Directory.systemTemp.createTempSync('stagehand.test.'); - try { - _testGenerator(generator, dir); - } finally { - dir.deleteSync(recursive: true); - } - } -} - -void _testGenerator(stagehand.Generator generator, Directory tempDir) { - log(''); - log('${generator.id} template:'); - - Dart.run(path.join(path.current, 'bin/stagehand.dart'), - arguments: ['--mock-analytics', generator.id], - workingDirectory: tempDir.path); - - var pubspecPath = path.join(tempDir.path, 'pubspec.yaml'); - var pubspecFile = new File(pubspecPath); - - if (!pubspecFile.existsSync()) { - throw 'A pubspec much be defined!'; - } - - run('pub', arguments: ['get'], workingDirectory: tempDir.path); - - var filePath = path.join(tempDir.path, generator.entrypoint.path); - - if (path.extension(filePath) != '.dart' || - !FileSystemEntity.isFileSync(filePath)) { - var parent = new Directory(path.dirname(filePath)); - - var file = _listSync(parent).firstWhere((f) => f.path.endsWith('.dart'), - orElse: () => null); - - if (file == null) { - filePath = null; - } else { - filePath = file.path; - } - } - - // Run the analyzer. - if (filePath != null) { - Analyzer.analyze(filePath, fatalWarnings: true, - packageRoot: new Directory(path.join(tempDir.path, 'packages'))); - } - - // Run package tests, if `test` is included. - var pubspecContent = yaml.loadYaml(pubspecFile.readAsStringSync()); - var devDeps = pubspecContent['dev_dependencies']; - if (devDeps != null && devDeps.containsKey('test')) { - new PubApp.local('test').run([], workingDirectory: tempDir.path); - } + new PubApp.local('test').run(['test/validate_templates.dart']); } void _concatenateFiles(Directory src, File target) { diff --git a/tool/travis.sh b/tool/travis.sh index 4126cf28..39b8656b 100755 --- a/tool/travis.sh +++ b/tool/travis.sh @@ -17,7 +17,7 @@ dartanalyzer --fatal-warnings \ dart test/all.dart # Run all the generators and analyze the generated code. -dart tool/grind.dart test +pub run test test/validate_templates.dart # Install dart_coveralls; gather and send coverage data. if [ "$COVERALLS_TOKEN" ]; then