File size: 3,633 Bytes
b7f6949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
71
72
73
74
75
76
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014 The Flutter 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:io' as io;

import 'package:path/path.dart' as path;
import 'package:test/test.dart';

class FileContentPair {
  FileContentPair({required this.name, required this.original, required this.formatted});

  final String name;
  final String original;
  final String formatted;
}

final FileContentPair dartContentPair = FileContentPair(
  name: 'format_test.dart',
  original: 'enum \n\nfoo {\n  entry1,\n  entry2,\n}',
  formatted: 'enum foo { entry1, entry2 }\n',
);

class TestFileFixture {
  TestFileFixture(this.filePairs, this.baseDir) {
    for (final FileContentPair filePair in filePairs) {
      final io.File file = io.File(path.join(baseDir.path, filePair.name));
      file.writeAsStringSync(filePair.original);
      files.add(file);
    }
  }

  final List<io.File> files = <io.File>[];
  final io.Directory baseDir;
  final List<FileContentPair> filePairs;

  void gitAdd() {
    final List<String> args = <String>['add'];
    for (final io.File file in files) {
      args.add(file.path);
    }

    io.Process.runSync('git', args);
  }

  void gitRemove() {
    final List<String> args = <String>['rm', '-f'];
    for (final io.File file in files) {
      args.add(file.path);
    }
    io.Process.runSync('git', args);
  }

  Iterable<FileContentPair> getFileContents() {
    final List<FileContentPair> results = <FileContentPair>[];
    for (int i = 0; i < files.length; i++) {
      final io.File file = files[i];
      final FileContentPair filePair = filePairs[i];
      final String content = file.readAsStringSync().replaceAll('\r\n', '\n');
      results.add(
        FileContentPair(name: filePair.name, original: content, formatted: filePair.formatted),
      );
    }
    return results;
  }
}

void main() {
  final io.File script = io.File(path.current).absolute;
  final io.Directory flutterRoot = script.parent.parent;
  final String formatterPath = path.join(
    flutterRoot.path,
    'dev',
    'tools',
    'format.${io.Platform.isWindows ? 'bat' : 'sh'}',
  );

  test(
    'Can fix Dart formatting errors',
    () {
      final TestFileFixture fixture = TestFileFixture(<FileContentPair>[
        dartContentPair,
      ], flutterRoot);
      try {
        fixture.gitAdd();
        io.Process.runSync(formatterPath, <String>['--fix'], workingDirectory: flutterRoot.path);

        final Iterable<FileContentPair> files = fixture.getFileContents();
        for (final FileContentPair pair in files) {
          expect(pair.original, equals(pair.formatted));
        }
      } finally {
        fixture.gitRemove();
      }
    },
    // TODO(goderbauer): Re-enable after the formatting changes have landed.
    skip: true,
  );

  test(
    'Prints error if dart formatter fails',
    () {
      final TestFileFixture fixture = TestFileFixture(<FileContentPair>[], flutterRoot);
      final io.File dartFile = io.File('${flutterRoot.path}/format_test2.dart');
      dartFile.writeAsStringSync('P\n');
      fixture.files.add(dartFile);

      try {
        fixture.gitAdd();
        final io.ProcessResult result = io.Process.runSync(formatterPath, <String>[
          '--fix',
        ], workingDirectory: flutterRoot.path);
        expect(result.stdout, contains('format_test2.dart produced the following error'));
        expect(result.exitCode, isNot(0));
      } finally {
        fixture.gitRemove();
      }
    }, // TODO(goderbauer): Re-enable after the formatting changes have landed.
    skip: true,
  );
}