|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// A scaffold into the current directory (no name, or an empty one) used to |
| 4 | +// copy the template straight over whatever was there: an existing project's |
| 5 | +// README.md and package.json were replaced and the run exited 0, while a named |
| 6 | +// non-empty directory was refused. The current directory now gets the same |
| 7 | +// protection, scoped to collisions — a template file that would replace an |
| 8 | +// existing one refuses the run before anything is written; files the template |
| 9 | +// does not write may stay. |
| 10 | +// |
| 11 | +// Runs the real CLI through `tsx` (index.ts calls `program.parse()` on import, |
| 12 | +// so it cannot be imported), the no-build pattern scaffold-description.test.ts |
| 13 | +// uses. |
| 14 | + |
| 15 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 16 | +import { spawnSync } from 'node:child_process'; |
| 17 | +import fs from 'node:fs'; |
| 18 | +import os from 'node:os'; |
| 19 | +import path from 'node:path'; |
| 20 | +import { fileURLToPath } from 'node:url'; |
| 21 | +import { findTemplateCollisions } from './template-copy.js'; |
| 22 | + |
| 23 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 24 | +const PKG_ROOT = path.resolve(HERE, '..'); |
| 25 | +const REPO_ROOT = path.resolve(PKG_ROOT, '..', '..'); |
| 26 | +const TSX = path.join(REPO_ROOT, 'node_modules', '.bin', 'tsx'); |
| 27 | +const INDEX_TS = path.join(PKG_ROOT, 'src', 'index.ts'); |
| 28 | +const BLANK_TEMPLATE = path.join(PKG_ROOT, 'src', 'templates', 'blank'); |
| 29 | + |
| 30 | +const USER_README = '# My notes\n\nWritten by hand, not by the scaffolder.\n'; |
| 31 | +const USER_PACKAGE_JSON = '{ "name": "my-existing-thing", "version": "3.1.4" }\n'; |
| 32 | +const USER_NOTES = 'keep me\n'; |
| 33 | + |
| 34 | +let tmp: string; |
| 35 | +let projectDir: string; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'create-objectstack-collide-')); |
| 39 | + projectDir = path.join(tmp, 'my-app'); |
| 40 | + fs.mkdirSync(projectDir); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(() => fs.rmSync(tmp, { recursive: true, force: true })); |
| 44 | + |
| 45 | +function run(cwd: string, nameArgs: string[]) { |
| 46 | + const result = spawnSync( |
| 47 | + TSX, |
| 48 | + [INDEX_TS, ...nameArgs, '--template', 'blank', '--skip-install', '--skip-skills'], |
| 49 | + { cwd, encoding: 'utf8', env: { ...process.env, NO_COLOR: '1', FORCE_COLOR: '0' } }, |
| 50 | + ); |
| 51 | + return { status: result.status, out: `${result.stdout}${result.stderr}` }; |
| 52 | +} |
| 53 | + |
| 54 | +// Every path under `dir` with its bytes, so "nothing was written" is checked |
| 55 | +// against the whole tree, not only the files a test happens to name. |
| 56 | +function snapshot(dir: string): Record<string, string> { |
| 57 | + const out: Record<string, string> = {}; |
| 58 | + const walk = (abs: string, rel: string) => { |
| 59 | + for (const entry of fs.readdirSync(abs, { withFileTypes: true })) { |
| 60 | + const childAbs = path.join(abs, entry.name); |
| 61 | + const childRel = rel ? `${rel}/${entry.name}` : entry.name; |
| 62 | + if (entry.isDirectory()) { |
| 63 | + out[`${childRel}/`] = ''; |
| 64 | + walk(childAbs, childRel); |
| 65 | + } else { |
| 66 | + out[childRel] = fs.readFileSync(childAbs, 'utf8'); |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | + walk(dir, ''); |
| 71 | + return out; |
| 72 | +} |
| 73 | + |
| 74 | +describe('scaffold into the current directory', () => { |
| 75 | + for (const [label, nameArgs] of [ |
| 76 | + ['no name', []], |
| 77 | + ['an empty name', ['']], |
| 78 | + ] as const) { |
| 79 | + it(`refuses with ${label} when template files would overwrite existing ones, writing nothing`, () => { |
| 80 | + fs.writeFileSync(path.join(projectDir, 'README.md'), USER_README); |
| 81 | + fs.writeFileSync(path.join(projectDir, 'package.json'), USER_PACKAGE_JSON); |
| 82 | + fs.writeFileSync(path.join(projectDir, 'notes.txt'), USER_NOTES); |
| 83 | + const before = snapshot(projectDir); |
| 84 | + |
| 85 | + const { status, out } = run(projectDir, [...nameArgs]); |
| 86 | + |
| 87 | + expect(status).toBe(1); |
| 88 | + expect(snapshot(projectDir)).toEqual(before); |
| 89 | + expect(out).toContain('the template would overwrite 2 existing files'); |
| 90 | + expect(out).toMatch(/^\s+README\.md$/m); |
| 91 | + expect(out).toMatch(/^\s+package\.json$/m); |
| 92 | + expect(out).not.toMatch(/^\s+notes\.txt$/m); |
| 93 | + expect(out).not.toContain('Environment created!'); |
| 94 | + }, 30_000); |
| 95 | + } |
| 96 | + |
| 97 | + it('scaffolds when the directory only holds files the template does not write, leaving them untouched', () => { |
| 98 | + fs.mkdirSync(path.join(projectDir, '.git')); |
| 99 | + fs.writeFileSync(path.join(projectDir, '.git', 'HEAD'), 'ref: refs/heads/main\n'); |
| 100 | + fs.writeFileSync(path.join(projectDir, 'notes.txt'), USER_NOTES); |
| 101 | + |
| 102 | + const { status, out } = run(projectDir, []); |
| 103 | + |
| 104 | + expect(status).toBe(0); |
| 105 | + expect(out).toContain('Environment created!'); |
| 106 | + expect(out).toContain('the directory already had contents'); |
| 107 | + expect(JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), 'utf8')).name).toBe( |
| 108 | + 'my-app', |
| 109 | + ); |
| 110 | + expect(fs.readFileSync(path.join(projectDir, 'notes.txt'), 'utf8')).toBe(USER_NOTES); |
| 111 | + expect(fs.readFileSync(path.join(projectDir, '.git', 'HEAD'), 'utf8')).toBe( |
| 112 | + 'ref: refs/heads/main\n', |
| 113 | + ); |
| 114 | + }, 30_000); |
| 115 | + |
| 116 | + it('scaffolds an empty current directory as before', () => { |
| 117 | + const { status, out } = run(projectDir, []); |
| 118 | + |
| 119 | + expect(status).toBe(0); |
| 120 | + expect(out).toContain('Created files:'); |
| 121 | + expect(fs.existsSync(path.join(projectDir, 'objectstack.config.ts'))).toBe(true); |
| 122 | + }, 30_000); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('scaffold into a named directory', () => { |
| 126 | + it('still refuses a named non-empty directory with the old message', () => { |
| 127 | + fs.writeFileSync(path.join(projectDir, 'notes.txt'), USER_NOTES); |
| 128 | + const before = snapshot(projectDir); |
| 129 | + |
| 130 | + const { status, out } = run(tmp, ['my-app']); |
| 131 | + |
| 132 | + expect(status).toBe(1); |
| 133 | + expect(out).toContain(`Directory already exists and is not empty: ${projectDir}`); |
| 134 | + expect(snapshot(projectDir)).toEqual(before); |
| 135 | + }, 30_000); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('findTemplateCollisions', () => { |
| 139 | + it('names aliased dotfiles by the name they land under', () => { |
| 140 | + fs.writeFileSync(path.join(projectDir, '.gitignore'), 'node_modules\n'); |
| 141 | + expect(findTemplateCollisions(BLANK_TEMPLATE, projectDir)).toEqual(['.gitignore']); |
| 142 | + }); |
| 143 | + |
| 144 | + it('reports a file standing where the template needs a directory', () => { |
| 145 | + fs.writeFileSync(path.join(projectDir, 'src'), 'not a directory\n'); |
| 146 | + expect(findTemplateCollisions(BLANK_TEMPLATE, projectDir)).toEqual(['src']); |
| 147 | + }); |
| 148 | + |
| 149 | + it('looks inside a directory the template shares with the user', () => { |
| 150 | + fs.mkdirSync(path.join(projectDir, 'src')); |
| 151 | + fs.writeFileSync(path.join(projectDir, 'src', 'mine.ts'), 'export {};\n'); |
| 152 | + expect(findTemplateCollisions(BLANK_TEMPLATE, projectDir)).toEqual([]); |
| 153 | + }); |
| 154 | +}); |
0 commit comments