Skip to content

Commit 80558b3

Browse files
toph-allendotNomad
andauthored
chore: add tests to usage metrics dashboard (#260)
* Move testable functions out of app.R * Add tests * Rename variables related to the session window * Update CI for usage metrics dashboard * Remove usage metrics dashboard from simple extension process * make extension look like a package to testthat * Update file list in workflow * Correctly load files before running tests * update workflow * respond to feedback * Update extensions/usage-metrics-dashboard/CHANGELOG.md Co-authored-by: Jordan Jensen <[email protected]> * remove unclear sentence. --------- Co-authored-by: Jordan Jensen <[email protected]>
1 parent 9a87176 commit 80558b3

17 files changed

+330
-78
lines changed

.github/workflows/extensions.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ jobs:
5252
script-r: extensions/script-r/**
5353
top-5-income-share-streamlit: extensions/top-5-income-share-streamlit/**
5454
stock-report-jupyter: extensions/stock-report-jupyter/**
55-
usage-metrics-dashboard: extensions/usage-metrics-dashboard/**
5655
content-health-monitor: extensions/content-health-monitor/**
5756
voila-example: extensions/voila-example/**
5857
stock-report: extensions/stock-report/**
@@ -190,6 +189,7 @@ jobs:
190189
publisher-command-center: ${{ steps.changes.outputs.publisher-command-center }}
191190
package-vulnerability-scanner: ${{ steps.changes.outputs.package-vulnerability-scanner }}
192191
runtime-version-scanner: ${{ steps.changes.outputs.runtime-version-scanner }}
192+
usage-metrics-dashboard: ${{ steps.changes.outputs.usage-metrics-dashboard }}
193193

194194
steps:
195195
- uses: actions/checkout@v4
@@ -205,6 +205,7 @@ jobs:
205205
publisher-command-center: extensions/publisher-command-center/**
206206
package-vulnerability-scanner: extensions/package-vulnerability-scanner/**
207207
runtime-version-scanner: extensions/runtime-version-scanner/**
208+
usage-metrics-dashboard: extensions/usage-metrics-dashboard/**
208209
209210
# Creates and releases the Publisher Command Center extension using a custom
210211
# workflow
@@ -234,6 +235,15 @@ jobs:
234235
uses: ./.github/workflows/runtime-version-scanner.yml
235236
secrets: inherit
236237

238+
# Creates and releases the Usage Metrics Dashboard extension using a custom workflow
239+
usage-metrics-dashboard:
240+
needs: [complex-extension-changes]
241+
# Only runs if the `complex-extension-changes` job detects changes in the
242+
# usage-metrics-dashboard extension directory
243+
if: ${{ needs.complex-extension-changes.outputs.usage-metrics-dashboard == 'true' }}
244+
uses: ./.github/workflows/usage-metrics-dashboard.yml
245+
secrets: inherit
246+
237247
# All extensions have been linted, packaged, and released, if necessary
238248
# Continuing to update the extension list with the latest release data
239249

@@ -248,7 +258,8 @@ jobs:
248258
simple-extension-release,
249259
publisher-command-center,
250260
package-vulnerability-scanner,
251-
runtime-version-scanner
261+
runtime-version-scanner,
262+
usage-metrics-dashboard,
252263
]
253264
if: ${{ always() }}
254265
outputs:
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Usage Metrics Dashboard
2+
3+
# Re-usable workflows use the `workflow_call` trigger
4+
# https://docs.github.com/en/actions/sharing-automations/reusing-workflows#creating-a-reusable-workflow
5+
on:
6+
workflow_call:
7+
8+
# Setup the environment with the extension name for easy re-use
9+
# Also set the GH_TOKEN for the release-extension action to be able to use gh
10+
env:
11+
EXTENSION_NAME: usage-metrics-dashboard
12+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13+
14+
jobs:
15+
extension:
16+
runs-on: ubuntu-latest
17+
defaults:
18+
run:
19+
working-directory: ./extensions/${{ env.EXTENSION_NAME }}
20+
21+
steps:
22+
# Checkout the repository so the rest of the actions can run with no issue
23+
- uses: actions/checkout@v4
24+
25+
# We want to fail quickly if the linting fails, do that first
26+
- uses: ./.github/actions/lint-extension
27+
with:
28+
extension-name: ${{ env.EXTENSION_NAME }}
29+
30+
# ---
31+
# Run R tests
32+
# ---
33+
34+
- uses: r-lib/actions/setup-r@v2
35+
with:
36+
r-version: '4.3.3'
37+
38+
- uses: r-lib/actions/setup-renv@v2
39+
with:
40+
working-directory: extensions/${{ env.EXTENSION_NAME }}
41+
42+
- run: Rscript -e 'install.packages(c("testthat"))'
43+
working-directory: extensions/${{ env.EXTENSION_NAME }}
44+
45+
- run: Rscript -e 'testthat::test_local()'
46+
working-directory: extensions/${{ env.EXTENSION_NAME }}
47+
48+
# Now that the extension is built we need to upload an artifact to pass
49+
# to the package-extension action that contains the files we want to be
50+
# included in the extension
51+
# This only includes necessary files for the extension to run leaving out
52+
# the files that were used to build the /dist/ directory
53+
- name: Upload built extension
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: ${{ env.EXTENSION_NAME }}
57+
# Replace the below with the files your content needs
58+
path: |
59+
extensions/${{ env.EXTENSION_NAME }}/app.R
60+
extensions/${{ env.EXTENSION_NAME }}/R/
61+
extensions/${{ env.EXTENSION_NAME }}/renv.lock
62+
extensions/${{ env.EXTENSION_NAME }}/www/styles.css
63+
extensions/${{ env.EXTENSION_NAME }}/manifest.json
64+
65+
# Package up the extension into a TAR using the package-extension action
66+
- uses: ./.github/actions/package-extension
67+
with:
68+
extension-name: ${{ env.EXTENSION_NAME }}
69+
artifact-name: ${{ env.EXTENSION_NAME }}
70+
71+
connect-integration-tests:
72+
needs: extension
73+
uses: ./.github/workflows/connect-integration-tests.yml
74+
secrets: inherit
75+
with:
76+
extensions: '["usage-metrics-dashboard"]' # JSON array format to match the workflow input schema
77+
78+
release:
79+
runs-on: ubuntu-latest
80+
needs: [extension, connect-integration-tests]
81+
# Release the extension using the release-extension action
82+
# Will only create a GitHub release if merged to `main` and the semver
83+
# version has been updated
84+
steps:
85+
# Checkout the repository so the rest of the actions can run with no issue
86+
- uses: actions/checkout@v4
87+
88+
- uses: ./.github/actions/release-extension
89+
with:
90+
extension-name: ${{ env.EXTENSION_NAME }}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^renv$
2+
^renv\.lock$
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tests
2+
DESCRIPTION
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
All notable changes to the Usage Metrics Dashboard extension will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
9+
## [Unreleased]
10+
11+
### Added
12+
13+
- Added CHANGELOG.md to the repo
14+
- Added tests for business logic. In support of that, moved source files to `R/` and added a `DESCRIPTION` file.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributing to Usage Metrics Dashboard
2+
3+
## Organization
4+
5+
- The app's core code lives in `app.R`.
6+
- Supporting functions live in `R/`.
7+
8+
## Tests
9+
10+
- Run `make test` to run tests.
11+
- Tests live in `tests/testthat`, and run against the code in `R/`.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Package: usage-metrics-dashboard
2+
Version: 1.0.7
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: test update-manifest
2+
3+
test:
4+
Rscript -e 'testthat::test_local()'
5+
6+
# This recipe updates the manifest, with a few helpful modifications:
7+
# - Copies over `extension` and `environment` blocks from the old manifest to
8+
# the new one. These are Gallery-specific blocks, and not created by
9+
# `rsconnect`.
10+
# - Preserves set of files listed in the `files` block, where
11+
# `rsconnect::writeManifest()` by default includes all the files in the
12+
# directory.
13+
update-manifest:
14+
cp manifest.json manifest.old.json
15+
FILES=$$(jq -r '.files | keys | join(",")' manifest.old.json); \
16+
Rscript -e "rsconnect::writeManifest(appFiles = strsplit('$$FILES', ',')[[1]])"; \
17+
jq -n --slurpfile old manifest.old.json --slurpfile new manifest.json \
18+
'$$new[0] * {"environment": $$old[0].environment, "extension": $$old[0].extension}' \
19+
> manifest.merged.json
20+
mv manifest.merged.json manifest.json
21+
rm manifest.old.json

extensions/usage-metrics-dashboard/get_usage.R renamed to extensions/usage-metrics-dashboard/R/get_usage.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ usage_dtype <- tibble::tibble(
1111
"data" = NA_list_
1212
)
1313

14-
to_iso8601 <- function(x) {
15-
strftime(x, "%Y-%m-%dT%H:%M:%S%z") |>
14+
to_iso8601 <- function(x, tz = "") {
15+
strftime(x, "%Y-%m-%dT%H:%M:%S%z", tz = tz) |>
1616
sub("([+-]\\d{2})(\\d{2})$", "\\1:\\2", x = _)
1717
}
1818

File renamed without changes.

0 commit comments

Comments
 (0)