create_test.dart 2.89 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6 7
import 'dart:io';

8
import 'package:args/command_runner.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10 11
import 'package:flutter_tools/src/commands/create.dart';
import 'package:path/path.dart' as path;
12 13
import 'package:test/test.dart';

14
import 'src/context.dart';
15

16
void main() {
17
  group('create', () {
18 19 20
    Directory temp;

    setUp(() {
21
      temp = Directory.systemTemp.createTempSync('flutter_tools');
22 23 24 25 26 27
    });

    tearDown(() {
      temp.deleteSync(recursive: true);
    });

Devon Carew's avatar
Devon Carew committed
28
    // Verify that we create a project that is well-formed.
29 30 31
    testUsingContext('project', () async {
      return _createAndAnalyzeProject(temp, <String>[]);
    });
Devon Carew's avatar
Devon Carew committed
32

33 34 35
    testUsingContext('project with-driver-test', () async {
      return _createAndAnalyzeProject(temp, <String>['--with-driver-test']);
    });
36

37 38
    // Verify that we can regenerate over an existing project.
    testUsingContext('can re-gen over existing project', () async {
39 40 41 42
      ArtifactStore.flutterRoot = '../..';
      CreateCommand command = new CreateCommand();
      CommandRunner runner = new CommandRunner('test_flutter', '')
        ..addCommand(command);
43

44 45 46 47 48
      int code = await runner.run(<String>['create', '--no-pub', temp.path]);
      expect(code, equals(0));

      code = await runner.run(<String>['create', '--no-pub', temp.path]);
      expect(code, equals(0));
49 50 51
    });

    // Verify that we fail with an error code when the file exists.
52
    testUsingContext('fails when file exists', () async {
53 54 55 56 57 58
      ArtifactStore.flutterRoot = '../..';
      CreateCommand command = new CreateCommand();
      CommandRunner runner = new CommandRunner('test_flutter', '')
        ..addCommand(command);
      File existingFile = new File("${temp.path.toString()}/bad");
      if (!existingFile.existsSync()) existingFile.createSync();
59 60
      int code = await runner.run(<String>['create', existingFile.path]);
      expect(code, equals(1));
61
    });
62 63
  });
}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

Future<Null> _createAndAnalyzeProject(Directory dir, List<String> createArgs) async {
  ArtifactStore.flutterRoot = '../..';
  CreateCommand command = new CreateCommand();
  CommandRunner runner = new CommandRunner('test_flutter', '')
    ..addCommand(command);
  List<String> args = <String>['create'];
  args.addAll(createArgs);
  args.add(dir.path);
  int code = await runner.run(args);
  expect(code, equals(0));

  String mainPath = path.join(dir.path, 'lib', 'main.dart');
  expect(new File(mainPath).existsSync(), true);
  String flutterToolsPath = path.absolute(path.join('bin', 'flutter_tools.dart'));
  ProcessResult exec = Process.runSync(
    'dart', <String>[flutterToolsPath, 'analyze'],
    workingDirectory: dir.path
  );
  if (exec.exitCode != 0) {
    print(exec.stdout);
    print(exec.stderr);
  }
  expect(exec.exitCode, 0);
}