create_test.dart 5.16 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
import 'dart:convert';
7

8
import 'package:args/command_runner.dart';
9
import 'package:flutter_tools/src/base/common.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11
import 'package:flutter_tools/src/base/io.dart';
12
import 'package:flutter_tools/src/cache.dart';
13
import 'package:flutter_tools/src/commands/create.dart';
14
import 'package:flutter_tools/src/dart/sdk.dart';
15 16
import 'package:test/test.dart';

17
import 'src/common.dart';
18
import 'src/context.dart';
19

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

24 25 26 27
    setUpAll(() {
      Cache.disableLocking();
    });

28
    setUp(() {
29
      temp = fs.systemTempDirectory.createTempSync('flutter_tools');
30 31 32 33 34 35
    });

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

Devon Carew's avatar
Devon Carew committed
36
    // Verify that we create a project that is well-formed.
37 38 39
    testUsingContext('project', () async {
      return _createAndAnalyzeProject(temp, <String>[]);
    });
Devon Carew's avatar
Devon Carew committed
40

41 42 43
    testUsingContext('project with-driver-test', () async {
      return _createAndAnalyzeProject(temp, <String>['--with-driver-test']);
    });
44

45 46 47 48
    // Verify content and formatting
    testUsingContext('content', () async {
      Cache.flutterRoot = '../..';

49 50
      final CreateCommand command = new CreateCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
51

52
      await runner.run(<String>['create', '--no-pub', temp.path]);
53 54

      void expectExists(String relPath) {
55
        expect(fs.isFileSync('${temp.path}/$relPath'), true);
56 57 58 59
      }
      expectExists('lib/main.dart');
      for (FileSystemEntity file in temp.listSync(recursive: true)) {
        if (file is File && file.path.endsWith('.dart')) {
60
          final String original= file.readAsStringSync();
61

62
          final Process process = await Process.start(
63 64 65 66
              sdkBinaryName('dartfmt'),
              <String>[file.path],
              workingDirectory: temp.path,
          );
67
          final String formatted =
68 69 70 71 72
            await process.stdout.transform(UTF8.decoder).join();

          expect(original, formatted, reason: file.path);
        }
      }
73 74

      // Generated Xcode settings
75
      final String xcodeConfigPath = fs.path.join('ios', 'Flutter', 'Generated.xcconfig');
76
      expectExists(xcodeConfigPath);
77 78
      final File xcodeConfigFile = fs.file(fs.path.join(temp.path, xcodeConfigPath));
      final String xcodeConfig = xcodeConfigFile.readAsStringSync();
79 80 81
      expect(xcodeConfig, contains('FLUTTER_ROOT='));
      expect(xcodeConfig, contains('FLUTTER_APPLICATION_PATH='));
      expect(xcodeConfig, contains('FLUTTER_FRAMEWORK_DIR='));
82 83
    });

84 85
    // Verify that we can regenerate over an existing project.
    testUsingContext('can re-gen over existing project', () async {
86
      Cache.flutterRoot = '../..';
87

88 89
      final CreateCommand command = new CreateCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
90

91
      await runner.run(<String>['create', '--no-pub', temp.path]);
92

93
      await runner.run(<String>['create', '--no-pub', temp.path]);
94 95
    });

96 97 98 99
    // Verify that we help the user correct an option ordering issue
    testUsingContext('produces sensible error message', () async {
      Cache.flutterRoot = '../..';

100 101
      final CreateCommand command = new CreateCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
102

103 104 105 106 107 108 109
      try {
        await runner.run(<String>['create', temp.path, '--pub']);
        fail('expected ToolExit exception');
      } on ToolExit catch (e) {
        expect(e.exitCode, 2);
        expect(e.message, contains('Try moving --pub'));
      }
110 111
    });

112
    // Verify that we fail with an error code when the file exists.
113
    testUsingContext('fails when file exists', () async {
114
      Cache.flutterRoot = '../..';
115 116 117
      final CreateCommand command = new CreateCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
      final File existingFile = fs.file("${temp.path.toString()}/bad");
118
      if (!existingFile.existsSync()) existingFile.createSync();
119 120 121 122 123 124
      try {
        await runner.run(<String>['create', existingFile.path]);
        fail('expected ToolExit exception');
      } on ToolExit catch (e) {
        expect(e.message, contains('file exists'));
      }
125
    });
126 127
  });
}
128 129

Future<Null> _createAndAnalyzeProject(Directory dir, List<String> createArgs) async {
130
  Cache.flutterRoot = '../..';
131 132 133
  final CreateCommand command = new CreateCommand();
  final CommandRunner<Null> runner = createTestCommandRunner(command);
  final List<String> args = <String>['create'];
134 135
  args.addAll(createArgs);
  args.add(dir.path);
136
  await runner.run(args);
137

138
  final String mainPath = fs.path.join(dir.path, 'lib', 'main.dart');
139
  expect(fs.file(mainPath).existsSync(), true);
140 141
  final String flutterToolsPath = fs.path.absolute(fs.path.join('bin', 'flutter_tools.dart'));
  final ProcessResult exec = Process.runSync(
142
    '$dartSdkPath/bin/dart', <String>[flutterToolsPath, 'analyze'],
143 144 145 146 147 148 149 150
    workingDirectory: dir.path
  );
  if (exec.exitCode != 0) {
    print(exec.stdout);
    print(exec.stderr);
  }
  expect(exec.exitCode, 0);
}