format_test.dart 2.72 KB
Newer Older
1 2 3 4 5
// 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 'package:args/command_runner.dart';
6
import 'package:flutter_tools/src/base/file_system.dart';
7 8 9 10
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/format.dart';
import 'package:test/test.dart';

11 12
import '../src/common.dart';
import '../src/context.dart';
13 14 15 16 17 18 19

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

    setUp(() {
      Cache.disableLocking();
20
      temp = fs.systemTempDirectory.createTempSync('flutter_tools');
21 22 23 24 25 26 27
    });

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

    testUsingContext('a file', () async {
28
      final String projectPath = await createProject(temp);
29

30
      final File srcFile = fs.file(fs.path.join(projectPath, 'lib', 'main.dart'));
31
      final String original = srcFile.readAsStringSync();
32 33
      srcFile.writeAsStringSync(original.replaceFirst('main()', 'main(  )'));

34 35
      final FormatCommand command = new FormatCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);
36
      await runner.run(<String>['format', srcFile.path]);
37

38
      final String formatted = srcFile.readAsStringSync();
39 40
      expect(formatted, original);
    });
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 71 72 73 74 75 76 77

    testUsingContext('dry-run', () async {
      final String projectPath = await createProject(temp);

      final File srcFile = fs.file(
          fs.path.join(projectPath, 'lib', 'main.dart'));
      final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
          'main()', 'main(  )');
      srcFile.writeAsStringSync(nonFormatted);

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

      final String shouldNotFormatted = srcFile.readAsStringSync();
      expect(shouldNotFormatted, nonFormatted);
    });

    testUsingContext('dry-run with set-exit-if-changed', () async {
      final String projectPath = await createProject(temp);

      final File srcFile = fs.file(
          fs.path.join(projectPath, 'lib', 'main.dart'));
      final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
          'main()', 'main(  )');
      srcFile.writeAsStringSync(nonFormatted);

      final FormatCommand command = new FormatCommand();
      final CommandRunner<Null> runner = createTestCommandRunner(command);

      expect(runner.run(<String>[
        'format', '--dry-run', '--set-exit-if-changed', srcFile.path
      ]), throwsException);

      final String shouldNotFormatted = srcFile.readAsStringSync();
      expect(shouldNotFormatted, nonFormatted);
    });
78 79
  });
}