Skip to content

Commit

Permalink
ci: add tests for regex
Browse files Browse the repository at this point in the history
Add tests for the update bundle filename regex.

Signed-off-by: Markus Tacker <[email protected]>
  • Loading branch information
coderbyheart authored and gregersrygg committed Oct 9, 2024
1 parent ca78ae5 commit 918299f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/lib/nameRegEx.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const nameRegEx = (version) =>
new RegExp(
`^hello\.nrfcloud\.com-${version}(\\+(?<configuration>[0-9A-Za-z.]+))?-thingy91x-nrf91-update-signed\.bin$`
);
34 changes: 34 additions & 0 deletions .github/workflows/lib/nameRegEx.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { nameRegEx } from "./nameRegEx.mjs";
import assert from "node:assert/strict";
import { describe, it } from "node:test";

describe("nameRegEx", () => {
it("should match a valid filename without configuration", () =>
assert.ok(
nameRegEx("1.0.0").test(
"hello.nrfcloud.com-1.0.0-thingy91x-nrf91-update-signed.bin"
)
));

it("should match a valid filename with configuration", () =>
assert.ok(
nameRegEx("1.0.0").test(
"hello.nrfcloud.com-1.0.0+debug-thingy91x-nrf91-update-signed.bin"
)
));

it("should not match an invalid filename", () => {
const version = "1.0.0";
const regex = nameRegEx(version);
const filename = "invalid-filename.bin";
assert.ok(!regex.test(filename));
});

it("should not match a filename with a different version", () => {
const version = "1.0.0";
const regex = nameRegEx(version);
const filename =
"hello.nrfcloud.com-2.0.0-thingy91x-nrf91-update-signed.bin";
assert.ok(!regex.test(filename));
});
});
23 changes: 23 additions & 0 deletions .github/workflows/publish-firmware-bundles-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Firmware Bundles Tests

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 22

- name: Run tests
run: node --test ./.github/workflows/**/*.test.mjs
10 changes: 4 additions & 6 deletions .github/workflows/register-firmware-bundles.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import { mkdtemp } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import yazl from "yazl";
import { nameRegEx } from "./lib/nameRegEx.mjs";

const version = (process.argv[process.argv.length - 1] ?? "").trim();
const filenameRegEx = nameRegEx(version);

console.log(`Publishing release version`, version);

const nameRegEx = new RegExp(
`^hello\.nrfcloud\.com-${version}(\\+(?<configuration>[0-9A-Za-z.]+))?-thingy91x-nrf91-update-signed\.bin$`
);

const assets = fs
.readdirSync(process.cwd())
.filter((name) => nameRegEx.test(name));
.filter((name) => filenameRegEx.test(name));

if (assets.length === 0) {
console.error(`No assets found for release ${version}!`);
Expand All @@ -25,7 +23,7 @@ if (assets.length === 0) {
for (const asset of assets) {
const {
groups: { configuration },
} = nameRegEx.exec(asset);
} = filenameRegEx.exec(asset);

const fwversion = `${version}${
configuration !== undefined ? `+${configuration}` : ""
Expand Down

0 comments on commit 918299f

Please sign in to comment.