Skip to content

Commit

Permalink
enh(tests): Add unit tests and CI workflow for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jan 25, 2024
1 parent 83aa6e5 commit 63ecffb
Show file tree
Hide file tree
Showing 10 changed files with 1,434 additions and 328 deletions.
169 changes: 169 additions & 0 deletions .github/workflows/node-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
name: Node tests

on:
pull_request:
schedule:
- cron: "5 2 * * *"

concurrency:
group: node-tests-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
changes:
runs-on: ubuntu-latest

outputs:
src: ${{ steps.changes.outputs.src}}

steps:
- uses: dorny/paths-filter@4512585405083f25c027a35db413c2b3b9006d50 # v2.11.1
id: changes
continue-on-error: true
with:
filters: |
src:
- '.github/workflows/**'
- '**/__tests__/**'
- '**/__mocks__/**'
- '**/src/**'
- '**/appinfo/info.xml'
- 'package.json'
- 'package-lock.json'
- 'tsconfig.json'
- '**.js'
- '**.ts'
- '**.vue'
- '**.handlebars'
versions:
runs-on: ubuntu-latest
needs: changes

if: ${{ github.repository_owner != 'nextcloud-gmbh' && needs.changes.outputs.src != 'false' }}

outputs:
nodeVersion: ${{ steps.versions.outputs.nodeVersion }}
npmVersion: ${{ steps.versions.outputs.npmVersion }}

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Read package.json node and npm engines version
uses: skjnldsv/read-package-engines-version-actions@8205673bab74a63eb9b8093402fd9e0e018663a1
id: versions
with:
fallbackNode: '^20'
fallbackNpm: '^9'

test:
runs-on: ubuntu-latest
needs: [versions, changes]

if: ${{ needs.versions.result != 'failure' && needs.changes.outputs.src != 'false' }}

env:
CYPRESS_INSTALL_BINARY: 0
PUPPETEER_SKIP_DOWNLOAD: true

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Set up node ${{ needs.versions.outputs.nodeVersion }}
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8
with:
node-version: ${{ needs.versions.outputs.nodeVersion }}

- name: Set up npm ${{ needs.versions.outputs.npmVersion }}
run: npm i -g npm@"${{ needs.versions.outputs.npmVersion }}"

- name: Install dependencies
run: npm ci

- name: Show cypress version
run: npm run cypress:version

- name: Test and process coverage
run: npm run test:coverage

- name: Collect coverage
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
with:
files: ./coverage/lcov.info

jsunit:
runs-on: ubuntu-latest
needs: [versions, changes]

if: ${{ needs.versions.result != 'failure' && needs.changes.outputs.src != 'false' }}

env:
CYPRESS_INSTALL_BINARY: 0

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Set up node ${{ needs.versions.outputs.nodeVersion }}
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8
with:
node-version: ${{ needs.versions.outputs.nodeVersion }}

- name: Set up npm ${{ needs.versions.outputs.npmVersion }}
run: npm i -g npm@"${{ needs.versions.outputs.npmVersion }}"

- name: Install dependencies
run: npm ci

- name: Show cypress version
run: npm run cypress:version

- name: Test
run: npm run test:jsunit

handlebars:
runs-on: ubuntu-latest
needs: [versions, changes]

if: ${{ needs.versions.result != 'failure' && needs.changes.outputs.src != 'false' }}

env:
CYPRESS_INSTALL_BINARY: 0
PUPPETEER_SKIP_DOWNLOAD: true

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Set up node ${{ needs.versions.outputs.nodeVersion }}
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8
with:
node-version: ${{ needs.versions.outputs.nodeVersion }}

- name: Set up npm ${{ needs.versions.outputs.npmVersion }}
run: npm i -g npm@"${{ needs.versions.outputs.npmVersion }}"

- name: Install dependencies
run: npm ci

- name: Show cypress version
run: npm run cypress:version

- name: Run compile
run: ./build/compile-handlebars-templates.sh

summary:
permissions:
contents: none
runs-on: ubuntu-latest
needs: [changes, test, jsunit, handlebars]

if: always()

