From acf189eaf709f659f9e63ac94580745c7e316038 Mon Sep 17 00:00:00 2001
From: chris48s <chris48s@users.noreply.github.com>
Date: Thu, 25 Aug 2022 18:02:03 +0100
Subject: [PATCH] migrate 'main' tests to GH actions (#8332)

* implement 'main' tests using GH actions

* remove 'main' builds from Circle CI config

* give jobs better names
---
 .circleci/config.yml                  | 70 ---------------------------
 .github/actions/core-tests/action.yml | 21 ++++++++
 .github/actions/setup/action.yml      | 25 ++++++++++
 .github/workflows/test-lint.yml       | 26 ++++++++++
 .github/workflows/test-main-17.yml    | 23 +++++++++
 .github/workflows/test-main.yml       | 21 ++++++++
 scripts/mocha2md.js                   | 36 ++++++++++++++
 7 files changed, 152 insertions(+), 70 deletions(-)
 create mode 100644 .github/actions/core-tests/action.yml
 create mode 100644 .github/actions/setup/action.yml
 create mode 100644 .github/workflows/test-lint.yml
 create mode 100644 .github/workflows/test-main-17.yml
 create mode 100644 .github/workflows/test-main.yml
 create mode 100644 scripts/mocha2md.js

diff --git a/.circleci/config.yml b/.circleci/config.yml
index d94d1ab254..d7c9574cb3 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -1,47 +1,5 @@
 version: 2
 
-main_steps: &main_steps
-  steps:
-    - checkout
-
-    - run:
-        name: Install dependencies
-        command: |
-          npm ci
-        environment:
-          # https://docs.cypress.io/guides/getting-started/installing-cypress.html#Skipping-installation
-          # We don't need to install the Cypress binary in jobs that aren't actually running Cypress.
-          CYPRESS_INSTALL_BINARY: 0
-
-    - run:
-        name: Linter
-        when: always
-        command: npm run lint
-
-    - run:
-        name: Core tests
-        when: always
-        environment:
-          mocha_reporter: mocha-junit-reporter
-          MOCHA_FILE: junit/core/results.xml
-        command: npm run test:core
-
-    - run:
-        name: Entrypoint tests
-        when: always
-        environment:
-          mocha_reporter: mocha-junit-reporter
-          MOCHA_FILE: junit/entrypoint/results.xml
-        command: npm run test:entrypoint
-
-    - store_test_results:
-        path: junit
-
-    - run:
-        name: 'Prettier check (quick fix: `npm run prettier`)'
-        when: always
-        command: npm run prettier:check
-
 integration_steps: &integration_steps
   steps:
     - checkout
@@ -140,20 +98,6 @@ package_steps: &package_steps
         path: junit
 
 jobs:
-  main:
-    docker:
-      - image: cimg/node:16.15
-
-    <<: *main_steps
-
-  main@node-17:
-    docker:
-      - image: cimg/node:17.9
-    environment:
-      NPM_CONFIG_ENGINE_STRICT: 'false'
-
-    <<: *main_steps
-
   integration:
     docker:
       - image: cimg/node:16.15
@@ -297,14 +241,6 @@ workflows:
 
   on-commit:
     jobs:
-      - main:
-          filters:
-            branches:
-              ignore: gh-pages
-      - main@node-17:
-          filters:
-            branches:
-              ignore: gh-pages
       - integration@node-17:
           filters:
             branches:
@@ -346,12 +282,6 @@ workflows:
   #         filters:
   #           branches:
   #             ignore: gh-pages
-  #     - main:
-  #         requires:
-  #           - npm-install
-  #     - main@node-latest:
-  #         requires:
-  #           - npm-install
   #     - frontend:
   #         requires:
   #           - npm-install
diff --git a/.github/actions/core-tests/action.yml b/.github/actions/core-tests/action.yml
new file mode 100644
index 0000000000..9f5f1e4d31
--- /dev/null
+++ b/.github/actions/core-tests/action.yml
@@ -0,0 +1,21 @@
+name: 'Core tests'
+description: 'Run core and entrypoint tests'
+runs:
+  using: 'composite'
+  steps:
+    - name: Core tests
+      if: always()
+      run: npm run test:core -- --reporter json --reporter-option 'output=reports/core.json'
+      shell: bash
+
+    - name: Entrypoint tests
+      if: always()
+      run: npm run test:entrypoint -- --reporter json --reporter-option 'output=reports/entrypoint.json'
+      shell: bash
+
+    - name: Write Markdown Summary
+      if: always()
+      run: |
+        node scripts/mocha2md.js Core reports/core.json >> $GITHUB_STEP_SUMMARY
+        node scripts/mocha2md.js Entrypoint reports/entrypoint.json >> $GITHUB_STEP_SUMMARY
+      shell: bash
diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml
new file mode 100644
index 0000000000..0c516f018d
--- /dev/null
+++ b/.github/actions/setup/action.yml
@@ -0,0 +1,25 @@
+name: 'Set up project'
+description: 'Set up project'
+inputs:
+  node-version:
+    description: 'Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0.'
+    required: true
+  cypress:
+    description: 'Install Cypress binary: 0 or 1'
+    # https://docs.cypress.io/guides/getting-started/installing-cypress.html#Skipping-installation
+    # We don't need to install the Cypress binary in jobs that aren't actually running Cypress.
+    required: false
+    default: 0
+runs:
+  using: 'composite'
+  steps:
+    - name: Install Node JS ${{ inputs.node-version }}
+      uses: actions/setup-node@v2
+      with:
+        node-version: ${{ inputs.node-version }}
+
+    - name: Install dependencies
+      env:
+        CYPRESS_INSTALL_BINARY: ${{ inputs.cypress }}
+      run: npm ci
+      shell: bash
diff --git a/.github/workflows/test-lint.yml b/.github/workflows/test-lint.yml
new file mode 100644
index 0000000000..b05c8f8b34
--- /dev/null
+++ b/.github/workflows/test-lint.yml
@@ -0,0 +1,26 @@
+name: Lint
+on:
+  pull_request:
+  push:
+    branches-ignore:
+      - 'gh-pages'
+
+jobs:
+  test-lint:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v3
+
+      - name: Setup
+        uses: ./.github/actions/setup
+        with:
+          node-version: 16
+
+      - name: ESLint
+        if: always()
+        run: npm run lint
+
+      - name: 'Prettier check (quick fix: `npm run prettier`)'
+        if: always()
+        run: npm run prettier:check
diff --git a/.github/workflows/test-main-17.yml b/.github/workflows/test-main-17.yml
new file mode 100644
index 0000000000..daabb51cc1
--- /dev/null
+++ b/.github/workflows/test-main-17.yml
@@ -0,0 +1,23 @@
+name: Main@node 17
+on:
+  pull_request:
+  push:
+    branches-ignore:
+      - 'gh-pages'
+
+jobs:
+  test-main-17:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v3
+
+      - name: Setup
+        uses: ./.github/actions/setup
+        with:
+          node-version: 17
+        env:
+          NPM_CONFIG_ENGINE_STRICT: 'false'
+
+      - name: Core tests
+        uses: ./.github/actions/core-tests
diff --git a/.github/workflows/test-main.yml b/.github/workflows/test-main.yml
new file mode 100644
index 0000000000..e07463cd21
--- /dev/null
+++ b/.github/workflows/test-main.yml
@@ -0,0 +1,21 @@
+name: Main
+on:
+  pull_request:
+  push:
+    branches-ignore:
+      - 'gh-pages'
+
+jobs:
+  test-main:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v3
+
+      - name: Setup
+        uses: ./.github/actions/setup
+        with:
+          node-version: 16
+
+      - name: Core tests
+        uses: ./.github/actions/core-tests
diff --git a/scripts/mocha2md.js b/scripts/mocha2md.js
new file mode 100644
index 0000000000..6c9f4a009a
--- /dev/null
+++ b/scripts/mocha2md.js
@@ -0,0 +1,36 @@
+import fs from 'fs'
+
+let data
+let title
+
+try {
+  if (process.argv.length < 4) {
+    throw new Error()
+  }
+  title = process.argv[2]
+  data = JSON.parse(fs.readFileSync(process.argv[3]))
+} catch (e) {
+  process.stdout.write('failed to write summary\n')
+  process.exit(1)
+}
+
+process.stdout.write(`# ${title}\n\n`)
+
+if (data.stats.passes > 0) {
+  process.stdout.write(`✔ ${data.stats.passes} passed\n`)
+}
+if (data.stats.failures > 0) {
+  process.stdout.write(`✖ ${data.stats.failures} failed\n\n`)
+}
+
+if (data.stats.failures > 0) {
+  for (const test of data.tests) {
+    if (test.err && Object.keys(test.err).length > 0) {
+      process.stdout.write(`### ${test.title}\n\n`)
+      process.stdout.write(`${test.fullTitle}\n\n`)
+      process.stdout.write('```\n')
+      process.stdout.write(`${test.err.stack}\n`)
+      process.stdout.write('```\n\n')
+    }
+  }
+}
-- 
GitLab