Fixes found during a full audit of the CLI: - auth logout: remote token revocation always failed with 401 because Forgejo only accepts Basic auth on the token endpoints. Logout now asks for (or accepts) the account password, supports --password, --password-file and --local-only, and clearly reports when the token is left active. - Silent password prompt actually echoed the password on a TTY: overriding rl.write does not suppress readline echo. Switched to the callback readline module and mute _writeToOutput instead (the readline/promises interface does not honor that hook). - Global --config flag was silently ignored: config paths were resolved at require time, before the preAction hook set STOKE_CONFIG_FILE. Paths are now resolved lazily on every access. - XDG_CONFIG_HOME handling put the config in $XDG_CONFIG_HOME/.config/stoke; per the XDG spec it now resolves to $XDG_CONFIG_HOME/stoke. - repo create: --auto-init defaulted to true with no way to disable it; added --no-auto-init. - repo import/import-batch: a GitHub token was required even for non-GitHub services (e.g. --service git), making those imports fail without gh/GITHUB_TOKEN. Tokens are now only auto-resolved for the github service; batch imports resolve per entry and memoize. - Branding leftovers: 'Run: forgejo auth login' hint and forgejo-cli/1.0.0 User-Agent now say stoke (UA tracks pkg.version). - Added request timeouts (30s default, 10m for migrations). - --limit and --team-id are validated as integers instead of silently misbehaving on garbage (NaN made -l show all results). New commands (per the repo's every-operation-becomes-a-command design): - stoke issue create (title/body/body-file/assignees) - stoke pr create (head/base/title/body/body-file) Tests and metadata: - New test suite on the built-in node:test runner (25 tests) covering config resolution/persistence, the API client with a mocked fetch, and end-to-end CLI behavior. npm test previously matched no files. - package.json: engines >=22.12.0 (required by commander@15 — the README claimed Node 18), repository, keywords, author; version 1.1.0. - README: corrected Node requirement, documented repo rename (was missing), issue create, pr create, logout options and revocation caveat, --no-auto-init, XDG behavior, import token rules, testing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
const { test, beforeEach, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const os = require('node:os');
|
|
const path = require('node:path');
|
|
|
|
const config = require('../src/config');
|
|
|
|
const ENV_KEYS = [
|
|
'STOKE_CONFIG_DIR', 'STOKE_CONFIG_FILE',
|
|
'FORGEJO_CONFIG_DIR', 'FORGEJO_CONFIG_FILE',
|
|
'XDG_CONFIG_HOME',
|
|
];
|
|
|
|
let savedEnv;
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
savedEnv = {};
|
|
for (const key of ENV_KEYS) {
|
|
savedEnv[key] = process.env[key];
|
|
delete process.env[key];
|
|
}
|
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'stoke-test-'));
|
|
});
|
|
|
|
afterEach(() => {
|
|
for (const key of ENV_KEYS) {
|
|
if (savedEnv[key] === undefined) delete process.env[key];
|
|
else process.env[key] = savedEnv[key];
|
|
}
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
});
|
|
|
|
test('default config path lives under ~/.config/stoke', () => {
|
|
assert.equal(config.getConfigPath(), path.join(os.homedir(), '.config', 'stoke', 'config.json'));
|
|
});
|
|
|
|
test('XDG_CONFIG_HOME replaces ~/.config entirely', () => {
|
|
process.env.XDG_CONFIG_HOME = tmpDir;
|
|
assert.equal(config.getConfigPath(), path.join(tmpDir, 'stoke', 'config.json'));
|
|
});
|
|
|
|
test('STOKE_CONFIG_FILE overrides everything', () => {
|
|
process.env.XDG_CONFIG_HOME = '/elsewhere';
|
|
process.env.STOKE_CONFIG_FILE = path.join(tmpDir, 'custom.json');
|
|
assert.equal(config.getConfigPath(), path.join(tmpDir, 'custom.json'));
|
|
});
|
|
|
|
test('config path is resolved lazily (STOKE_CONFIG_FILE set after require)', () => {
|
|
const first = path.join(tmpDir, 'a.json');
|
|
const second = path.join(tmpDir, 'b.json');
|
|
process.env.STOKE_CONFIG_FILE = first;
|
|
assert.equal(config.getConfigPath(), first);
|
|
process.env.STOKE_CONFIG_FILE = second;
|
|
assert.equal(config.getConfigPath(), second);
|
|
});
|
|
|
|
test('save/load/clear round-trip with restrictive permissions', () => {
|
|
process.env.STOKE_CONFIG_FILE = path.join(tmpDir, 'nested', 'config.json');
|
|
|
|
assert.equal(config.loadConfig(), null);
|
|
|
|
const data = { url: 'https://example.test', token: 'abc', tokenId: 1 };
|
|
config.saveConfig(data);
|
|
assert.deepEqual(config.loadConfig(), data);
|
|
|
|
if (process.platform !== 'win32') {
|
|
const mode = fs.statSync(config.getConfigPath()).mode & 0o777;
|
|
assert.equal(mode, 0o600);
|
|
}
|
|
|
|
config.clearConfig();
|
|
assert.equal(config.loadConfig(), null);
|
|
// clearing twice must not throw
|
|
config.clearConfig();
|
|
});
|
|
|
|
test('loadConfig surfaces corrupt config files as errors', () => {
|
|
process.env.STOKE_CONFIG_FILE = path.join(tmpDir, 'corrupt.json');
|
|
fs.writeFileSync(process.env.STOKE_CONFIG_FILE, 'not-json');
|
|
assert.throws(() => config.loadConfig(), /Failed to read config/);
|
|
});
|