name: node-test-summary

steps:
- name: Summary status
run: if ${{ needs.changes.outputs.src != 'false' && needs.test.result != 'success' && needs.jsunit.result != 'success' && needs.handlebars.result != 'success' }}; then exit 1; fi
132 changes: 132 additions & 0 deletions __tests__/paths.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* @copyright 2024 Ferdinand Thiessen <[email protected]
*
* @author Ferdinand Thiessen <[email protected]
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { beforeAll, beforeEach, describe, expect, test } from 'vitest'
import { generateFilePath, imagePath, linkTo } from '../lib/index'

declare global {
interface Window {
_oc_appswebroots?: Record<string, string|undefined>
_oc_webroot?: string
OC?: Record<string, unknown>
}
}

describe('Path generation', () => {
beforeAll(() => {
window.OC = {
coreApps: ['', 'admin', 'log', 'core/search', 'core', '3rdparty'],
}
})

describe('generateFilePath', () => {
beforeEach(() => {
window._oc_webroot = ''
window._oc_appswebroots = { forms: '/apps-extra/forms' }
})

test('non core PHP index file', () => {
expect(generateFilePath('forms', '', 'index.php')).toBe('/index.php/apps/forms')
})

// TODO: This feels wrong, I would expect `/index.php/apps/forms/templates`
test('non core PHP index files with type', () => {
expect(generateFilePath('forms', 'templates', 'index.php')).toBe('/index.php/apps/forms')
})

test('non core PHP file', () => {
expect(generateFilePath('forms', '', 'version.php')).toBe('/index.php/apps/forms/version.php')
})

test('non core PHP file with type', () => {
expect(generateFilePath('forms', 'templates', 'version.php')).toBe('/index.php/apps/forms/templates/version.php')
})

test('non core file', () => {
expect(generateFilePath('forms', '', 'file.js')).toBe('/apps-extra/forms/file.js')
})

test('non core file with type', () => {
expect(generateFilePath('forms', 'js', 'file.js')).toBe('/apps-extra/forms/js/file.js')
})

test('core PHP file with ajax type', () => {
expect(generateFilePath('admin', 'ajax', 'file.php')).toBe('/admin/ajax/file.php')
})

test('special core PHP file with ajax type', () => {
expect(generateFilePath('core', 'ajax', 'file.php')).toBe('/index.php/core/ajax/file.php')
})

test('empty app file path', () => {
expect(generateFilePath('', '', 'file.php')).toBe('/file.php')
})

test('empty app file path with type', () => {
expect(generateFilePath('', 'ajax', 'file.php')).toBe('/ajax/file.php')
})
})

describe('linkTo', () => {
test('non core PHP index file', () => {
expect(linkTo('forms', 'index.php')).toBe('/index.php/apps/forms')
})

test('non core PHP file', () => {
expect(linkTo('forms', 'version.php')).toBe('/index.php/apps/forms/version.php')
})

test('non core file', () => {
expect(linkTo('forms', 'file.js')).toBe('/apps-extra/forms/file.js')
})

test('empty app file path', () => {
expect(linkTo('', 'file.php')).toBe('/file.php')
})
})

describe('imagePath', () => {
test('non core file without extension', () => {
expect(imagePath('forms', 'color')).toBe('/apps-extra/forms/img/color.svg')
})

test('non core file with extension', () => {
expect(imagePath('forms', 'color.png')).toBe('/apps-extra/forms/img/color.png')
})

test('core file without extension', () => {
expect(imagePath('admin', 'color')).toBe('/admin/img/color.svg')
})

test('core file with extension', () => {
expect(imagePath('admin', 'color.png')).toBe('/admin/img/color.png')
})

test('empty app without extension', () => {
expect(imagePath('', 'color')).toBe('/img/color.svg')
})

test('empty app without extension', () => {
expect(imagePath('', 'color.png')).toBe('/img/color.png')
})
})
})
7 changes: 7 additions & 0 deletions __tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"include": ["./**.ts"],
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": ".."
}
}
Loading

0 comments on commit 63ecffb

Please sign in to comment.