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 import 'package:scheduled_test/scheduled_stream.dart'; | 12 import 'package:scheduled_test/scheduled_stream.dart'; |
13 | 13 |
14 import 'utils.dart'; | 14 import 'utils.dart'; |
15 | 15 |
16 void main() { | 16 void main() { |
17 setUpTestSuite(); | 17 setUpTestSuite(); |
18 | 18 |
19 test("Exits with 0 on success.", () { | 19 test("exits with 0 on success", () { |
20 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 20 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
21 | 21 |
22 var process = runFormatterOnDir(); | 22 var process = runFormatterOnDir(); |
23 process.shouldExit(0); | 23 process.shouldExit(0); |
24 }); | 24 }); |
25 | 25 |
26 test("Exits with 64 on a command line argument error.", () { | 26 test("exits with 64 on a command line argument error", () { |
27 var process = runFormatterOnDir(["-wat"]); | 27 var process = runFormatterOnDir(["-wat"]); |
28 process.shouldExit(64); | 28 process.shouldExit(64); |
29 }); | 29 }); |
30 | 30 |
31 test("Exits with 65 on a parse error.", () { | 31 test("exits with 65 on a parse error", () { |
32 d.dir("code", [d.file("a.dart", "herp derp i are a dart")]).create(); | 32 d.dir("code", [d.file("a.dart", "herp derp i are a dart")]).create(); |
33 | 33 |
34 var process = runFormatterOnDir(); | 34 var process = runFormatterOnDir(); |
35 process.shouldExit(65); | 35 process.shouldExit(65); |
36 }); | 36 }); |
37 | 37 |
38 test("Errors if --dry-run and --overwrite are both passed.", () { | 38 test("errors if --dry-run and --overwrite are both passed", () { |
39 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 39 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
40 | 40 |
41 var process = runFormatterOnDir(["--dry-run", "--overwrite"]); | 41 var process = runFormatterOnDir(["--dry-run", "--overwrite"]); |
42 process.shouldExit(64); | 42 process.shouldExit(64); |
43 }); | 43 }); |
44 | 44 |
45 test("Errors if --dry-run and --machine are both passed.", () { | 45 test("errors if --dry-run and --machine are both passed", () { |
46 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 46 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
47 | 47 |
48 var process = runFormatterOnDir(["--dry-run", "--machine"]); | 48 var process = runFormatterOnDir(["--dry-run", "--machine"]); |
49 process.shouldExit(64); | 49 process.shouldExit(64); |
50 }); | 50 }); |
51 | 51 |
52 test("Errors if --machine and --overwrite are both passed.", () { | 52 test("errors if --machine and --overwrite are both passed", () { |
53 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 53 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
54 | 54 |
55 var process = runFormatterOnDir(["--machine", "--overwrite"]); | 55 var process = runFormatterOnDir(["--machine", "--overwrite"]); |
56 process.shouldExit(64); | 56 process.shouldExit(64); |
57 }); | 57 }); |
58 | 58 |
59 test("Errors if --dry-run and --machine are both passed.", () { | 59 test("errors if --dry-run and --machine are both passed", () { |
60 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 60 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
61 | 61 |
62 var process = runFormatter(["--dry-run", "--machine"]); | 62 var process = runFormatter(["--dry-run", "--machine"]); |
63 process.shouldExit(64); | 63 process.shouldExit(64); |
64 }); | 64 }); |
65 | 65 |
66 test("Errors if --machine and --overwrite are both passed.", () { | 66 test("errors if --machine and --overwrite are both passed", () { |
67 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 67 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
68 | 68 |
69 var process = runFormatter(["--machine", "--overwrite"]); | 69 var process = runFormatter(["--machine", "--overwrite"]); |
70 process.shouldExit(64); | 70 process.shouldExit(64); |
71 }); | 71 }); |
72 | 72 |
73 test("--version prints the version number", () { | 73 test("--version prints the version number", () { |
74 var process = runFormatter(["--version"]); | 74 var process = runFormatter(["--version"]); |
75 | 75 |
76 // Match something roughly semver-like. | 76 // Match something roughly semver-like. |
(...skipping 10 matching lines...) Expand all Loading... |
87 ]).create(); | 87 ]).create(); |
88 | 88 |
89 var process = runFormatterOnDir(); | 89 var process = runFormatterOnDir(); |
90 | 90 |
91 process.stdout.expect(startsWith("Formatting directory")); | 91 process.stdout.expect(startsWith("Formatting directory")); |
92 process.stdout.expect("Skipping hidden path ${p.join("code", ".skip")}"); | 92 process.stdout.expect("Skipping hidden path ${p.join("code", ".skip")}"); |
93 process.shouldExit(); | 93 process.shouldExit(); |
94 }); | 94 }); |
95 | 95 |
96 group("--dry-run", () { | 96 group("--dry-run", () { |
97 test("prints names of files that would change.", () { | 97 test("prints names of files that would change", () { |
98 d.dir("code", [ | 98 d.dir("code", [ |
99 d.file("a_bad.dart", unformattedSource), | 99 d.file("a_bad.dart", unformattedSource), |
100 d.file("b_good.dart", formattedSource), | 100 d.file("b_good.dart", formattedSource), |
101 d.file("c_bad.dart", unformattedSource), | 101 d.file("c_bad.dart", unformattedSource), |
102 d.file("d_good.dart", formattedSource) | 102 d.file("d_good.dart", formattedSource) |
103 ]).create(); | 103 ]).create(); |
104 | 104 |
105 var aBad = p.join("code", "a_bad.dart"); | 105 var aBad = p.join("code", "a_bad.dart"); |
106 var cBad = p.join("code", "c_bad.dart"); | 106 var cBad = p.join("code", "c_bad.dart"); |
107 | 107 |
108 var process = runFormatterOnDir(["--dry-run"]); | 108 var process = runFormatterOnDir(["--dry-run"]); |
109 | 109 |
110 // The order isn't specified. | 110 // The order isn't specified. |
111 process.stdout.expect(either(aBad, cBad)); | 111 process.stdout.expect(either(aBad, cBad)); |
112 process.stdout.expect(either(aBad, cBad)); | 112 process.stdout.expect(either(aBad, cBad)); |
113 process.shouldExit(); | 113 process.shouldExit(); |
114 }); | 114 }); |
115 | 115 |
116 test("does not modify files.", () { | 116 test("does not modify files", () { |
117 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 117 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
118 | 118 |
119 var process = runFormatterOnDir(["--dry-run"]); | 119 var process = runFormatterOnDir(["--dry-run"]); |
120 process.stdout.expect(p.join("code", "a.dart")); | 120 process.stdout.expect(p.join("code", "a.dart")); |
121 process.shouldExit(); | 121 process.shouldExit(); |
122 | 122 |
123 d.dir('code', [d.file('a.dart', unformattedSource)]).validate(); | 123 d.dir('code', [d.file('a.dart', unformattedSource)]).validate(); |
124 }); | 124 }); |
125 }); | 125 }); |
126 | 126 |
(...skipping 19 matching lines...) Expand all Loading... |
146 var process = runFormatterOnDir(["--machine"]); | 146 var process = runFormatterOnDir(["--machine"]); |
147 | 147 |
148 // The order isn't specified. | 148 // The order isn't specified. |
149 process.stdout.expect(either(jsonA, jsonB)); | 149 process.stdout.expect(either(jsonA, jsonB)); |
150 process.stdout.expect(either(jsonA, jsonB)); | 150 process.stdout.expect(either(jsonA, jsonB)); |
151 process.shouldExit(); | 151 process.shouldExit(); |
152 }); | 152 }); |
153 }); | 153 }); |
154 | 154 |
155 group("--preserve", () { | 155 group("--preserve", () { |
156 test("errors if given paths.", () { | 156 test("errors if given paths", () { |
157 var process = runFormatter(["--preserve", "path", "another"]); | 157 var process = runFormatter(["--preserve", "path", "another"]); |
158 process.shouldExit(64); | 158 process.shouldExit(64); |
159 }); | 159 }); |
160 | 160 |
161 test("errors on wrong number of components.", () { | 161 test("errors on wrong number of components", () { |
162 var process = runFormatter(["--preserve", "1"]); | 162 var process = runFormatter(["--preserve", "1"]); |
163 process.shouldExit(64); | 163 process.shouldExit(64); |
164 | 164 |
165 process = runFormatter(["--preserve", "1:2:3"]); | 165 process = runFormatter(["--preserve", "1:2:3"]); |
166 process.shouldExit(64); | 166 process.shouldExit(64); |
167 }); | 167 }); |
168 | 168 |
169 test("errors on non-integer component.", () { | 169 test("errors on non-integer component", () { |
170 var process = runFormatter(["--preserve", "1:2.3"]); | 170 var process = runFormatter(["--preserve", "1:2.3"]); |
171 process.shouldExit(64); | 171 process.shouldExit(64); |
172 }); | 172 }); |
173 | 173 |
174 test("updates selection.", () { | 174 test("updates selection", () { |
175 var process = runFormatter(["--preserve", "6:10", "-m"]); | 175 var process = runFormatter(["--preserve", "6:10", "-m"]); |
176 process.writeLine(unformattedSource); | 176 process.writeLine(unformattedSource); |
177 process.closeStdin(); | 177 process.closeStdin(); |
178 | 178 |
179 var json = JSON.encode({ | 179 var json = JSON.encode({ |
180 "path": "<stdin>", | 180 "path": "<stdin>", |
181 "source": formattedSource, | 181 "source": formattedSource, |
182 "selection": {"offset": 5, "length": 9} | 182 "selection": {"offset": 5, "length": 9} |
183 }); | 183 }); |
184 | 184 |
185 process.stdout.expect(json); | 185 process.stdout.expect(json); |
186 process.shouldExit(); | 186 process.shouldExit(); |
187 }); | 187 }); |
188 }); | 188 }); |
189 | 189 |
| 190 group("--indent", () { |
| 191 test("sets the leading indentation of the output", () { |
| 192 var process = runFormatter(["--indent", "3"]); |
| 193 process.writeLine("main() {'''"); |
| 194 process.writeLine("a flush left multi-line string''';}"); |
| 195 process.closeStdin(); |
| 196 |
| 197 process.stdout.expect(" main() {"); |
| 198 process.stdout.expect(" '''"); |
| 199 process.stdout.expect("a flush left multi-line string''';"); |
| 200 process.stdout.expect(" }"); |
| 201 process.shouldExit(0); |
| 202 }); |
| 203 |
| 204 test("errors if the indent is not a non-negative number", () { |
| 205 var process = runFormatter(["--indent", "notanum"]); |
| 206 process.shouldExit(64); |
| 207 |
| 208 process = runFormatter(["--preserve", "-4"]); |
| 209 process.shouldExit(64); |
| 210 }); |
| 211 }); |
| 212 |
190 group("with no paths", () { | 213 group("with no paths", () { |
191 test("errors on --overwrite.", () { | 214 test("errors on --overwrite", () { |
192 var process = runFormatter(["--overwrite"]); | 215 var process = runFormatter(["--overwrite"]); |
193 process.shouldExit(64); | 216 process.shouldExit(64); |
194 }); | 217 }); |
195 | 218 |
196 test("exits with 65 on parse error.", () { | 219 test("exits with 65 on parse error", () { |
197 var process = runFormatter(); | 220 var process = runFormatter(); |
198 process.writeLine("herp derp i are a dart"); | 221 process.writeLine("herp derp i are a dart"); |
199 process.closeStdin(); | 222 process.closeStdin(); |
200 process.shouldExit(65); | 223 process.shouldExit(65); |
201 }); | 224 }); |
202 | 225 |
203 test("reads from stdin.", () { | 226 test("reads from stdin", () { |
204 var process = runFormatter(); | 227 var process = runFormatter(); |
205 process.writeLine(unformattedSource); | 228 process.writeLine(unformattedSource); |
206 process.closeStdin(); | 229 process.closeStdin(); |
207 | 230 |
208 // No trailing newline at the end. | 231 // No trailing newline at the end. |
209 process.stdout.expect(formattedSource.trimRight()); | 232 process.stdout.expect(formattedSource.trimRight()); |
210 process.shouldExit(0); | 233 process.shouldExit(0); |
211 }); | 234 }); |
212 }); | 235 }); |
213 } | 236 } |
OLD | NEW |