format_test.dart 1.59 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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.

import 'dart:async';

import 'package:args/command_runner.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9 10 11 12 13 14 15 16 17 18 19 20 21 22
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/create.dart';
import 'package:flutter_tools/src/commands/format.dart';
import 'package:test/test.dart';

import 'src/common.dart';
import 'src/context.dart';

void main() {
  group('format', () {
    Directory temp;

    setUp(() {
      Cache.disableLocking();
23
      temp = fs.systemTempDirectory.createTempSync('flutter_tools');
24 25 26 27 28 29 30
    });

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

    Future<Null> createProject() async {
31 32
      final CreateCommand command = new CreateCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
33

34
      await runner.run(<String>['create', '--no-pub', temp.path]);
35 36 37 38 39
    }

    testUsingContext('a file', () async {
      await createProject();

40 41
      final File srcFile = fs.file(fs.path.join(temp.path, 'lib', 'main.dart'));
      final String original = srcFile.readAsStringSync();
42 43
      srcFile.writeAsStringSync(original.replaceFirst('main()', 'main(  )'));

44 45
      final FormatCommand command = new FormatCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
46
      await runner.run(<String>['format', srcFile.path]);
47

48
      final String formatted = srcFile.readAsStringSync();
49 50 51 52
      expect(formatted, original);
    });
  });
}