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:
@@ -74,3 +74,28 @@ test('backupName and passphraseProblem', () => {
|
||||
assert.ok(passphraseProblem('elevenchars'));
|
||||
assert.equal(passphraseProblem('twelve chars!'), '');
|
||||
});
|
||||
|
||||
// ---- version ----
|
||||
import { APP_VERSION, isVersion, nextVersion, versionDate } from '../src/version.js';
|
||||
|
||||
test('APP_VERSION is a valid Ver YYMMDDX', () => {
|
||||
assert.equal(isVersion(APP_VERSION), true);
|
||||
assert.equal(isVersion('261109B'), true);
|
||||
for (const bad of ['', '26110B', '2611091', '261109b', '261109AA', null, 5]) assert.equal(isVersion(bad), false, String(bad));
|
||||
});
|
||||
|
||||
test('nextVersion: next letter same day, A on a new day, error past Z', () => {
|
||||
const d = new Date(2026, 10, 9, 15, 0); // 9 Nov 2026 local
|
||||
assert.equal(versionDate(d), '261109');
|
||||
assert.equal(nextVersion('261109A', d), '261109B');
|
||||
assert.equal(nextVersion('261109Y', d), '261109Z');
|
||||
assert.throws(() => nextVersion('261109Z', d), /26 versions/);
|
||||
assert.equal(nextVersion('261108D', d), '261109A');
|
||||
assert.equal(nextVersion('261109C', new Date(2027, 0, 2)), '270102A');
|
||||
assert.throws(() => nextVersion('nope', d), /Not a version/);
|
||||
});
|
||||
|
||||
test('the built page carries the version', async () => {
|
||||
const { bundleJs } = await import('../build.js'); // importing must not run a build
|
||||
assert.ok(bundleJs().includes(`APP_VERSION = '${APP_VERSION}'`));
|
||||
});
|
||||
|
||||
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