format_test.dart 3.96 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/format.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10

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

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

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

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

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

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

34
      final FormatCommand command = FormatCommand();
35
      final CommandRunner<void> 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

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

45 46
      final File srcFile = globals.fs.file(
          globals.fs.path.join(projectPath, 'lib', 'main.dart'));
47 48 49 50
      final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
          'main()', 'main(  )');
      srcFile.writeAsStringSync(nonFormatted);

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

62 63
      final File srcFile = globals.fs.file(
          globals.fs.path.join(projectPath, 'lib', 'main.dart'));
64 65 66 67
      final String nonFormatted = srcFile.readAsStringSync().replaceFirst(
          'main()', 'main(  )');
      srcFile.writeAsStringSync(nonFormatted);

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

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

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

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

84 85
      final File srcFile = globals.fs.file(
          globals.fs.path.join(projectPath, 'lib', 'main.dart'));
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
      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));
    });
103 104
  });
}