From da29bc2261a183a48b0ade6e359fb2605c3a940d Mon Sep 17 00:00:00 2001 From: thecoderatekid Date: Thu, 7 Aug 2025 20:56:35 +0000 Subject: [PATCH] Update .gitea/workflows/nodejs-test.yml --- .gitea/workflows/nodejs-test.yml | 54 ++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/.gitea/workflows/nodejs-test.yml b/.gitea/workflows/nodejs-test.yml index 5916436..c788b24 100644 --- a/.gitea/workflows/nodejs-test.yml +++ b/.gitea/workflows/nodejs-test.yml @@ -4,22 +4,31 @@ on: [push] jobs: nodejs-test: runs-on: ubuntu-latest + steps: - name: Checkout code uses: actions/checkout@v4 - - name: Set up Node.js + # Use caching only when a lockfile exists (avoids setup-node error) + - name: Set up Node.js (with cache) + if: ${{ hashFiles('**/package-lock.json','**/npm-shrinkwrap.json','**/yarn.lock') != '' }} uses: actions/setup-node@v4 with: - node-version: '20' # or '18' if you need older + node-version: '20' cache: 'npm' + - name: Set up Node.js (no cache) + if: ${{ hashFiles('**/package-lock.json','**/npm-shrinkwrap.json','**/yarn.lock') == '' }} + uses: actions/setup-node@v4 + with: + node-version: '20' + - name: Verify Node & npm run: | node -v npm -v - - name: Detect & install deps (lockfile-aware) + run tests + - name: Install deps and run tests (or scaffold a smoke test) shell: bash run: | set -euo pipefail @@ -27,43 +36,40 @@ jobs: if [ -f package.json ]; then echo "package.json found." - if [ -f package-lock.json ]; then - echo "Using npm ci (lockfile present)." + if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then + echo "Lockfile present -> npm ci" npm ci else - echo "No lockfile; using npm install." + echo "No lockfile -> npm install" npm install fi - # Run tests only if there's a real test script (not the default placeholder) - if node -e "const p=require('./package.json'); const s=p.scripts&&p.scripts.test; process.exit(!(s && !/no test specified/.test(String(s))))"; then + # Run tests only if there's a meaningful test script (not the default) + if node -e "const p=require('./package.json'); const s=p.scripts&&p.scripts.test; process.exit(!(s && !/no test specified/i.test(String(s))));"; then npm test else echo "No meaningful test script found; skipping tests." fi else - echo "No Node project detected. Creating a tiny smoke-test project..." + echo "No Node project detected -> scaffold a tiny Mocha test" npm init -y >/dev/null 2>&1 npm install --save-dev mocha >/dev/null 2>&1 npm pkg set scripts.test="mocha" >/dev/null 2>&1 - # Write minimal app and test (note: heredoc terminators aligned under run: |) - cat > sum.js <<'EOF' - function add(a, b) { return a + b; } - module.exports = { add }; - EOF + printf '%s\n' \ + 'function add(a, b) { return a + b; }' \ + 'module.exports = { add };' > sum.js - mkdir -p test - cat > test/sum.test.js <<'EOF' - const assert = require('assert'); - const { add } = require('../sum'); - describe('add', () => { - it('adds numbers', () => { - assert.strictEqual(add(2, 2), 4); - }); - }); - EOF + mkdir -p test + printf '%s\n' \ + "const assert = require('assert');" \ + "const { add } = require('../sum');" \ + "describe('add', () => {" \ + " it('adds numbers', () => {" \ + " assert.strictEqual(add(2, 2), 4);" \ + " });" \ + "});" > test/sum.test.js npm test fi