Show the app version at the top of the page (Ver YYMMDDX)
Adds src/version.js, bump-version.js and a pre-commit hook that bumps the version in commits that change the app. Closes #15 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
54
test/bump.test.js
Normal file
54
test/bump.test.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFileSync, spawnSync } from 'node:child_process';
|
||||
import { mkdtempSync, mkdirSync, copyFileSync, writeFileSync, readFileSync, chmodSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { needsBump, isAppPath } from '../bump-version.js';
|
||||
import { versionDate } from '../src/version.js';
|
||||
|
||||
const repo = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
|
||||
test('needsBump: only app changes, and only if the version was not already bumped', () => {
|
||||
assert.equal(needsBump(['src/app.js'], '261109A', '261109A'), true);
|
||||
assert.equal(needsBump(['build.js'], '261109A', '261109A'), true);
|
||||
assert.equal(needsBump(['README.md', 'docs/MAINTENANCE.md', 'test/x.test.js', 'OVERVIEW.md'], '261109A', '261109A'), false);
|
||||
assert.equal(needsBump(['src/version.js'], '261109A', '261109A'), false);
|
||||
assert.equal(needsBump(['src/app.js', 'src/version.js'], '261109A', '261109B'), false); // already bumped by hand
|
||||
assert.equal(needsBump(['src/app.js'], null, '261109A'), false); // first commit of the file
|
||||
assert.equal(isAppPath('src/style.css'), true);
|
||||
});
|
||||
|
||||
const hasGit = spawnSync('git', ['--version']).status === 0;
|
||||
test('pre-commit hook bumps the version in the commit that changes the app', { skip: !hasGit && 'git not found' }, () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'fw-bump-'));
|
||||
try {
|
||||
const run = (...a) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', ...a], { cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
mkdirSync(join(dir, 'src')); mkdirSync(join(dir, '.githooks'));
|
||||
for (const f of ['bump-version.js', 'package.json', 'src/version.js', '.githooks/pre-commit']) copyFileSync(join(repo, f), join(dir, f));
|
||||
chmodSync(join(dir, '.githooks/pre-commit'), 0o755);
|
||||
const today = versionDate(new Date());
|
||||
const setVersion = (v) => writeFileSync(join(dir, 'src/version.js'), readFileSync(join(dir, 'src/version.js'), 'utf8').replace(/APP_VERSION = '[^']+'/, `APP_VERSION = '${v}'`));
|
||||
const version = () => /APP_VERSION = '([^']+)'/.exec(run('show', 'HEAD:src/version.js'))[1];
|
||||
run('init', '-q'); run('config', 'core.hooksPath', '.githooks');
|
||||
setVersion('200101A');
|
||||
writeFileSync(join(dir, 'src/app.js'), '1'); writeFileSync(join(dir, 'README.md'), 'a');
|
||||
run('add', '-A'); run('commit', '-q', '-m', 'first');
|
||||
assert.equal(version(), '200101A'); // first commit of the file is left alone
|
||||
|
||||
writeFileSync(join(dir, 'README.md'), 'b'); run('add', '-A'); run('commit', '-q', '-m', 'docs only');
|
||||
assert.equal(version(), '200101A'); // docs do not bump
|
||||
|
||||
writeFileSync(join(dir, 'src/app.js'), '2'); run('add', '-A'); run('commit', '-q', '-m', 'app change');
|
||||
assert.equal(version(), today + 'A'); // new day starts at A, and the bump is inside that commit
|
||||
assert.equal(run('status', '--porcelain').trim(), '');
|
||||
|
||||
writeFileSync(join(dir, 'src/app.js'), '3'); run('add', '-A'); run('commit', '-q', '-m', 'app change 2');
|
||||
assert.equal(version(), today + 'B');
|
||||
|
||||
setVersion(today + 'Z'); run('add', '-A'); run('commit', '-q', '-m', 'hand bump');
|
||||
writeFileSync(join(dir, 'src/app.js'), '4'); run('add', '-A'); run('commit', '-q', '-m', 'overflow');
|
||||
assert.equal(version(), today + 'Z'); // past Z it warns and keeps Z, the commit still succeeds
|
||||
} finally { rmSync(dir, { recursive: true, force: true }); }
|
||||
});
|
||||
Reference in New Issue
Block a user