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

Commit

Permalink
Make the generate validator use the test syntax
Browse files Browse the repository at this point in the history
Runs all generators, even if one fails
Updated travis.sh
  • Loading branch information
kevmoo committed May 13, 2015
1 parent 3ddfec3 commit 1fdead5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 57 deletions.
101 changes: 101 additions & 0 deletions test/validate_templates.dart
Original file line number Diff line number Diff line change
@@ -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<FileSystemEntity> _listSync(Directory dir,
{bool recursive: false, bool followLinks: true}) {
List<FileSystemEntity> results =
dir.listSync(recursive: recursive, followLinks: followLinks);
results.sort((entity1, entity2) => entity1.path.compareTo(entity2.path));
return results;
}
57 changes: 1 addition & 56 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tool/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1fdead5

Please sign in to comment.