Skip to content

Commit 23fbc34

Browse files
Merge pull request #328 from Workiva/export_formatter_inputs
CPLAT-8079: Export FormatterInputs and support following links
2 parents 05f47f2 + 6dac628 commit 23fbc34

File tree

10 files changed

+94
-66
lines changed

10 files changed

+94
-66
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [3.1.0](https://github.com/Workiva/dart_dev/compare/3.0.0...3.1.0)
4+
5+
- Update `FormatTool.getInputs()` to support an optional `followLinks` param.
6+
When enabled, links will be followed instead of skipped.
7+
- Export the `FormatterInputs` class that is the return type of
8+
`FormatTool.getInputs()`.
9+
310
## [3.0.0](https://github.com/Workiva/dart_dev/compare/2.2.1...3.0.0)
411

512
**This is a major release of `dart_dev` with breaking changes.** It is also the

lib/dart_dev.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export 'src/dart_dev_tool.dart'
44
export 'src/tools/analyze_tool.dart' show AnalyzeTool;
55
export 'src/tools/compound_tool.dart'
66
show ArgMapper, CompoundTool, CompoundToolMixin, takeAllArgs;
7-
export 'src/tools/format_tool.dart' show FormatMode, Formatter, FormatTool;
7+
export 'src/tools/format_tool.dart'
8+
show FormatMode, Formatter, FormatterInputs, FormatTool;
89
export 'src/tools/process_tool.dart' show ProcessTool;
910
export 'src/tools/test_tool.dart' show TestTool;
1011
export 'src/tools/tuneup_check_tool.dart' show TuneupCheckTool;

lib/src/tools/format_tool.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ class FormatTool extends DevTool {
120120
/// By default these globs are assumed to be relative to the current working
121121
/// directory, but that can be overridden via [root] for testing purposes.
122122
static FormatterInputs getInputs(
123-
{List<Glob> exclude, bool expandCwd, String root}) {
123+
{List<Glob> exclude, bool expandCwd, bool followLinks, String root}) {
124124
expandCwd ??= false;
125+
followLinks ??= false;
126+
125127
final includedFiles = <String>{};
126128
final excludedFiles = <String>{};
127129
final skippedLinks = <String>{};
@@ -135,7 +137,8 @@ class FormatTool extends DevTool {
135137

136138
final dir = Directory(root ?? '.');
137139

138-
for (final entry in dir.listSync(recursive: true, followLinks: false)) {
140+
for (final entry
141+
in dir.listSync(recursive: true, followLinks: followLinks)) {
139142
final relative = p.relative(entry.path, from: dir.path);
140143

141144
if (entry is Link) {
@@ -177,15 +180,15 @@ class FormatTool extends DevTool {
177180

178181
class FormatterInputs {
179182
FormatterInputs(this.includedFiles,
180-
{this.skippedLinks, this.excludedFiles, this.hiddenDirectories});
181-
182-
final Set<String> includedFiles;
183-
184-
final Set<String> skippedLinks;
183+
{this.excludedFiles, this.hiddenDirectories, this.skippedLinks});
185184

186185
final Set<String> excludedFiles;
187186

188187
final Set<String> hiddenDirectories;
188+
189+
final Set<String> includedFiles;
190+
191+
final Set<String> skippedLinks;
189192
}
190193

191194
/// A declarative representation of an execution of the [FormatTool].
@@ -341,17 +344,17 @@ FormatExecution buildExecution(
341344

342345
if (inputs.excludedFiles?.isNotEmpty ?? false) {
343346
_log.fine('Excluding these paths from formatting:\n '
344-
'${inputs.excludedFiles.join('\n')}');
347+
'${inputs.excludedFiles.join('\n ')}');
345348
}
346349

347350
if (inputs.skippedLinks?.isNotEmpty ?? false) {
348351
_log.fine('Excluding these links from formatting:\n '
349-
'${inputs.skippedLinks.join('\n')}');
352+
'${inputs.skippedLinks.join('\n ')}');
350353
}
351354

352355
if (inputs.hiddenDirectories?.isNotEmpty ?? false) {
353356
_log.fine('Excluding these hidden directories from formatting:\n '
354-
'${inputs.hiddenDirectories.join('\n')}');
357+
'${inputs.hiddenDirectories.join('\n ')}');
355358
}
356359

357360
final dartfmt = buildProcess(formatter);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: dart_dev
2-
version: 3.0.0
2+
version: 3.1.0
33
description: Centralized tooling for Dart projects. Consistent interface across projects. Easily configurable.
44
authors:
55
- Workiva Client Platform Team <[email protected]>

test/tools/fixtures/format/globs/lib-link

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/tools/fixtures/format/globs/link.dart

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/tools/fixtures/format/globs/linked.dart

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../lib/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../linked.dart

test/tools/format_tool_test.dart

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:dart_dev/src/dart_dev_tool.dart';
77
import 'package:glob/glob.dart';
88
import 'package:io/io.dart';
99
import 'package:logging/logging.dart';
10+
import 'package:path/path.dart' as p;
1011
import 'package:test/test.dart';
1112

1213
import 'package:dart_dev/src/tools/format_tool.dart';
@@ -15,7 +16,7 @@ import '../log_matchers.dart';
1516
import 'shared_tool_tests.dart';
1617

1718
void main() {
18-
group('FormatCommand', () {
19+
group('FormatTool', () {
1920
sharedDevToolTests(() => FormatTool());
2021

2122
test('toCommand overrides the argParser', () {
@@ -37,6 +38,73 @@ void main() {
3738

3839
expect(argParser.options['formatter-args'].type, OptionType.single);
3940
});
41+
42+
group('getInputs', () {
43+
final root = 'test/tools/fixtures/format/globs';
44+
45+
test('no excludes', () {
46+
final formatterInputs = FormatTool.getInputs(root: root);
47+
expect(formatterInputs.includedFiles, unorderedEquals({'.'}));
48+
expect(formatterInputs.excludedFiles, null);
49+
expect(formatterInputs.hiddenDirectories, null);
50+
expect(formatterInputs.skippedLinks, null);
51+
});
52+
53+
test('custom excludes', () {
54+
FormatterInputs formatterInputs =
55+
FormatTool.getInputs(exclude: [Glob('*_exclude.dart')], root: root);
56+
57+
expect(
58+
formatterInputs.includedFiles,
59+
unorderedEquals({
60+
'file.dart',
61+
'lib/sub/file.dart',
62+
'linked.dart',
63+
'other/file.dart',
64+
}));
65+
66+
expect(formatterInputs.excludedFiles,
67+
unorderedEquals({'should_exclude.dart'}));
68+
expect(formatterInputs.hiddenDirectories,
69+
unorderedEquals({'.dart_tool_test'}));
70+
expect(formatterInputs.skippedLinks,
71+
unorderedEquals({'links/lib-link', 'links/link.dart'}));
72+
});
73+
74+
test('empty inputs due to excludes config', () async {
75+
expect(
76+
FormatTool.getInputs(exclude: [Glob('**')], root: root)
77+
.includedFiles,
78+
isEmpty);
79+
});
80+
81+
test('expandCwd forces . to be expanded to all files', () async {
82+
final formatterInputs =
83+
FormatTool.getInputs(expandCwd: true, root: root);
84+
expect(
85+
formatterInputs.includedFiles,
86+
unorderedEquals({
87+
'file.dart',
88+
'lib/sub/file.dart',
89+
'linked.dart',
90+
'other/file.dart',
91+
'should_exclude.dart',
92+
}));
93+
expect(formatterInputs.excludedFiles, isEmpty);
94+
});
95+
96+
test('followLinks follows linked files and directories', () async {
97+
final formatterInputs = FormatTool.getInputs(
98+
expandCwd: true, followLinks: true, root: p.join(root, 'links'));
99+
expect(
100+
formatterInputs.includedFiles,
101+
unorderedEquals({
102+
'lib-link/sub/file.dart',
103+
'link.dart',
104+
}));
105+
expect(formatterInputs.skippedLinks, isEmpty);
106+
});
107+
});
40108
});
41109

42110
group('buildArgs', () {
@@ -217,57 +285,6 @@ void main() {
217285
});
218286
});
219287

220-
group('getFormatterInputs', () {
221-
final root = 'test/tools/fixtures/format/globs';
222-
223-
test('no excludes', () {
224-
final formatterInputs = FormatTool.getInputs(root: root);
225-
expect(formatterInputs.includedFiles, unorderedEquals({'.'}));
226-
expect(formatterInputs.excludedFiles, null);
227-
expect(formatterInputs.hiddenDirectories, null);
228-
expect(formatterInputs.skippedLinks, null);
229-
});
230-
231-
test('custom excludes', () {
232-
FormatterInputs formatterInputs =
233-
FormatTool.getInputs(exclude: [Glob('*_exclude.dart')], root: root);
234-
235-
expect(
236-
formatterInputs.includedFiles,
237-
unorderedEquals({
238-
'file.dart',
239-
'lib/sub/file.dart',
240-
'other/file.dart',
241-
}));
242-
243-
expect(formatterInputs.excludedFiles,
244-
unorderedEquals({'should_exclude.dart'}));
245-
expect(formatterInputs.hiddenDirectories,
246-
unorderedEquals({'.dart_tool_test'}));
247-
expect(formatterInputs.skippedLinks,
248-
unorderedEquals({'lib-link', 'link.dart'}));
249-
});
250-
251-
test('empty inputs due to excludes config', () async {
252-
expect(
253-
FormatTool.getInputs(exclude: [Glob('**')], root: root).includedFiles,
254-
isEmpty);
255-
});
256-
257-
test('expandCwd forces . to be expanded to all files', () async {
258-
final formatterInputs = FormatTool.getInputs(expandCwd: true, root: root);
259-
expect(
260-
formatterInputs.includedFiles,
261-
unorderedEquals({
262-
'file.dart',
263-
'lib/sub/file.dart',
264-
'other/file.dart',
265-
'should_exclude.dart',
266-
}));
267-
expect(formatterInputs.excludedFiles, isEmpty);
268-
});
269-
});
270-
271288
group('buildProcess', () {
272289
test('dartfmt', () {
273290
final process = buildProcess(Formatter.dartfmt);

0 commit comments

Comments
 (0)