Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPLAT-8079: Export FormatterInputs and support following links #328

Merged
merged 2 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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', () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these tests are all new, but I just moved it from below where it was named differently. The only new test is the followLinks one.

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