format_test.dart 3.83 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
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/format.dart';

10 11
import '../../src/common.dart';
import '../../src/context.dart';
12 13 14

void main() {
  group('format', () {
15
    Directory tempDir;
16 17 18

    setUp(() {
      Cache.disableLocking();
19
      tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_format_test.');
20 21 22
    });

    tearDown(() {
23
      tryToDelete(tempDir);
24 25 26
    });

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

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

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

37
      final String formatted = srcFile.readAsStringSync();
38 39
      expect(formatted, original);
    });
40 41

    testUsingContext('dry-run', () async {
42
      final String projectPath = await createProject(tempDir);
43 44 45 46 47 48 49

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

50
      final FormatCommand command = FormatCommand();
51
      final CommandRunner<void> runner = createTestCommandRunner(command);
52 53 54 55 56 57 58
      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 {
59
      final String projectPath = await createProject(tempDir);
60 61 62 63 64 65 66

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

67
      final FormatCommand command = FormatCommand();
68
      final CommandRunner<void> runner = createTestCommandRunner(command);
69 70

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

      final String shouldNotFormatted = srcFile.readAsStringSync();
      expect(shouldNotFormatted, nonFormatted);
    });
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    testUsingContext('line-length', () async {
      const int lineLengthShort = 50;
      const int lineLengthLong = 120;
      final String projectPath = await createProject(tempDir);

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

      final String nonFormattedWithLongLine = srcFile.readAsStringSync();
      final FormatCommand command = FormatCommand();
      final CommandRunner<void> runner = createTestCommandRunner(command);

      await runner.run(<String>['format', '--line-length', '$lineLengthLong', srcFile.path]);
      final String notFormatted = srcFile.readAsStringSync();
      expect(nonFormattedWithLongLine, notFormatted);

      await runner.run(<String>['format', '--line-length', '$lineLengthShort', srcFile.path]);
      final String shouldFormatted = srcFile.readAsStringSync();
      expect(nonFormattedWithLongLine, isNot(shouldFormatted));
    });
102 103
  });
}