Repository navigation
157 lines (150 loc) · 5.87 KB
/
Copy pathnpm-test-coverage.yml
File metadata and controls
157 lines (150 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# This source file is part of the Schmiedmayer Lab open-source organization
#
# SPDX-FileCopyrightText: 2026 Schmiedmayer Lab and the project authors (see CONTRIBUTORS.md)
#
# SPDX-License-Identifier: MIT
#
name: Test npm Package and Upload Coverage
on:
workflow_call:
inputs:
nodeVersionFile:
description: |
Repository-relative Node.js version file.
required: false
type: string
default: '.nvmrc'
npmVersion:
description: |
Optional exact stable npm version override.
Defaults to the exact packageManager version in package.json.
required: false
type: string
default: ''
working-directory:
description: |
The working directory where the project is located. Defaults to $GITHUB_WORKSPACE.
required: false
type: string
default: '.'
coverage-files:
description: |
Optional comma-separated coverage report paths passed to Codecov.
Disables automatic report discovery when supplied.
required: false
type: string
default: ''
setup-firebase-emulator:
description: |
Set up the Firebase Emulator.
required: false
type: boolean
default: false
secrets:
token:
description: |
The Codecov token used for private repositories.
required: false
GOOGLE_APPLICATION_CREDENTIALS_BASE64:
description: |
The Base64-encoded private key JSON file used to start the Firebase Emulator.
Only needed if Cloud Functions must run in the emulator.
required: false
permissions:
contents: read
jobs:
testandcoverage:
name: Test npm Package and Upload Coverage
runs-on: ubuntu-latest
timeout-minutes: 30
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version-file: ${{ inputs.nodeVersionFile }}
cache: 'npm'
cache-dependency-path: '${{ inputs.working-directory }}/package-lock.json'
- name: Use the repository npm version
env:
NPM_VERSION_OVERRIDE: ${{ inputs.npmVersion }}
WORKING_DIRECTORY: ${{ inputs.working-directory }}
run: |
set -euo pipefail
NPM_VERSION="$NPM_VERSION_OVERRIDE"
if [ -z "$NPM_VERSION" ]; then
NPM_VERSION="$(node --input-type=module -e '
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const { packageManager } = JSON.parse(
readFileSync(resolve(process.env.WORKING_DIRECTORY, "package.json"), "utf8"),
);
const match = /^npm@((?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*))$/.exec(packageManager ?? "");
if (!match) throw new Error("package.json must declare an exact npm packageManager version.");
console.log(match[1]);
')"
fi
if [[ ! "$NPM_VERSION" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then
echo "npmVersion must be an exact stable semantic version without leading zeroes."
exit 1
fi
npm install --global --ignore-scripts "npm@$NPM_VERSION"
INSTALLED_NPM_VERSION="$(npm --version)"
if [ "$INSTALLED_NPM_VERSION" != "$NPM_VERSION" ]; then
echo "Expected npm $NPM_VERSION, but installed $INSTALLED_NPM_VERSION."
exit 1
fi
- name: Install Node.js dependencies
run: npm ci
- name: Cache Firebase emulators
if: ${{ runner.environment == 'github-hosted' && inputs.setup-firebase-emulator }}
uses: actions/cache@v6
with:
path: ~/.cache/firebase/emulators
key: firebase-emulators-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles(format('{0}/package-lock.json', inputs.working-directory)) }}
- name: Set up Java
if: ${{ runner.environment == 'github-hosted' && inputs.setup-firebase-emulator }}
uses: actions/setup-java@v6
with:
distribution: 'microsoft'
java-version: '21'
- name: Verify Firebase CLI tools
if: ${{ runner.environment == 'github-hosted' && inputs.setup-firebase-emulator }}
run: |
FIREBASE_BINARY="node_modules/.bin/firebase"
if [ ! -x "$FIREBASE_BINARY" ]; then
echo "firebase-tools must be installed in the caller's lockfile."
exit 1
fi
- name: Run the tests
if: ${{ !inputs.setup-firebase-emulator }}
run: npm test
- name: Run the tests with Firebase Emulator support
if: ${{ inputs.setup-firebase-emulator }}
env:
GOOGLE_APPLICATION_CREDENTIALS_BASE64: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS_BASE64 }}
run: |
if [ -n "$GOOGLE_APPLICATION_CREDENTIALS_BASE64" ]; then
printf '%s' "$GOOGLE_APPLICATION_CREDENTIALS_BASE64" | base64 --decode > "$RUNNER_TEMP/google-application-credentials.json"
export GOOGLE_APPLICATION_CREDENTIALS="$RUNNER_TEMP/google-application-credentials.json"
fi
npm test
- name: Clean up Google application credentials
if: ${{ always() && inputs.setup-firebase-emulator }}
working-directory: ${{ github.workspace }}
run: rm -f "$RUNNER_TEMP/google-application-credentials.json"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v7
if: ${{ always() }}
with:
token: ${{ secrets.token }}
working-directory: ${{ inputs.working-directory }}
files: ${{ inputs.coverage-files }}
disable_search: ${{ inputs.coverage-files != '' }}
fail_ci_if_error: true