|
| 1 | +// Copyright (c) 2014, Google Inc. Please see the AUTHORS file for details. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +@TestOn('vm') |
| 6 | + |
| 7 | +// This is explicitly not named with _test.dart extension so it is not run as |
| 8 | +// part of the normal test process |
| 9 | +library stagehand.test.validate_templates; |
| 10 | + |
| 11 | +import 'dart:io'; |
| 12 | + |
| 13 | +import 'package:grinder/grinder.dart'; |
| 14 | +import 'package:path/path.dart' as path; |
| 15 | +import 'package:stagehand/stagehand.dart' as stagehand; |
| 16 | +import 'package:test/test.dart'; |
| 17 | +import 'package:yaml/yaml.dart' as yaml; |
| 18 | + |
| 19 | +void main() { |
| 20 | + Directory dir; |
| 21 | + |
| 22 | + setUp(() async { |
| 23 | + dir = await Directory.systemTemp.createTemp('stagehand.test.'); |
| 24 | + }); |
| 25 | + |
| 26 | + tearDown(() async { |
| 27 | + if (dir != null) { |
| 28 | + await dir.delete(recursive: true); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + for (stagehand.Generator generator in stagehand.generators) { |
| 33 | + test(generator.id, () { |
| 34 | + _testGenerator(generator, dir); |
| 35 | + }); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void _testGenerator(stagehand.Generator generator, Directory tempDir) { |
| 40 | + Dart.run(path.join(path.current, 'bin/stagehand.dart'), |
| 41 | + arguments: ['--mock-analytics', generator.id], |
| 42 | + workingDirectory: tempDir.path); |
| 43 | + |
| 44 | + var pubspecPath = path.join(tempDir.path, 'pubspec.yaml'); |
| 45 | + var pubspecFile = new File(pubspecPath); |
| 46 | + |
| 47 | + if (!pubspecFile.existsSync()) { |
| 48 | + throw 'A pubspec much be defined!'; |
| 49 | + } |
| 50 | + |
| 51 | + run('pub', arguments: ['get'], workingDirectory: tempDir.path); |
| 52 | + |
| 53 | + var filePath = path.join(tempDir.path, generator.entrypoint.path); |
| 54 | + |
| 55 | + if (path.extension(filePath) != '.dart' || |
| 56 | + !FileSystemEntity.isFileSync(filePath)) { |
| 57 | + var parent = new Directory(path.dirname(filePath)); |
| 58 | + |
| 59 | + var file = _listSync(parent).firstWhere((f) => f.path.endsWith('.dart'), |
| 60 | + orElse: () => null); |
| 61 | + |
| 62 | + if (file == null) { |
| 63 | + filePath = null; |
| 64 | + } else { |
| 65 | + filePath = file.path; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Run the analyzer. |
| 70 | + if (filePath != null) { |
| 71 | + String packagesDir = path.join(tempDir.path, 'packages'); |
| 72 | + |
| 73 | + // TODO: We should be able to pass a cwd into `analyzePath`. |
| 74 | + Analyzer.analyze(filePath, |
| 75 | + fatalWarnings: true, packageRoot: new Directory(packagesDir)); |
| 76 | + } |
| 77 | + |
| 78 | + // |
| 79 | + // Run package tests, if test is included |
| 80 | + // |
| 81 | + var pubspecContent = yaml.loadYaml(pubspecFile.readAsStringSync()); |
| 82 | + var devDeps = pubspecContent['dev_dependencies']; |
| 83 | + if (devDeps != null) { |
| 84 | + if (devDeps.containsKey('test')) { |
| 85 | + run('pub', arguments: ['run', 'test'], workingDirectory: tempDir.path); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Return the list of children for the given directory. This list is normalized |
| 92 | + * (by sorting on the file path) in order to prevent large merge diffs in the |
| 93 | + * generated template data files. |
| 94 | + */ |
| 95 | +List<FileSystemEntity> _listSync(Directory dir, |
| 96 | + {bool recursive: false, bool followLinks: true}) { |
| 97 | + List<FileSystemEntity> results = |
| 98 | + dir.listSync(recursive: recursive, followLinks: followLinks); |
| 99 | + results.sort((entity1, entity2) => entity1.path.compareTo(entity2.path)); |
| 100 | + return results; |
| 101 | +} |
0 commit comments