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", () { |
| 74 var process = runFormatter(["--version"]); |
| 75 |
| 76 // Match something roughly semver-like. |
| 77 process.stdout.expect(matches(r"\d+\.\d+\.\d+.*")); |
| 78 process.shouldExit(0); |
| 79 }); |
| 80 |
| 81 test("only prints a hidden directory once", () { |
| 82 d.dir('code', [ |
| 83 d.dir('.skip', [ |
| 84 d.file('a.dart', unformattedSource), |
| 85 d.file('b.dart', unformattedSource) |
| 86 ]) |
| 87 ]).create(); |
| 88 |
| 89 var process = runFormatterOnDir(); |
| 90 |
| 91 process.stdout.expect(startsWith("Formatting directory")); |
| 92 process.stdout.expect("Skipping hidden path ${p.join("code", ".skip")}"); |
| 93 process.shouldExit(); |
| 94 }); |
| 95 |
73 group("--dry-run", () { | 96 group("--dry-run", () { |
74 test("prints names of files that would change.", () { | 97 test("prints names of files that would change", () { |
75 d.dir("code", [ | 98 d.dir("code", [ |
76 d.file("a_bad.dart", unformattedSource), | 99 d.file("a_bad.dart", unformattedSource), |
77 d.file("b_good.dart", formattedSource), | 100 d.file("b_good.dart", formattedSource), |
78 d.file("c_bad.dart", unformattedSource), | 101 d.file("c_bad.dart", unformattedSource), |
79 d.file("d_good.dart", formattedSource) | 102 d.file("d_good.dart", formattedSource) |
80 ]).create(); | 103 ]).create(); |
81 | 104 |
82 var aBad = p.join("code", "a_bad.dart"); | 105 var aBad = p.join("code", "a_bad.dart"); |
83 var cBad = p.join("code", "c_bad.dart"); | 106 var cBad = p.join("code", "c_bad.dart"); |
84 | 107 |
85 var process = runFormatterOnDir(["--dry-run"]); | 108 var process = runFormatterOnDir(["--dry-run"]); |
86 | 109 |
87 // The order isn't specified. | 110 // The order isn't specified. |
88 process.stdout.expect(either(aBad, cBad)); | 111 process.stdout.expect(either(aBad, cBad)); |
89 process.stdout.expect(either(aBad, cBad)); | 112 process.stdout.expect(either(aBad, cBad)); |
90 process.shouldExit(); | 113 process.shouldExit(); |
91 }); | 114 }); |
92 | 115 |
93 test("does not modify files.", () { | 116 test("does not modify files", () { |
94 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); | 117 d.dir("code", [d.file("a.dart", unformattedSource)]).create(); |
95 | 118 |
96 var process = runFormatterOnDir(["--dry-run"]); | 119 var process = runFormatterOnDir(["--dry-run"]); |
97 process.stdout.expect(p.join("code", "a.dart")); | 120 process.stdout.expect(p.join("code", "a.dart")); |
98 process.shouldExit(); | 121 process.shouldExit(); |
99 | 122 |
100 d.dir('code', [d.file('a.dart', unformattedSource)]).validate(); | 123 d.dir('code', [d.file('a.dart', unformattedSource)]).validate(); |
101 }); | 124 }); |
102 }); | 125 }); |
103 | 126 |
(...skipping 19 matching lines...) Expand all Loading... |
123 var process = runFormatterOnDir(["--machine"]); | 146 var process = runFormatterOnDir(["--machine"]); |
124 | 147 |
125 // The order isn't specified. | 148 // The order isn't specified. |
126 process.stdout.expect(either(jsonA, jsonB)); | 149 process.stdout.expect(either(jsonA, jsonB)); |
127 process.stdout.expect(either(jsonA, jsonB)); | 150 process.stdout.expect(either(jsonA, jsonB)); |
128 process.shouldExit(); | 151 process.shouldExit(); |
129 }); | 152 }); |
130 }); | 153 }); |
131 | 154 |
132 group("--preserve", () { | 155 group("--preserve", () { |
133 test("errors if given paths.", () { | 156 test("errors if given paths", () { |
134 var process = runFormatter(["--preserve", "path", "another"]); | 157 var process = runFormatter(["--preserve", "path", "another"]); |
135 process.shouldExit(64); | 158 process.shouldExit(64); |
136 }); | 159 }); |
137 | 160 |
138 test("errors on wrong number of components.", () { | 161 test("errors on wrong number of components", () { |
139 var process = runFormatter(["--preserve", "1"]); | 162 var process = runFormatter(["--preserve", "1"]); |
140 process.shouldExit(64); | 163 process.shouldExit(64); |
141 | 164 |
142 process = runFormatter(["--preserve", "1:2:3"]); | 165 process = runFormatter(["--preserve", "1:2:3"]); |
143 process.shouldExit(64); | 166 process.shouldExit(64); |
144 }); | 167 }); |
145 | 168 |
146 test("errors on non-integer component.", () { | 169 test("errors on non-integer component", () { |
147 var process = runFormatter(["--preserve", "1:2.3"]); | 170 var process = runFormatter(["--preserve", "1:2.3"]); |
148 process.shouldExit(64); | 171 process.shouldExit(64); |
149 }); | 172 }); |
150 | 173 |
151 test("updates selection.", () { | 174 test("updates selection", () { |
152 var process = runFormatter(["--preserve", "6:10", "-m"]); | 175 var process = runFormatter(["--preserve", "6:10", "-m"]); |
153 process.writeLine(unformattedSource); | 176 process.writeLine(unformattedSource); |
154 process.closeStdin(); | 177 process.closeStdin(); |
155 | 178 |
156 var json = JSON.encode({ | 179 var json = JSON.encode({ |
157 "path": "<stdin>", | 180 "path": "<stdin>", |
158 "source": formattedSource, | 181 "source": formattedSource, |
159 "selection": {"offset": 5, "length": 9} | 182 "selection": {"offset": 5, "length": 9} |
160 }); | 183 }); |
161 | 184 |
162 process.stdout.expect(json); | 185 process.stdout.expect(json); |
163 process.shouldExit(); | 186 process.shouldExit(); |
164 }); | 187 }); |
165 }); | 188 }); |
166 | 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 |
167 group("with no paths", () { | 213 group("with no paths", () { |
168 test("errors on --overwrite.", () { | 214 test("errors on --overwrite", () { |
169 var process = runFormatter(["--overwrite"]); | 215 var process = runFormatter(["--overwrite"]); |
170 process.shouldExit(64); | 216 process.shouldExit(64); |
171 }); | 217 }); |
172 | 218 |
173 test("exits with 65 on parse error.", () { | 219 test("exits with 65 on parse error", () { |
174 var process = runFormatter(); | 220 var process = runFormatter(); |
175 process.writeLine("herp derp i are a dart"); | 221 process.writeLine("herp derp i are a dart"); |
176 process.closeStdin(); | 222 process.closeStdin(); |
177 process.shouldExit(65); | 223 process.shouldExit(65); |
178 }); | 224 }); |
179 | 225 |
180 test("reads from stdin.", () { | 226 test("reads from stdin", () { |
181 var process = runFormatter(); | 227 var process = runFormatter(); |
182 process.writeLine(unformattedSource); | 228 process.writeLine(unformattedSource); |
183 process.closeStdin(); | 229 process.closeStdin(); |
184 | 230 |
185 // No trailing newline at the end. | 231 // No trailing newline at the end. |
186 process.stdout.expect(formattedSource.trimRight()); | 232 process.stdout.expect(formattedSource.trimRight()); |
187 process.shouldExit(0); | 233 process.shouldExit(0); |
188 }); | 234 }); |
189 }); | 235 }); |
190 } | 236 } |
OLD | NEW |