OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart_style.test.command_line; | 5 library dart_style.test.command_line; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
10 import 'package:scheduled_test/descriptor.dart' as d; | 10 import 'package:scheduled_test/descriptor.dart' as d; |
11 import 'package:scheduled_test/scheduled_test.dart'; | 11 import 'package:scheduled_test/scheduled_test.dart'; |
12 | 12 |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 | 14 |
15 void main() { | 15 void main() { |
16 setUpTestSuite(); | 16 setUpTestSuite(); |
17 | 17 |
18 test("Exits with 0 on success.", () { | 18 test("Exits with 0 on success.", () { |
19 d.dir("code", [ | 19 d.dir("code", [ |
20 d.file("a.dart", unformattedSource) | 20 d.file("a.dart", unformattedSource) |
21 ]).create(); | 21 ]).create(); |
22 | 22 |
23 var process = runFormatter(); | 23 var process = runFormatterOnDir(); |
24 process.shouldExit(0); | 24 process.shouldExit(0); |
25 }); | 25 }); |
26 | 26 |
27 test("Exits with 64 on a command line argument error.", () { | 27 test("Exits with 64 on a command line argument error.", () { |
28 var process = runFormatter(["-wat"]); | 28 var process = runFormatterOnDir(["-wat"]); |
29 process.shouldExit(64); | 29 process.shouldExit(64); |
30 }); | 30 }); |
31 | 31 |
32 test("Exits with 65 on a parse error.", () { | 32 test("Exits with 65 on a parse error.", () { |
33 d.dir("code", [ | 33 d.dir("code", [ |
34 d.file("a.dart", "herp derp i are a dart") | 34 d.file("a.dart", "herp derp i are a dart") |
35 ]).create(); | 35 ]).create(); |
36 | 36 |
37 var process = runFormatter(); | 37 var process = runFormatterOnDir(); |
38 process.shouldExit(65); | 38 process.shouldExit(65); |
39 }); | 39 }); |
40 | 40 |
41 test("Errors if --dry-run and --overwrite are both passed.", () { | 41 test("Errors if --dry-run and --overwrite are both passed.", () { |
42 d.dir("code", [ | 42 d.dir("code", [ |
43 d.file("a.dart", unformattedSource) | 43 d.file("a.dart", unformattedSource) |
44 ]).create(); | 44 ]).create(); |
45 | 45 |
46 var process = runFormatter(["--dry-run", "--overwrite"]); | 46 var process = runFormatterOnDir(["--dry-run", "--overwrite"]); |
47 process.shouldExit(64); | 47 process.shouldExit(64); |
48 }); | 48 }); |
49 | 49 |
| 50 test("Errors if --dry-run and --machine are both passed.", () { |
| 51 d.dir("code", [ |
| 52 d.file("a.dart", unformattedSource) |
| 53 ]).create(); |
| 54 |
| 55 var process = runFormatterOnDir(["--dry-run", "--machine"]); |
| 56 process.shouldExit(64); |
| 57 }); |
| 58 |
| 59 test("Errors if --machine and --overwrite are both passed.", () { |
| 60 d.dir("code", [ |
| 61 d.file("a.dart", unformattedSource) |
| 62 ]).create(); |
| 63 |
| 64 var process = runFormatterOnDir(["--machine", "--overwrite"]); |
| 65 process.shouldExit(64); |
| 66 }); |
| 67 |
50 test("Errors if --dry-run and --machine are both passed.", () { | 68 test("Errors if --dry-run and --machine are both passed.", () { |
51 d.dir("code", [ | 69 d.dir("code", [ |
52 d.file("a.dart", unformattedSource) | 70 d.file("a.dart", unformattedSource) |
53 ]).create(); | 71 ]).create(); |
54 | 72 |
55 var process = runFormatter(["--dry-run", "--machine"]); | 73 var process = runFormatter(["--dry-run", "--machine"]); |
56 process.shouldExit(64); | 74 process.shouldExit(64); |
57 }); | 75 }); |
58 | 76 |
59 test("Errors if --machine and --overwrite are both passed.", () { | 77 test("Errors if --machine and --overwrite are both passed.", () { |
60 d.dir("code", [ | 78 d.dir("code", [ |
61 d.file("a.dart", unformattedSource) | 79 d.file("a.dart", unformattedSource) |
62 ]).create(); | 80 ]).create(); |
63 | 81 |
64 var process = runFormatter(["--machine", "--overwrite"]); | 82 var process = runFormatter(["--machine", "--overwrite"]); |
65 process.shouldExit(64); | 83 process.shouldExit(64); |
66 }); | 84 }); |
67 | 85 |
68 group("--dry-run", () { | 86 group("--dry-run", () { |
69 test("prints names of files that would change.", () { | 87 test("prints names of files that would change.", () { |
70 d.dir("code", [ | 88 d.dir("code", [ |
71 d.file("a_bad.dart", unformattedSource), | 89 d.file("a_bad.dart", unformattedSource), |
72 d.file("b_good.dart", formattedSource), | 90 d.file("b_good.dart", formattedSource), |
73 d.file("c_bad.dart", unformattedSource), | 91 d.file("c_bad.dart", unformattedSource), |
74 d.file("d_good.dart", formattedSource) | 92 d.file("d_good.dart", formattedSource) |
75 ]).create(); | 93 ]).create(); |
76 | 94 |
77 var process = runFormatter(["--dry-run"]); | 95 var process = runFormatterOnDir(["--dry-run"]); |
78 process.stdout.expect(p.join("code", "a_bad.dart")); | 96 process.stdout.expect(p.join("code", "a_bad.dart")); |
79 process.stdout.expect(p.join("code", "c_bad.dart")); | 97 process.stdout.expect(p.join("code", "c_bad.dart")); |
80 process.shouldExit(); | 98 process.shouldExit(); |
81 }); | 99 }); |
82 | 100 |
83 test("does not modify files.", () { | 101 test("does not modify files.", () { |
84 d.dir("code", [ | 102 d.dir("code", [ |
85 d.file("a.dart", unformattedSource) | 103 d.file("a.dart", unformattedSource) |
86 ]).create(); | 104 ]).create(); |
87 | 105 |
88 var process = runFormatter(["--dry-run"]); | 106 var process = runFormatterOnDir(["--dry-run"]); |
89 process.stdout.expect(p.join("code", "a.dart")); | 107 process.stdout.expect(p.join("code", "a.dart")); |
90 process.shouldExit(); | 108 process.shouldExit(); |
91 | 109 |
92 d.dir('code', [ | 110 d.dir('code', [ |
93 d.file('a.dart', unformattedSource) | 111 d.file('a.dart', unformattedSource) |
94 ]).validate(); | 112 ]).validate(); |
95 }); | 113 }); |
96 }); | 114 }); |
97 | 115 |
98 group("--machine", () { | 116 group("--machine", () { |
99 test("writes each output as json", () { | 117 test("writes each output as json", () { |
100 d.dir("code", [ | 118 d.dir("code", [ |
101 d.file("a.dart", unformattedSource), | 119 d.file("a.dart", unformattedSource), |
102 d.file("b.dart", unformattedSource) | 120 d.file("b.dart", unformattedSource) |
103 ]).create(); | 121 ]).create(); |
104 | 122 |
105 var process = runFormatter(["--machine", "code"]); | 123 var process = runFormatterOnDir(["--machine"]); |
106 | 124 |
107 var json = { | 125 var json = { |
108 "path": p.join("code", "a.dart"), | 126 "path": p.join("code", "a.dart"), |
109 "source": formattedSource, | 127 "source": formattedSource, |
110 "selection": {"offset": -1, "length": -1} | 128 "selection": {"offset": -1, "length": -1} |
111 }; | 129 }; |
112 | 130 |
113 process.stdout.expect(JSON.encode(json)); | 131 process.stdout.expect(JSON.encode(json)); |
114 | 132 |
115 json["path"] = p.join("code", "b.dart"); | 133 json["path"] = p.join("code", "b.dart"); |
116 process.stdout.expect(JSON.encode(json)); | 134 process.stdout.expect(JSON.encode(json)); |
117 | 135 |
118 process.shouldExit(); | 136 process.shouldExit(); |
119 }); | 137 }); |
120 }); | 138 }); |
| 139 |
| 140 group("with no paths", () { |
| 141 test("errors on --overwrite.", () { |
| 142 var process = runFormatter(["--overwrite"]); |
| 143 process.shouldExit(64); |
| 144 }); |
| 145 |
| 146 test("reads from stdin.", () { |
| 147 var process = runFormatter(); |
| 148 process.writeLine(unformattedSource); |
| 149 process.closeStdin(); |
| 150 |
| 151 // No trailing newline at the end. |
| 152 process.stdout.expect(formattedSource.trimRight()); |
| 153 process.shouldExit(); |
| 154 }); |
| 155 }); |
121 } | 156 } |
OLD | NEW |