diff --git a/.github/workflows/_test:checks.yaml b/.github/workflows/_test:checks.yaml index c4b268b..74b2a78 100644 --- a/.github/workflows/_test:checks.yaml +++ b/.github/workflows/_test:checks.yaml @@ -12,14 +12,19 @@ on: jobs: checks: + name: ${{ matrix.fixture }} + strategy: + matrix: + fixture: [dart-dev, dart-basic] uses: ./.github/workflows/checks.yaml with: - package-path: __tests__/fixtures/checks + package-path: __tests__/fixtures/checks/${{ matrix.fixture }} + checks-with-additional: uses: ./.github/workflows/checks.yaml with: - package-path: __tests__/fixtures/checks + package-path: __tests__/fixtures/checks/dart-dev additional-checks: | echo "all good!" dart format diff --git a/.github/workflows/_test:test-unit.yaml b/.github/workflows/_test:test-unit.yaml index c353209..e345451 100644 --- a/.github/workflows/_test:test-unit.yaml +++ b/.github/workflows/_test:test-unit.yaml @@ -11,12 +11,12 @@ on: - .github/workflows/_test:test-unit.yaml jobs: - test-unit-without-chrome: - uses: ./.github/workflows/test-unit.yaml - with: - package-path: __tests__/fixtures/test-unit/without-chrome + test-unit: + name: ${{ matrix.fixture }} + strategy: + matrix: + fixture: [with-chrome, without-chrome, dart-basic, dart-dev, build-runner] - test-unit-with-chrome: uses: ./.github/workflows/test-unit.yaml with: - package-path: __tests__/fixtures/test-unit/with-chrome \ No newline at end of file + package-path: __tests__/fixtures/test-unit/${{ matrix.fixture }} \ No newline at end of file diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index f633f30..792a861 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -27,7 +27,12 @@ jobs: with: sdk: ${{ inputs.sdk }} - working-directory: ${{ inputs.package-path }} - run: dart run dart_dev analyze + run: | + if (grep -q "^ dart_dev:" pubspec.yaml); then + dart run dart_dev analyze + else + dart analyze . + fi format: runs-on: ubuntu-latest @@ -38,7 +43,12 @@ jobs: sdk: ${{ inputs.sdk }} - name: Run format working-directory: ${{ inputs.package-path }} - run: dart run dart_dev format --check + run: | + if (grep -q "^ dart_dev:" pubspec.yaml); then + dart run dart_dev format --check + else + dart format --output=none --set-exit-if-changed . + fi dependency-validator: runs-on: ubuntu-latest diff --git a/.github/workflows/test-unit.yaml b/.github/workflows/test-unit.yaml index e76f2f7..2e37747 100644 --- a/.github/workflows/test-unit.yaml +++ b/.github/workflows/test-unit.yaml @@ -7,17 +7,6 @@ on: documentation on presets for more information. type: string - browser-aggregation: - description: > - Whether or not to run the test with the --browser-aggregation flag. See test_html_builder's - readme for more information: https://github.com/Workiva/test_html_builder#aggregating-browser-tests - default: false - type: boolean - - test-inputs: - description: Optional path(s) to a specific test file(s) passed to the dart test command - type: string - test-args: description: Arguments passed directly to the dart test command type: string @@ -41,32 +30,50 @@ jobs: pre: runs-on: ubuntu-latest outputs: - has-chrome-platform: ${{ steps.analyze.outputs.HAS_CHROME_PLATFORM }} + chrome-platform: ${{ steps.analyze-dart-test.outputs.chrome-platform }} + dart-dev: ${{ steps.analyze-pubspec.outputs.dart-dev }} + build-runner: ${{ steps.analyze-pubspec.outputs.build-runner }} + build-test: ${{ steps.analyze-pubspec.outputs.build-test }} steps: - uses: actions/checkout@v4 - - working-directory: ${{ inputs.package-path }} - id: analyze + - name: Analyze dart_test.yaml + working-directory: ${{ inputs.package-path }} + id: analyze-dart-test run: | if [[ -f 'dart_test.yaml' ]]; then platforms=$(yq '.platforms[], .presets.*.platforms[]' dart_test.yaml) if [[ "$platforms" == *"chrome"* ]]; then echo "Project has chrome platform config" - echo "HAS_CHROME_PLATFORM=true" >> $GITHUB_OUTPUT + echo "chrome-platform=true" >> $GITHUB_OUTPUT exit 0 fi fi echo "Project does not have chrome platform config" - echo "HAS_CHROME_PLATFORM=false" >> $GITHUB_OUTPUT + echo "chrome-platform=false" >> $GITHUB_OUTPUT + - name: Analyze pubspec.yaml + working-directory: ${{ inputs.package-path }} + id: analyze-pubspec + run: | + echo "dart-dev=$(grep -q "^ dart_dev:" pubspec.yaml && echo "true" || echo "false")" >> $GITHUB_OUTPUT + echo "build-runner=$(grep -q "^ build_runner:" pubspec.yaml && echo "true" || echo "false")" >> $GITHUB_OUTPUT + echo "build-test=$(grep -q "^ build_test:" pubspec.yaml && echo "true" || echo "false")" >> $GITHUB_OUTPUT + + - name: Print outputs + run: | + echo "chrome-platform: ${{ steps.analyze-dart-test.outputs.chrome-platform }}" + echo "dart-dev: ${{ steps.analyze-pubspec.outputs.dart-dev }}" + echo "build-runner: ${{ steps.analyze-pubspec.outputs.build-runner }}" + echo "build-test: ${{ steps.analyze-pubspec.outputs.build-test }}" unit: runs-on: ubuntu-latest needs: pre - name: ${{ matrix.release-mode && 'dev' || 'release' }} + name: ${{ matrix.release-mode && 'release' || 'dev' }} strategy: fail-fast: false matrix: - release-mode: ${{ fromJson(needs.pre.outputs.has-chrome-platform == 'true' && '[true, false]' || '[false]') }} + release-mode: ${{ fromJson(needs.pre.outputs.chrome-platform == 'true' && '[true, false]' || '[false]') }} steps: - uses: actions/checkout@v4 - uses: dart-lang/setup-dart@v1 @@ -74,28 +81,46 @@ jobs: sdk: ${{ inputs.sdk }} - name: Setup Chrome - if: needs.pre.outputs.has-chrome-platform == 'true' + if: needs.pre.outputs.chrome-platform == 'true' uses: browser-actions/setup-chrome@v1 with: chrome-version: stable + - name: Setup Chrome Alias - if: needs.pre.outputs.has-chrome-platform == 'true' + if: needs.pre.outputs.chrome-platform == 'true' run: alias google-chrome="/opt/hostedtoolcache/chromium/stable/x64/chrome" - name: Pub get working-directory: ${{ inputs.package-path }} run: dart pub get - - name: Run dart_dev unit tests + - name: Run tests with dart_dev working-directory: ${{ inputs.package-path }} + if: needs.pre.outputs.dart-dev == 'true' run: | args=() if [[ "${{ matrix.release-mode }}" == "true" ]]; then args+=(--release) fi - if [[ "${{ inputs.browser-aggregation }}" == "true" ]]; then - args+=(--browser-aggregation) + if [[ "${{ inputs.preset }}" != "" ]]; then + args+=(-P ${{ inputs.preset }}) + fi + + if [[ "${{ inputs.test-args }}" != "" ]]; then + args+=(--test-args="${{ inputs.test-args }}") + fi + + echo "timeout ${{ inputs.timeout }}m dart run dart_dev test "${args[@]}"" + timeout ${{ inputs.timeout }}m dart run dart_dev test "${args[@]}" + + - name: Run tests with build_runner + working-directory: ${{ inputs.package-path }} + if: needs.pre.outputs.dart-dev == 'false' && needs.pre.outputs.build-runner == 'true' && needs.pre.outputs.build-test == 'true' + run: | + args=() + if [[ "${{ matrix.release-mode }}" == "true" ]]; then + args+=(--release) fi if [[ "${{ inputs.preset }}" != "" ]]; then @@ -103,7 +128,29 @@ jobs: fi if [[ "${{ inputs.test-args }}" != "" ]]; then - args+=(--test-args="${{ inputs.test-args }}") + args+=(-- "${{ inputs.test-args }}") + fi + + echo "timeout ${{ inputs.timeout }}m dart run build_runner test "${args[@]}"" + timeout ${{ inputs.timeout }}m dart run build_runner test "${args[@]}" + + - name: Run tests with dart test + working-directory: ${{ inputs.package-path }} + if: needs.pre.outputs.dart-dev == 'false' && needs.pre.outputs.build-runner == 'false' && needs.pre.outputs.build-test == 'false' + run: | + args=() + if [[ "${{ matrix.release-mode }}" == "true" ]]; then + echo "::error:: release-mode is only supported with dart_dev or build_runner" + exit 1 + fi + + if [[ "${{ inputs.preset }}" != "" ]]; then + args+=(-P ${{ inputs.preset }}) + fi + + if [[ "${{ inputs.test-args }}" != "" ]]; then + args+=("${{ inputs.test-args }}") fi - timeout ${{ inputs.timeout }}m dart run dart_dev test "${args[@]}" ${{ inputs.test-inputs }} \ No newline at end of file + echo "timeout ${{ inputs.timeout }}m dart test "${args[@]}"" + timeout ${{ inputs.timeout }}m dart test "${args[@]}" \ No newline at end of file diff --git a/README.md b/README.md index 6453212..a582e64 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Workflows for use with [Workiva's](https://github.com/Workiva) OpenSource Dart projects. -The majority of workflows assume [dart_dev](https://github.com/Workiva/dart_dev) is installed and being used within the repo +Workflows support configuration with [`dart_dev`](https://github.com/Workiva/dart_dev), [`build_runner`](https://pub.dev/packages/build_runner), and basic dart packages. ## Basic Dart CI Setup @@ -26,7 +26,8 @@ jobs: build: uses: Workiva/gha-dart-oss/.github/workflows/build.yaml@v1.0.0 - # Runs unit tests in both d2js and ddc + # Runs unit tests in dev mode (vm/ddc), and optionally `--release` if executing + # against a webdev configured repo unit-tests: uses: Workiva/gha-dart-oss/.github/workflows/test-unit.yaml@v1.0.0 ``` diff --git a/__tests__/fixtures/checks/lib/main.dart b/__tests__/fixtures/checks/dart-basic/lib/main.dart similarity index 100% rename from __tests__/fixtures/checks/lib/main.dart rename to __tests__/fixtures/checks/dart-basic/lib/main.dart diff --git a/__tests__/fixtures/checks/dart-basic/pubspec.yaml b/__tests__/fixtures/checks/dart-basic/pubspec.yaml new file mode 100644 index 0000000..5dcbb99 --- /dev/null +++ b/__tests__/fixtures/checks/dart-basic/pubspec.yaml @@ -0,0 +1,4 @@ +name: test_pubspec +publish_to: none +environment: + sdk: '>=2.10.0 <3.0.0' \ No newline at end of file diff --git a/__tests__/fixtures/checks/dart-dev/lib/main.dart b/__tests__/fixtures/checks/dart-dev/lib/main.dart new file mode 100644 index 0000000..d254ccd --- /dev/null +++ b/__tests__/fixtures/checks/dart-dev/lib/main.dart @@ -0,0 +1,9 @@ +final newVar = 'asdf'; + +String foo(int inp) { + return inp.toString(); +} + +void main() { + print(foo(4)); +} diff --git a/__tests__/fixtures/checks/pubspec.yaml b/__tests__/fixtures/checks/dart-dev/pubspec.yaml similarity index 82% rename from __tests__/fixtures/checks/pubspec.yaml rename to __tests__/fixtures/checks/dart-dev/pubspec.yaml index 1408ba0..d576078 100644 --- a/__tests__/fixtures/checks/pubspec.yaml +++ b/__tests__/fixtures/checks/dart-dev/pubspec.yaml @@ -1,4 +1,4 @@ -name: checks_test +name: test_pubspec publish_to: none environment: sdk: '>=2.10.0 <3.0.0' diff --git a/__tests__/fixtures/test-unit/build-runner/pubspec.yaml b/__tests__/fixtures/test-unit/build-runner/pubspec.yaml new file mode 100644 index 0000000..bacbef4 --- /dev/null +++ b/__tests__/fixtures/test-unit/build-runner/pubspec.yaml @@ -0,0 +1,9 @@ +name: test_pubspec +publish_to: none +environment: + sdk: '>=2.11.0 <3.0.0' + +dependencies: + test: ^1.21.1 + build_runner: ^2.0.0 + build_test: ^2.2.1 diff --git a/__tests__/fixtures/test-unit/build-runner/test/main_test.dart b/__tests__/fixtures/test-unit/build-runner/test/main_test.dart new file mode 100644 index 0000000..45fa263 --- /dev/null +++ b/__tests__/fixtures/test-unit/build-runner/test/main_test.dart @@ -0,0 +1,7 @@ +import 'package:test/test.dart'; + +void main() { + test('some test', () { + expect(1, 1); + }); +} \ No newline at end of file diff --git a/__tests__/fixtures/test-unit/dart-basic/pubspec.yaml b/__tests__/fixtures/test-unit/dart-basic/pubspec.yaml new file mode 100644 index 0000000..8b16af7 --- /dev/null +++ b/__tests__/fixtures/test-unit/dart-basic/pubspec.yaml @@ -0,0 +1,7 @@ +name: test_pubspec +publish_to: none +environment: + sdk: '>=2.11.0 <3.0.0' + +dependencies: + test: ^1.21.1 diff --git a/__tests__/fixtures/test-unit/dart-basic/test/main_test.dart b/__tests__/fixtures/test-unit/dart-basic/test/main_test.dart new file mode 100644 index 0000000..45fa263 --- /dev/null +++ b/__tests__/fixtures/test-unit/dart-basic/test/main_test.dart @@ -0,0 +1,7 @@ +import 'package:test/test.dart'; + +void main() { + test('some test', () { + expect(1, 1); + }); +} \ No newline at end of file diff --git a/__tests__/fixtures/test-unit/dart-dev/pubspec.yaml b/__tests__/fixtures/test-unit/dart-dev/pubspec.yaml new file mode 100644 index 0000000..3a1fa8a --- /dev/null +++ b/__tests__/fixtures/test-unit/dart-dev/pubspec.yaml @@ -0,0 +1,8 @@ +name: test_pubspec +publish_to: none +environment: + sdk: '>=2.11.0 <3.0.0' + +dependencies: + test: ^1.21.1 + dart_dev: ^4.0.2 diff --git a/__tests__/fixtures/test-unit/dart-dev/test/main_test.dart b/__tests__/fixtures/test-unit/dart-dev/test/main_test.dart new file mode 100644 index 0000000..45fa263 --- /dev/null +++ b/__tests__/fixtures/test-unit/dart-dev/test/main_test.dart @@ -0,0 +1,7 @@ +import 'package:test/test.dart'; + +void main() { + test('some test', () { + expect(1, 1); + }); +} \ No newline at end of file diff --git a/__tests__/fixtures/test-unit/with-chrome/pubspec.yaml b/__tests__/fixtures/test-unit/with-chrome/pubspec.yaml index 439b36e..7f9fd4c 100644 --- a/__tests__/fixtures/test-unit/with-chrome/pubspec.yaml +++ b/__tests__/fixtures/test-unit/with-chrome/pubspec.yaml @@ -1,4 +1,4 @@ -name: unit_test_with_chrome +name: test_pubspec publish_to: none environment: sdk: '>=2.11.0 <3.0.0' diff --git a/__tests__/fixtures/test-unit/without-chrome/pubspec.yaml b/__tests__/fixtures/test-unit/without-chrome/pubspec.yaml index a25a0ed..3a1fa8a 100644 --- a/__tests__/fixtures/test-unit/without-chrome/pubspec.yaml +++ b/__tests__/fixtures/test-unit/without-chrome/pubspec.yaml @@ -1,4 +1,4 @@ -name: unit_test +name: test_pubspec publish_to: none environment: sdk: '>=2.11.0 <3.0.0'