Files
sweet/packages/init/tests/utils_test.ts
T
fry69 198816b042 feat(init): template based init script (#3524)
Supersedes earlier attempt ->
https://github.com/denoland/fresh/pull/3517

This is now ready for review.

The major upside and motivation for this template based approach is that
it is now possible to view what gets generated in the repository as
actual valid files including syntax highlighting and formatting.

Previously those files where just strings inside the old `init` script,
which is IMHO a nightmare for maintainability.

The approach for this template system is loosely based on Vite's
`create-vite`, which also includes (redundant) templates inside the
repository, see here ->
https://github.com/vitejs/vite/tree/main/packages/create-vite

Included in the tests are compatibility checks against the current
`@fresh/init@2.0.9`, those can get removed at a later point when the
templates start to diverge. They are only included for demonstration
purposes, that this new implementation generates 100% identical output.

This means that the first PR after this got included, which modifies the
templates so the output differs from the old `init` script, also needs
to include:
```shell
rm packages/init/tests/compatibility_test.ts
```

Since this is a rather large PR, it may be more approachable to view the
branch directly ->
https://github.com/fry69/fresh/tree/fry69/refactor-init-template-rename/packages/init

Generated PR message below:

---

Replaces inline string-based project generation with file-based
templates for improved maintainability and extensibility.

### Architecture

**Template structure:**
- `assets/template/` - Complete project templates (vite, vite-tailwind,
builder, builder-tailwind)
- `assets/variants/` - Additive overlays (docker, vscode,
vscode-tailwind)
- `assets/base/` - Common files synced across template variants

Templates use `__VARIABLE__` syntax for substitution (project name,
dependency versions). Files with `__` prefix are renamed to `.` prefix
during generation (e.g., `__gitignore` → .gitignore).

**Template selection logic:**
```
Base: vite|vite-tailwind|builder|builder-tailwind
Variants: +docker, +vscode, +vscode-tailwind
```

### Maintenance workflow

Edit shared files in `assets/base/vite/` or `assets/base/builder/`, then
run `deno task sync` to propagate changes to template variants.
Template-specific files (deno.json, vite.config.ts, styles.css) are
never overwritten.

### Version resolution

Matches existing behavior: only Fresh core version is fetched from JSR,
all other dependencies use fixed versions updated by release scripts.

### Testing

55 tests covering:
- Unit tests for core functionality
- Integration tests (project generation, builds, dev servers)
- CLI argument parsing
- Compatibility tests verifying output matches v2.0.9 (pre-refactor
baseline)
- Template sync workflow

### Breaking changes

None. CLI interface and programmatic API remain identical. Package
publishes to same `@fresh/init` scope at JSR.

### Documentation

- README.md - User-facing usage documentation
- DESIGN.md - Maintainer documentation (architecture, template
maintenance, development setup)
2025-10-06 09:12:07 +02:00

83 lines
2.8 KiB
TypeScript

import { expect } from "@std/expect";
import {
getLatestVersion,
isDirectoryEmpty,
processFilename,
substituteVariables,
} from "../src/utils.ts";
Deno.test("processFilename - converts double underscore prefix to dot", () => {
expect(processFilename("__gitignore")).toBe(".gitignore");
expect(processFilename("__vscode")).toBe(".vscode");
expect(processFilename("_app.tsx")).toBe("_app.tsx");
expect(processFilename("regular-file.ts")).toBe("regular-file.ts");
});
Deno.test("substituteVariables - replaces template variables", () => {
const template = "Hello __NAME__, version __VERSION__!";
const variables = { NAME: "Fresh", VERSION: "2.0.0" };
const result = substituteVariables(template, variables);
expect(result).toBe("Hello Fresh, version 2.0.0!");
});
Deno.test("substituteVariables - handles multiple occurrences", () => {
const template = "__X__ + __X__ = __Y__";
const variables = { X: "1", Y: "2" };
const result = substituteVariables(template, variables);
expect(result).toBe("1 + 1 = 2");
});
Deno.test("substituteVariables - handles boolean values", () => {
const template = "Enabled: __ENABLED__";
const variables = { ENABLED: true };
const result = substituteVariables(template, variables);
expect(result).toBe("Enabled: true");
});
Deno.test("isDirectoryEmpty - returns true for non-existent directory", async () => {
const isEmpty = await isDirectoryEmpty("/non/existent/path");
expect(isEmpty).toBe(true);
});
Deno.test("isDirectoryEmpty - returns false for directory with files", async () => {
const tempDir = await Deno.makeTempDir();
await Deno.writeTextFile(`${tempDir}/test.txt`, "test");
const isEmpty = await isDirectoryEmpty(tempDir);
expect(isEmpty).toBe(false);
await Deno.remove(tempDir, { recursive: true });
});
Deno.test("isDirectoryEmpty - returns true for empty directory", async () => {
const tempDir = await Deno.makeTempDir();
const isEmpty = await isDirectoryEmpty(tempDir);
expect(isEmpty).toBe(true);
await Deno.remove(tempDir);
});
Deno.test("isDirectoryEmpty - returns true for directory with only .git", async () => {
const tempDir = await Deno.makeTempDir();
await Deno.mkdir(`${tempDir}/.git`);
const isEmpty = await isDirectoryEmpty(tempDir);
expect(isEmpty).toBe(true);
await Deno.remove(tempDir, { recursive: true });
});
Deno.test("getLatestVersion - returns fallback on error", async () => {
const version = await getLatestVersion("@nonexistent/package", "1.0.0");
expect(version).toBe("1.0.0");
});
Deno.test("getLatestVersion - fetches real version", async () => {
// This test requires network access
const version = await getLatestVersion("@std/assert", "0.0.1");
expect(version).toBeTruthy();
// Should return something other than fallback
// (unless there's a network error, in which case it returns fallback)
});