|
1 | 1 | import { Config } from '@oclif/core'; |
2 | | -import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
3 | 3 | import os from 'node:os'; |
4 | 4 | import path from 'node:path'; |
5 | 5 | import { fileURLToPath } from 'node:url'; |
6 | | -import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { stripVTControlCharacters } from 'node:util'; |
| 7 | +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
7 | 8 |
|
| 9 | +import { onFinalExit } from '../utils/on-final-exit.js'; |
8 | 10 | import hook from './init.js'; |
9 | 11 |
|
| 12 | +vi.mock('../utils/on-final-exit.js', () => ({ onFinalExit: vi.fn() })); |
| 13 | + |
10 | 14 | const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); |
11 | 15 |
|
12 | 16 | describe('init hook (autoupdate)', () => { |
@@ -56,3 +60,118 @@ describe('init hook (autoupdate)', () => { |
56 | 60 | rmSync(tmp, { force: true, recursive: true }); |
57 | 61 | }); |
58 | 62 | }); |
| 63 | + |
| 64 | +describe('init hook (update notice)', () => { |
| 65 | + const env = { ...process.env }; |
| 66 | + const isTTY = process.stderr.isTTY; |
| 67 | + let tmp: string; |
| 68 | + let calls: string; |
| 69 | + let cacheDir: string; |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + tmp = mkdtempSync(path.join(os.tmpdir(), 'codify-hook-')); |
| 73 | + calls = path.join(tmp, 'calls'); |
| 74 | + cacheDir = path.join(tmp, 'cache'); |
| 75 | + const bin = path.join(tmp, 'codify'); |
| 76 | + writeFileSync(bin, `#!/bin/sh\necho "$@" >> "${calls}"\n`, { mode: 0o755 }); |
| 77 | + process.env = { |
| 78 | + ...env, |
| 79 | + CODIFY_BINPATH: bin, |
| 80 | + CODIFY_CACHE_DIR: cacheDir, |
| 81 | + CODIFY_DATA_DIR: path.join(tmp, 'data'), |
| 82 | + CODIFY_DISABLE_AUTOUPDATE: 'true', |
| 83 | + }; |
| 84 | + delete process.env.CI; |
| 85 | + delete process.env.CODIFY_DISABLE_UPDATE_NOTIFIER; |
| 86 | + process.stderr.isTTY = true; |
| 87 | + vi.mocked(onFinalExit).mockClear(); |
| 88 | + }); |
| 89 | + |
| 90 | + afterEach(() => { |
| 91 | + process.stderr.isTTY = isTTY; |
| 92 | + rmSync(tmp, { force: true, recursive: true }); |
| 93 | + }); |
| 94 | + |
| 95 | + afterAll(() => { |
| 96 | + process.env = env; |
| 97 | + }); |
| 98 | + |
| 99 | + const run = async (id: string, argv: string[] = []) => { |
| 100 | + const config = await Config.load(REPO_ROOT); |
| 101 | + await hook.call({ config } as never, { argv, config, context: {} as never, id }); |
| 102 | + return config; |
| 103 | + }; |
| 104 | + |
| 105 | + const writeCheck = (check: object) => { |
| 106 | + mkdirSync(cacheDir, { recursive: true }); |
| 107 | + writeFileSync(path.join(cacheDir, 'version-check.json'), JSON.stringify({ channel: 'stable', checkedAt: Date.now(), ...check })); |
| 108 | + }; |
| 109 | + |
| 110 | + const readCheck = () => JSON.parse(readFileSync(path.join(cacheDir, 'version-check.json'), 'utf8')); |
| 111 | + |
| 112 | + it('starts the version check in the background once a day, even with autoupdate disabled', async () => { |
| 113 | + await run('plan'); |
| 114 | + await vi.waitFor(() => expect(readFileSync(calls, 'utf8')).toBe('update --check\n')); |
| 115 | + expect(readCheck()).toMatchObject({ channel: 'stable', checkedAt: expect.any(Number) }); |
| 116 | + |
| 117 | + await run('apply'); |
| 118 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 119 | + expect(readFileSync(calls, 'utf8')).toBe('update --check\n'); |
| 120 | + expect(onFinalExit).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('prints the notice on exit when a newer version was found, at most once a day', async () => { |
| 124 | + writeCheck({ latest: '999.0.0' }); |
| 125 | + const config = await run('apply', ['--output', 'plain']); |
| 126 | + expect(onFinalExit).toHaveBeenCalledTimes(1); |
| 127 | + |
| 128 | + const write = vi.spyOn(process.stderr, 'write').mockImplementation(() => true); |
| 129 | + try { |
| 130 | + vi.mocked(onFinalExit).mock.calls[0][0](); |
| 131 | + expect(stripVTControlCharacters(String(write.mock.calls[0][0]))) |
| 132 | + .toBe(`\nUpdate available: ${config.version} → 999.0.0. Run codify update to upgrade.\n`); |
| 133 | + } finally { |
| 134 | + write.mockRestore(); |
| 135 | + } |
| 136 | + |
| 137 | + expect(readCheck()).toMatchObject({ latest: '999.0.0', notifiedAt: expect.any(Number) }); |
| 138 | + vi.mocked(onFinalExit).mockClear(); |
| 139 | + await run('apply'); |
| 140 | + expect(onFinalExit).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('says nothing when up to date or when autoupdate already installed the latest version', async () => { |
| 144 | + writeCheck({ latest: '0.0.1' }); |
| 145 | + await run('plan'); |
| 146 | + expect(onFinalExit).not.toHaveBeenCalled(); |
| 147 | + |
| 148 | + writeCheck({ latest: '999.0.0' }); |
| 149 | + const clientBin = path.join(tmp, 'data', 'client', 'bin', 'codify'); |
| 150 | + mkdirSync(path.dirname(clientBin), { recursive: true }); |
| 151 | + writeFileSync(clientBin, 'exec "$DIR/../999.0.0-abc1234/bin/codify" "$@"\n'); |
| 152 | + await run('plan'); |
| 153 | + expect(onFinalExit).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('does nothing outside a terminal, in CI, for JSON output, for the update command or when disabled', async () => { |
| 157 | + writeCheck({ checkedAt: 0, latest: '999.0.0' }); |
| 158 | + const cases: Array<[() => void, string, string[]]> = [ |
| 159 | + [() => { process.stderr.isTTY = false; }, 'plan', []], |
| 160 | + [() => { process.env.CI = 'true'; }, 'plan', []], |
| 161 | + [() => { process.env.CODIFY_DISABLE_UPDATE_NOTIFIER = 'true'; }, 'plan', []], |
| 162 | + [() => {}, 'plan', ['-o', 'json']], |
| 163 | + [() => {}, 'update', []], |
| 164 | + ]; |
| 165 | + for (const [setUp, id, argv] of cases) { |
| 166 | + process.stderr.isTTY = true; |
| 167 | + delete process.env.CI; |
| 168 | + delete process.env.CODIFY_DISABLE_UPDATE_NOTIFIER; |
| 169 | + setUp(); |
| 170 | + await run(id, argv); |
| 171 | + } |
| 172 | + |
| 173 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 174 | + expect(onFinalExit).not.toHaveBeenCalled(); |
| 175 | + expect(existsSync(calls)).toBe(false); |
| 176 | + }); |
| 177 | +}); |
0 commit comments