create_test.dart 2.82 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/cache.dart';
10
import 'package:flutter_tools/src/commands/create.dart';
11
import 'package:flutter_tools/src/dart/sdk.dart';
12
import 'package:path/path.dart' as path;
13 14
import 'package:test/test.dart';

15
import 'src/common.dart';
16
import 'src/context.dart';
17

18
void main() {
19
  group('create', () {
20 21 22
    Directory temp;

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

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

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

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

39 40
    // Verify that we can regenerate over an existing project.
    testUsingContext('can re-gen over existing project', () async {
41
      Cache.flutterRoot = '../..';
42

43
      CreateCommand command = new CreateCommand();
44
      CommandRunner runner = createTestCommandRunner(command);
45

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

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

    // Verify that we fail with an error code when the file exists.
54
    testUsingContext('fails when file exists', () async {
55
      Cache.flutterRoot = '../..';
56
      CreateCommand command = new CreateCommand();
57
      CommandRunner runner = createTestCommandRunner(command);
58 59
      File existingFile = new File("${temp.path.toString()}/bad");
      if (!existingFile.existsSync()) existingFile.createSync();
60
      int code = await runner.run(<String>['create', existingFile.path]);
61
      expect(code, 1);
62
    });
63 64
  });
}
65 66

Future<Null> _createAndAnalyzeProject(Directory dir, List<String> createArgs) async {
67
  Cache.flutterRoot = '../..';
68
  CreateCommand command = new CreateCommand();
69
  CommandRunner runner = createTestCommandRunner(command);
70 71 72 73
  List<String> args = <String>['create'];
  args.addAll(createArgs);
  args.add(dir.path);
  int code = await runner.run(args);
74
  expect(code, 0);
75 76 77 78 79

  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(
80
    '$dartSdkPath/bin/dart', <String>[flutterToolsPath, 'analyze'],
81 82 83 84 85 86 87 88
    workingDirectory: dir.path
  );
  if (exec.exitCode != 0) {
    print(exec.stdout);
    print(exec.stderr);
  }
  expect(exec.exitCode, 0);
}