Skip to content

Commit 845696b

Browse files
committed
Don't touch files whose contents do not change. Fix #127.
BUG=#127 [email protected] Review URL: https://chromiumcodereview.appspot.com//850953004
1 parent 6c8fcc6 commit 845696b

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Split different operators with the same precedence equally (#130).
44
* No spaces for empty for loop clauses (#132).
5+
* Don't touch files whose contents did not change (#127).
56

67
# 0.1.2
78

lib/src/io.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ void processFile(File file, {String label, bool overwrite, int pageWidth}) {
3838

3939
var formatter = new DartFormatter(pageWidth: pageWidth);
4040
try {
41-
var output = formatter.format(file.readAsStringSync(), uri: file.path);
41+
var source = file.readAsStringSync();
42+
var output = formatter.format(source, uri: file.path);
4243
if (overwrite) {
43-
file.writeAsStringSync(output);
44-
print("Formatted $label");
44+
if (source != output) {
45+
file.writeAsStringSync(output);
46+
print("Formatted $label");
47+
} else {
48+
print("Unchanged $label");
49+
}
4550
} else {
4651
// Don't add an extra newline.
4752
stdout.write(output);

test/io_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
library dart_style.test.io;
66

7+
import 'dart:async';
78
import 'dart:io';
89

910
import 'package:dart_style/src/io.dart';
@@ -51,6 +52,41 @@ void main() {
5152
]).validate();
5253
});
5354

55+
test("doesn't touch unchanged files", () {
56+
d.dir('code', [
57+
d.file('bad.dart', _SOURCE),
58+
d.file('good.dart', _FORMATTED),
59+
]).create();
60+
61+
modTime(String file) {
62+
return new File(p.join(d.defaultRoot, 'code', file)).statSync().modified;
63+
}
64+
65+
var badBefore;
66+
var goodBefore;
67+
68+
schedule(() {
69+
badBefore = modTime('bad.dart');
70+
goodBefore = modTime('good.dart');
71+
72+
// Wait a bit so the mod time of a formatted file will be different.
73+
return new Future.delayed(new Duration(seconds: 1));
74+
});
75+
76+
schedule(() {
77+
var dir = new Directory(p.join(d.defaultRoot, 'code'));
78+
processDirectory(dir, overwrite: true);
79+
80+
// Should be touched.
81+
var badAfter = modTime('bad.dart');
82+
expect(badAfter, isNot(equals(badBefore)));
83+
84+
// Should not be touched.
85+
var goodAfter = modTime('good.dart');
86+
expect(goodAfter, equals(goodBefore));
87+
});
88+
});
89+
5490
test("doesn't follow directory symlinks by default", () {
5591
d.dir('code', [
5692
d.file('a.dart', _SOURCE),

0 commit comments

Comments
 (0)