Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into alestiago/remove-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Nov 23, 2023
2 parents 803dc23 + 1e10387 commit 9e4aba0
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/very_good_dart_cli_hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: very_good_dart_cli_hooks

on:
pull_request:
paths:
- ".github/workflows/very_good_dart_cli_hooks.yaml"
- "brick/hooks/**"
push:
branches:
- main
paths:
- ".github/workflows/very_good_dart_cli_hooks.yaml"
- "brick/hooks/**"

jobs:
build:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
with:
working_directory: "brick/hooks"
analyze_directories: "test"
report_on: "post_gen.dart"
4 changes: 4 additions & 0 deletions brick/hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dart_tool
.packages
pubspec.lock
build
1 change: 1 addition & 0 deletions brick/hooks/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:very_good_analysis/analysis_options.5.1.0.yaml
30 changes: 30 additions & 0 deletions brick/hooks/post_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dart:io';

import 'package:mason/mason.dart';
import 'package:meta/meta.dart';

/// Type definition for [Process.run].
typedef RunProcess = Future<ProcessResult> Function(
String executable,
List<String> arguments, {
String? workingDirectory,
bool runInShell,
});

Future<void> run(
HookContext context, {
@visibleForTesting RunProcess runProcess = Process.run,
}) async {
// Some imports are relative to the user specified package name, hence
// we try to fix the import directive ordering after the template has
// been generated.
//
// We only fix for the [directives_ordering](https://dart.dev/tools/linter-rules/directives_ordering)
// linter rules, as the other rule should be tackled by the template itself.
await runProcess('dart', [
'fix',
Directory.current.path,
'--apply',
'--code=directives_ordering',
]);
}
13 changes: 13 additions & 0 deletions brick/hooks/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: very_good_dart_cli_hooks

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
mason: ">=0.1.0-dev.51 <0.1.0"
meta: ^1.11.0

dev_dependencies:
mocktail: ^1.0.0
test: ^1.19.2
very_good_analysis: ^5.1.0
93 changes: 93 additions & 0 deletions brick/hooks/test/post_gen_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'dart:io';

import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

import '../post_gen.dart' as post_gen;

class _MockHookContext extends Mock implements HookContext {}

class _MockProcessResult extends Mock implements ProcessResult {}

void main() {
group('post_gen', () {
late HookContext context;
late ProcessResult processResult;
late List<Invocation> invocations;

setUp(() {
context = _MockHookContext();
processResult = _MockProcessResult();
invocations = [];
});

Future<ProcessResult> runProcess(
String executable,
List<String> arguments, {
String? workingDirectory,
bool runInShell = false,
}) async {
final positionalArguments = [executable, arguments];
final namedArguments = {
const Symbol('workingDirectory'): workingDirectory,
const Symbol('runInShell'): runInShell,
};
final invocation = Invocation.method(
const Symbol('runProcess'),
positionalArguments,
namedArguments,
);
invocations.add(invocation);

return processResult;
}

test('fixes `directives_ordering` Dart linter rule', () async {
await post_gen.run(context, runProcess: runProcess);

expect(invocations, contains(_IsDartDirectiveOrderingFix()));
});
});
}

Matcher isDartDirectiveOrderingFix() {
return _IsDartDirectiveOrderingFix();
}

class _IsDartDirectiveOrderingFix extends Matcher {
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
if (item is! Invocation) {
return false;
}

final invocation = item;
final executableName = invocation.positionalArguments[0] as String;
final arguments = invocation.positionalArguments[1] as List<String>;
final workingDirectory =
invocation.namedArguments[const Symbol('workingDirectory')] as String?;

return executableName == 'dart' &&
arguments.contains('fix') &&
arguments.contains(Directory.current.path) &&
arguments.contains('--apply') &&
arguments.contains('--code=directives_ordering') &&
workingDirectory == null;
}

@override
Description describe(Description description) {
return description.add('is a Dart fix for directives_ordering');
}

@override
Description describeMismatch(
dynamic item,
Description mismatchDescription,
Map<dynamic, dynamic> matchState,
bool verbose,
) {
return mismatchDescription.add('is not a Dart fix for directives_ordering');
}
}

0 comments on commit 9e4aba0

Please sign in to comment.