Update test generator

This commit is contained in:
Tobe O 2018-07-14 18:04:33 -04:00
parent c676bb18fd
commit 00075b07cd

View file

@ -1,14 +1,14 @@
import 'dart:io'; import 'dart:io';
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
import "package:console/console.dart";
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'package:io/ansi.dart';
import 'package:prompts/prompts.dart' as prompter;
import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:recase/recase.dart'; import 'package:recase/recase.dart';
import '../../util.dart';
import 'maker.dart'; import 'maker.dart';
class TestCommand extends Command { class TestCommand extends Command {
final TextPen _pen = new TextPen();
@override @override
String get name => "test"; String get name => "test";
@ -29,13 +29,12 @@ class TestCommand extends Command {
@override @override
run() async { run() async {
var pubspec = await Pubspec.load(Directory.current); var pubspec = await loadPubspec();
String name; String name;
if (argResults.wasParsed('name')) name = argResults['name']; if (argResults.wasParsed('name')) name = argResults['name'] as String;
if (name?.isNotEmpty != true) { if (name?.isNotEmpty != true) {
var p = new Prompter('Name of Test: '); name = prompter.get('Name of test');
name = await p.prompt(checker: (s) => s.isNotEmpty);
} }
List<MakerDependency> deps = [ List<MakerDependency> deps = [
@ -46,7 +45,7 @@ class TestCommand extends Command {
var rc = new ReCase(name); var rc = new ReCase(name);
final testDir = new Directory.fromUri( final testDir = new Directory.fromUri(
Directory.current.uri.resolve(argResults['output-dir'])); Directory.current.uri.resolve(argResults['output-dir'] as String));
final testFile = final testFile =
new File.fromUri(testDir.uri.resolve("${rc.snakeCase}_test.dart")); new File.fromUri(testDir.uri.resolve("${rc.snakeCase}_test.dart"));
if (!await testFile.exists()) await testFile.create(recursive: true); if (!await testFile.exists()) await testFile.create(recursive: true);
@ -55,27 +54,22 @@ class TestCommand extends Command {
if (deps.isNotEmpty) await depend(deps); if (deps.isNotEmpty) await depend(deps);
_pen.green(); print(green.wrap(
_pen( '$checkmark Successfully generated test file "${testFile.absolute.path}".'));
'${Icon.CHECKMARK} Successfully generated test file "${testFile.absolute.path}".');
_pen();
if (argResults['run-configuration']) { if (argResults['run-configuration'] as bool) {
final runConfig = new File.fromUri(Directory.current.uri final runConfig = new File.fromUri(Directory.current.uri
.resolve('.idea/runConfigurations/${name}_Tests.xml')); .resolve('.idea/runConfigurations/${name}_Tests.xml'));
if (!await runConfig.exists()) await runConfig.create(recursive: true); if (!await runConfig.exists()) await runConfig.create(recursive: true);
await runConfig.writeAsString(_generateRunConfiguration(name, rc)); await runConfig.writeAsString(_generateRunConfiguration(name, rc));
_pen.reset(); print(green.wrap(
_pen.green(); '$checkmark Successfully generated run configuration "$name Tests" at "${runConfig.absolute.path}".'));
_pen(
'${Icon.CHECKMARK} Successfully generated run configuration "$name Tests" at "${runConfig.absolute.path}".');
_pen();
} }
} }
_generateRunConfiguration(String name, ReCase rc) { String _generateRunConfiguration(String name, ReCase rc) {
return ''' return '''
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="$name Tests" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true"> <configuration default="false" name="$name Tests" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true">