create_test.dart 3.86 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 'dart:io';

9
import 'package:args/command_runner.dart';
10
import 'package:flutter_tools/src/cache.dart';
11
import 'package:flutter_tools/src/commands/create.dart';
12
import 'package:flutter_tools/src/dart/sdk.dart';
13
import 'package:path/path.dart' as path;
14 15
import 'package:test/test.dart';

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

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

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

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

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

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    // Verify content and formatting
    testUsingContext('content', () async {
      Cache.flutterRoot = '../..';

      CreateCommand command = new CreateCommand();
      CommandRunner runner = createTestCommandRunner(command);

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

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

          Process process = await Process.start(
              sdkBinaryName('dartfmt'),
              <String>[file.path],
              workingDirectory: temp.path,
          );
          String formatted =
            await process.stdout.transform(UTF8.decoder).join();

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

71 72
    // Verify that we can regenerate over an existing project.
    testUsingContext('can re-gen over existing project', () async {
73
      Cache.flutterRoot = '../..';
74

75
      CreateCommand command = new CreateCommand();
76
      CommandRunner runner = createTestCommandRunner(command);
77

78
      int code = await runner.run(<String>['create', '--no-pub', temp.path]);
79
      expect(code, 0);
80 81

      code = await runner.run(<String>['create', '--no-pub', temp.path]);
82
      expect(code, 0);
83 84 85
    });

    // Verify that we fail with an error code when the file exists.
86
    testUsingContext('fails when file exists', () async {
87
      Cache.flutterRoot = '../..';
88
      CreateCommand command = new CreateCommand();
89
      CommandRunner runner = createTestCommandRunner(command);
90 91
      File existingFile = new File("${temp.path.toString()}/bad");
      if (!existingFile.existsSync()) existingFile.createSync();
92
      int code = await runner.run(<String>['create', existingFile.path]);
93
      expect(code, 1);
94
    });
95 96
  });
}
97 98

Future<Null> _createAndAnalyzeProject(Directory dir, List<String> createArgs) async {
99
  Cache.flutterRoot = '../..';
100
  CreateCommand command = new CreateCommand();
101
  CommandRunner runner = createTestCommandRunner(command);
102 103 104 105
  List<String> args = <String>['create'];
  args.addAll(createArgs);
  args.add(dir.path);
  int code = await runner.run(args);
106
  expect(code, 0);
107 108 109 110 111

  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(
112
    '$dartSdkPath/bin/dart', <String>[flutterToolsPath, 'analyze'],
113 114 115 116 117 118 119 120
    workingDirectory: dir.path
  );
  if (exec.exitCode != 0) {
    print(exec.stdout);
    print(exec.stderr);
  }
  expect(exec.exitCode, 0);
}