From 72cbb4827390ccd6d5b7403ff8579fc95480a63d Mon Sep 17 00:00:00 2001 From: Thomas Clark Date: Mon, 29 May 2023 10:31:41 +0100 Subject: [PATCH 1/2] feat: add support for custom output formatting --- lib/src/generate_for_element.dart | 9 ++-- lib/src/test_annotated_classes.dart | 18 ++++++-- test/generate_for_element_test.dart | 69 +++++++++++++++++++++++++++++ test/init_library_reader_test.dart | 1 + test/src/test_library.dart | 3 ++ 5 files changed, 93 insertions(+), 7 deletions(-) diff --git a/lib/src/generate_for_element.dart b/lib/src/generate_for_element.dart index b6e433a..7a7a543 100644 --- a/lib/src/generate_for_element.dart +++ b/lib/src/generate_for_element.dart @@ -19,8 +19,11 @@ final _testAnnotationWarnings = {}; Future generateForElement( GeneratorForAnnotation generator, LibraryReader libraryReader, - String name, -) async { + String name, { + String Function(String code)? formatOutput, +}) async { + formatOutput ??= _formatter.format; + final elements = libraryReader.allElements.where((e) => e.name == name).toList(); @@ -94,7 +97,7 @@ Future generateForElement( final generated = await generatedStream.join('\n\n'); - return _formatter.format(generated); + return formatOutput(generated); } // ignore: subtype_of_sealed_class diff --git a/lib/src/test_annotated_classes.dart b/lib/src/test_annotated_classes.dart index 92f5e59..228e006 100644 --- a/lib/src/test_annotated_classes.dart +++ b/lib/src/test_annotated_classes.dart @@ -29,6 +29,7 @@ void testAnnotatedElements( Map>? additionalGenerators, Iterable? expectedAnnotatedTests, Iterable? defaultConfiguration, + String Function(String code)? formatOutput, }) { for (var entry in getAnnotatedClasses( libraryReader, @@ -36,6 +37,7 @@ void testAnnotatedElements( additionalGenerators: additionalGenerators, expectedAnnotatedTests: expectedAnnotatedTests, defaultConfiguration: defaultConfiguration, + formatOutput: formatOutput, )) { entry._registerTest(); } @@ -50,6 +52,7 @@ List> getAnnotatedClasses( Map>? additionalGenerators, Iterable? expectedAnnotatedTests, Iterable? defaultConfiguration, + String Function(String code)? formatOutput, }) { final generators = >{ _defaultConfigurationName: defaultGenerator @@ -174,6 +177,7 @@ List> getAnnotatedClasses( configuration, entry.elementName, entry.expectation, + formatOutput: formatOutput, ), ); } @@ -205,6 +209,7 @@ class AnnotatedTest { final LibraryReader _libraryReader; final TestExpectation expectation; final String _elementName; + final String Function(String code)? _formatOutput; String get _testName { var value = _elementName; @@ -219,8 +224,9 @@ class AnnotatedTest { this.generator, this.configuration, this._elementName, - this.expectation, - ); + this.expectation, { + String Function(String code)? formatOutput, + }) : _formatOutput = formatOutput; void _registerTest() { if (expectation is ShouldGenerate) { @@ -233,8 +239,12 @@ class AnnotatedTest { throw StateError('Should never get here.'); } - Future _generate() => - generateForElement(generator, _libraryReader, _elementName); + Future _generate() => generateForElement( + generator, + _libraryReader, + _elementName, + formatOutput: _formatOutput, + ); Future _shouldGenerateTest() async { final output = await _generate(); diff --git a/test/generate_for_element_test.dart b/test/generate_for_element_test.dart index 7937501..24ea0d6 100644 --- a/test/generate_for_element_test.dart +++ b/test/generate_for_element_test.dart @@ -98,6 +98,75 @@ const TestClass2NameLowerCase = testclass2; ''', ); }); + + group( + 'formatOutput', + () { + test( + 'should format the generated source code with `DartFormatter` when `formatOutput` is `null`.', + () async { + final output = await generateForElement( + const TestGenerator(), + reader, + 'TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop', + formatOutput: null, + ); + printOnFailure(output); + expect( + output, + r''' +const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLength = + 68; + +const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLowerCase = + testclassthathasaverylongnamethatshouldnotwrapwhenformatoutputisanop; +''', + ); + }, + ); + + test( + 'should not format the generated source code when `formatOutput` is a NOP.', + () async { + final output = await generateForElement( + const TestGenerator(), + reader, + 'TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop', + formatOutput: (code) => code, + ); + printOnFailure(output); + expect( + output, + r''' +const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLength = 68; + +const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLowerCase = testclassthathasaverylongnamethatshouldnotwrapwhenformatoutputisanop;''', + ); + }, + ); + + test( + 'should format the generated source code with a custom formatter when `formatOutput` is a custom formatter.', + () async { + final output = await generateForElement( + const TestGenerator(), + reader, + 'TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop', + formatOutput: (code) => '$code\n', + ); + printOnFailure(output); + expect( + output, + r''' +const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLength = 68; + +const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLowerCase = testclassthathasaverylongnamethatshouldnotwrapwhenformatoutputisanop; +''', + ); + }, + ); + }, + ); }); test('throwsInvalidGenerationSourceError', () async { diff --git a/test/init_library_reader_test.dart b/test/init_library_reader_test.dart index ca384df..416f78e 100644 --- a/test/init_library_reader_test.dart +++ b/test/init_library_reader_test.dart @@ -32,6 +32,7 @@ void main() { 'class TestClassWithBadMember', 'int badTestFunc()', 'int badTestField', + 'class TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop', 'class TestClass2', 'import source /source_gen_test/lib/annotations.dart', 'import source /__test__/lib/test_annotation.dart', diff --git a/test/src/test_library.dart b/test/src/test_library.dart index 274822f..a57c4b6 100644 --- a/test/src/test_library.dart +++ b/test/src/test_library.dart @@ -78,3 +78,6 @@ int badTestFunc() => 42; ) @TestAnnotation() const badTestField = 42; + +@TestAnnotation() +class TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop {} From 44b008bb0d6ec9701aaa3f728a5a5101e259ddad Mon Sep 17 00:00:00 2001 From: Thomas Clark Date: Tue, 19 Dec 2023 14:21:20 +0000 Subject: [PATCH 2/2] ci: remove github actions workflows --- .github/dependabot.yml | 12 ------- .github/workflows/ci.yml | 60 ------------------------------- .github/workflows/no-response.yml | 37 ------------------- .github/workflows/publish.yaml | 17 --------- 4 files changed, 126 deletions(-) delete mode 100644 .github/dependabot.yml delete mode 100644 .github/workflows/ci.yml delete mode 100644 .github/workflows/no-response.yml delete mode 100644 .github/workflows/publish.yaml diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index c84404d..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,12 +0,0 @@ -# Set update schedule for GitHub Actions -# See https://docs.github.com/en/free-pro-team@latest/github/administering-a-repository/keeping-your-actions-up-to-date-with-dependabot - -version: 2 -updates: - -- package-ecosystem: github-actions - directory: / - schedule: - interval: monthly - labels: - - autosubmit diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 41105e1..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,60 +0,0 @@ -name: CI - -on: - # Run on PRs and pushes to the default branch. - push: - branches: [ master ] - pull_request: - branches: [ master ] - schedule: - - cron: "0 0 * * 0" - -env: - PUB_ENVIRONMENT: bot.github - -jobs: - # Check code formatting and static analysis on a single OS (linux) - # against Dart dev. - analyze: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - sdk: [dev] - steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - - uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d - with: - sdk: ${{ matrix.sdk }} - - id: install - name: Install dependencies - run: dart pub get - - name: Check formatting - run: dart format --output=none --set-exit-if-changed . - if: always() && steps.install.outcome == 'success' - - name: Analyze code - run: dart analyze --fatal-infos - if: always() && steps.install.outcome == 'success' - - # Run tests on a matrix consisting of two dimensions: - # 1. OS: ubuntu-latest, (macos-latest, windows-latest) - # 2. release channel: dev - test: - needs: analyze - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - # Add macos-latest and/or windows-latest if relevant for this package. - os: [ubuntu-latest] - sdk: [3.0.0, dev] - steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - - uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d - with: - sdk: ${{ matrix.sdk }} - - id: install - name: Install dependencies - run: dart pub get - - run: dart test --run-skipped - if: always() && steps.install.outcome == 'success' diff --git a/.github/workflows/no-response.yml b/.github/workflows/no-response.yml deleted file mode 100644 index 0947831..0000000 --- a/.github/workflows/no-response.yml +++ /dev/null @@ -1,37 +0,0 @@ -# A workflow to close issues where the author hasn't responded to a request for -# more information; see https://github.com/actions/stale. - -name: No Response - -# Run as a daily cron. -on: - schedule: - # Every day at 8am - - cron: '0 8 * * *' - -# All permissions not specified are set to 'none'. -permissions: - issues: write - pull-requests: write - -jobs: - no-response: - runs-on: ubuntu-latest - if: ${{ github.repository_owner == 'kevmoo' }} - steps: - - uses: actions/stale@1160a2240286f5da8ec72b1c0816ce2481aabf84 - with: - # Don't automatically mark inactive issues+PRs as stale. - days-before-stale: -1 - # Close needs-info issues and PRs after 14 days of inactivity. - days-before-close: 14 - stale-issue-label: "needs-info" - close-issue-message: > - Without additional information we're not able to resolve this issue. - Feel free to add more info or respond to any questions above and we - can reopen the case. Thanks for your contribution! - stale-pr-label: "needs-info" - close-pr-message: > - Without additional information we're not able to resolve this PR. - Feel free to add more info or respond to any questions above. - Thanks for your contribution! diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml deleted file mode 100644 index d95988a..0000000 --- a/.github/workflows/publish.yaml +++ /dev/null @@ -1,17 +0,0 @@ -# A CI configuration to auto-publish pub packages. - -name: Publish - -on: - pull_request: - branches: [ master ] - push: - tags: [ 'v[0-9]+.[0-9]+.[0-9]+' ] - -jobs: - publish: - if: ${{ github.repository_owner == 'kevmoo' }} - uses: dart-lang/ecosystem/.github/workflows/publish.yaml@main - permissions: - id-token: write # Required for authentication using OIDC - pull-requests: write # Required for writing the pull request note