Skip to content

Commit

Permalink
Merge pull request #328 from Workiva/export_formatter_inputs
Browse files Browse the repository at this point in the history
CPLAT-8079: Export FormatterInputs and support following links
  • Loading branch information
rmconsole4-wk authored Nov 1, 2019
2 parents 05f47f2 + 6dac628 commit 23fbc34
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 66 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [3.1.0](https://github.com/Workiva/dart_dev/compare/3.0.0...3.1.0)

- Update `FormatTool.getInputs()` to support an optional `followLinks` param.
When enabled, links will be followed instead of skipped.
- Export the `FormatterInputs` class that is the return type of
`FormatTool.getInputs()`.

## [3.0.0](https://github.com/Workiva/dart_dev/compare/2.2.1...3.0.0)

**This is a major release of `dart_dev` with breaking changes.** It is also the
Expand Down
3 changes: 2 additions & 1 deletion lib/dart_dev.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export 'src/dart_dev_tool.dart'
export 'src/tools/analyze_tool.dart' show AnalyzeTool;
export 'src/tools/compound_tool.dart'
show ArgMapper, CompoundTool, CompoundToolMixin, takeAllArgs;
export 'src/tools/format_tool.dart' show FormatMode, Formatter, FormatTool;
export 'src/tools/format_tool.dart'
show FormatMode, Formatter, FormatterInputs, FormatTool;
export 'src/tools/process_tool.dart' show ProcessTool;
export 'src/tools/test_tool.dart' show TestTool;
export 'src/tools/tuneup_check_tool.dart' show TuneupCheckTool;
Expand Down
23 changes: 13 additions & 10 deletions lib/src/tools/format_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ class FormatTool extends DevTool {
/// By default these globs are assumed to be relative to the current working
/// directory, but that can be overridden via [root] for testing purposes.
static FormatterInputs getInputs(
{List<Glob> exclude, bool expandCwd, String root}) {
{List<Glob> exclude, bool expandCwd, bool followLinks, String root}) {
expandCwd ??= false;
followLinks ??= false;

final includedFiles = <String>{};
final excludedFiles = <String>{};
final skippedLinks = <String>{};
Expand All @@ -135,7 +137,8 @@ class FormatTool extends DevTool {

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

for (final entry in dir.listSync(recursive: true, followLinks: false)) {
for (final entry
in dir.listSync(recursive: true, followLinks: followLinks)) {
final relative = p.relative(entry.path, from: dir.path);

if (entry is Link) {
Expand Down Expand Up @@ -177,15 +180,15 @@ class FormatTool extends DevTool {

class FormatterInputs {
FormatterInputs(this.includedFiles,
{this.skippedLinks, this.excludedFiles, this.hiddenDirectories});

final Set<String> includedFiles;

final Set<String> skippedLinks;
{this.excludedFiles, this.hiddenDirectories, this.skippedLinks});

final Set<String> excludedFiles;

final Set<String> hiddenDirectories;

final Set<String> includedFiles;

final Set<String> skippedLinks;
}

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

if (inputs.excludedFiles?.isNotEmpty ?? false) {
_log.fine('Excluding these paths from formatting:\n '
'${inputs.excludedFiles.join('\n')}');
'${inputs.excludedFiles.join('\n ')}');
}

if (inputs.skippedLinks?.isNotEmpty ?? false) {
_log.fine('Excluding these links from formatting:\n '
'${inputs.skippedLinks.join('\n')}');
'${inputs.skippedLinks.join('\n ')}');
}

if (inputs.hiddenDirectories?.isNotEmpty ?? false) {
_log.fine('Excluding these hidden directories from formatting:\n '
'${inputs.hiddenDirectories.join('\n')}');
'${inputs.hiddenDirectories.join('\n ')}');
}

final dartfmt = buildProcess(formatter);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dart_dev
version: 3.0.0
version: 3.1.0
description: Centralized tooling for Dart projects. Consistent interface across projects. Easily configurable.
authors:
- Workiva Client Platform Team <[email protected]>
Expand Down
1 change: 0 additions & 1 deletion test/tools/fixtures/format/globs/lib-link

This file was deleted.

1 change: 0 additions & 1 deletion test/tools/fixtures/format/globs/link.dart

This file was deleted.

Empty file.
1 change: 1 addition & 0 deletions test/tools/fixtures/format/globs/links/lib-link
1 change: 1 addition & 0 deletions test/tools/fixtures/format/globs/links/link.dart
121 changes: 69 additions & 52 deletions test/tools/format_tool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:dart_dev/src/dart_dev_tool.dart';
import 'package:glob/glob.dart';
import 'package:io/io.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

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

void main() {
group('FormatCommand', () {
group('FormatTool', () {
sharedDevToolTests(() => FormatTool());

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

expect(argParser.options['formatter-args'].type, OptionType.single);
});

group('getInputs', () {
final root = 'test/tools/fixtures/format/globs';

test('no excludes', () {
final formatterInputs = FormatTool.getInputs(root: root);
expect(formatterInputs.includedFiles, unorderedEquals({'.'}));
expect(formatterInputs.excludedFiles, null);
expect(formatterInputs.hiddenDirectories, null);
expect(formatterInputs.skippedLinks, null);
});

test('custom excludes', () {
FormatterInputs formatterInputs =
FormatTool.getInputs(exclude: [Glob('*_exclude.dart')], root: root);

expect(
formatterInputs.includedFiles,
unorderedEquals({
'file.dart',
'lib/sub/file.dart',
'linked.dart',
'other/file.dart',
}));

expect(formatterInputs.excludedFiles,
unorderedEquals({'should_exclude.dart'}));
expect(formatterInputs.hiddenDirectories,
unorderedEquals({'.dart_tool_test'}));
expect(formatterInputs.skippedLinks,
unorderedEquals({'links/lib-link', 'links/link.dart'}));
});

test('empty inputs due to excludes config', () async {
expect(
FormatTool.getInputs(exclude: [Glob('**')], root: root)
.includedFiles,
isEmpty);
});

test('expandCwd forces . to be expanded to all files', () async {
final formatterInputs =
FormatTool.getInputs(expandCwd: true, root: root);
expect(
formatterInputs.includedFiles,
unorderedEquals({
'file.dart',
'lib/sub/file.dart',
'linked.dart',
'other/file.dart',
'should_exclude.dart',
}));
expect(formatterInputs.excludedFiles, isEmpty);
});

test('followLinks follows linked files and directories', () async {
final formatterInputs = FormatTool.getInputs(
expandCwd: true, followLinks: true, root: p.join(root, 'links'));
expect(
formatterInputs.includedFiles,
unorderedEquals({
'lib-link/sub/file.dart',
'link.dart',
}));
expect(formatterInputs.skippedLinks, isEmpty);
});
});
});

group('buildArgs', () {
Expand Down Expand Up @@ -217,57 +285,6 @@ void main() {
});
});

group('getFormatterInputs', () {
final root = 'test/tools/fixtures/format/globs';

test('no excludes', () {
final formatterInputs = FormatTool.getInputs(root: root);
expect(formatterInputs.includedFiles, unorderedEquals({'.'}));
expect(formatterInputs.excludedFiles, null);
expect(formatterInputs.hiddenDirectories, null);
expect(formatterInputs.skippedLinks, null);
});

test('custom excludes', () {
FormatterInputs formatterInputs =
FormatTool.getInputs(exclude: [Glob('*_exclude.dart')], root: root);

expect(
formatterInputs.includedFiles,
unorderedEquals({
'file.dart',
'lib/sub/file.dart',
'other/file.dart',
}));

expect(formatterInputs.excludedFiles,
unorderedEquals({'should_exclude.dart'}));
expect(formatterInputs.hiddenDirectories,
unorderedEquals({'.dart_tool_test'}));
expect(formatterInputs.skippedLinks,
unorderedEquals({'lib-link', 'link.dart'}));
});

test('empty inputs due to excludes config', () async {
expect(
FormatTool.getInputs(exclude: [Glob('**')], root: root).includedFiles,
isEmpty);
});

test('expandCwd forces . to be expanded to all files', () async {
final formatterInputs = FormatTool.getInputs(expandCwd: true, root: root);
expect(
formatterInputs.includedFiles,
unorderedEquals({
'file.dart',
'lib/sub/file.dart',
'other/file.dart',
'should_exclude.dart',
}));
expect(formatterInputs.excludedFiles, isEmpty);
});
});

group('buildProcess', () {
test('dartfmt', () {
final process = buildProcess(Formatter.dartfmt);
Expand Down

0 comments on commit 23fbc34

Please sign in to comment.