tryscript 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,25 +1,43 @@
1
1
  # tryscript
2
2
 
3
- [![CI](https://github.com/jlevy/tryscript/actions/workflows/ci.yml/badge.svg)](https://github.com/jlevy/tryscript/actions/runs/21106289057)
4
- [![Coverage](https://raw.githubusercontent.com/jlevy/tryscript/main/badges/packages/tryscript/coverage-total.svg)](https://github.com/jlevy/tryscript/actions/runs/21106289057)
3
+ [![Follow @ojoshe on X](https://img.shields.io/badge/follow_%40ojoshe-black?logo=x&logoColor=white)](https://x.com/ojoshe)
4
+ [![CI](https://github.com/jlevy/tryscript/actions/workflows/ci.yml/badge.svg)](https://github.com/jlevy/tryscript/actions/runs/22267140455)
5
+ [![Coverage](https://raw.githubusercontent.com/jlevy/tryscript/main/badges/packages/tryscript/coverage-total.svg)](https://github.com/jlevy/tryscript/actions/runs/22267140455)
5
6
  [![npm version](https://img.shields.io/npm/v/tryscript)](https://www.npmjs.com/package/tryscript)
6
- [![X Follow](https://img.shields.io/twitter/follow/ojoshe)](https://x.com/ojoshe)
7
7
 
8
- Golden testing for CLI applications - a TypeScript port of [trycmd](https://github.com/assert-rs/trycmd).
8
+ **Powerful, agent-friendly testing of CLI applications via golden tests**
9
9
 
10
10
  > [!NOTE]
11
11
  > 100% of the code and specs in this repository were written by Claude Code.
12
12
  > The design and management and prompting was by me ([jlevy](https://github.com/jlevy)) supported by the workflows, agent rules,
13
- > and other research docs in [Speculate](https://github.com/jlevy/speculate).
13
+ > and other research docs in [tbd](https://github.com/jlevy/tbd).
14
14
  >
15
- > You can see what you think, but I find the code quality higher than most agent-written code I've
15
+ > I find the code quality higher than most agent-written code I've
16
16
  > seen because of the spec-driven process.
17
17
  > You can review the architecture doc and all of the specs all of the specs in [docs/project](docs/project).
18
18
  > The general research, guideline, and rules docs I use are in [docs/general](docs/general).
19
19
 
20
+ ## Why?
21
+
22
+ Write CLI tests as Markdown. tryscript runs commands, captures output, and compares against expected results:
23
+
24
+ - Tests are clear and maintainable for agents and humans: tests become documentation; documentation becomes tests
25
+ - Inner state and working can be exposed for greater test coverage at no extra cost
26
+ - Things are quick to implement or test using arbitrary shell commands
27
+
28
+ This began as a TypeScript port of [trycmd](https://github.com/assert-rs/trycmd) but I (well, Claude and friends)
29
+ have since enhanced it to be more agent-friendly and self-documenting as a CLI.
30
+
31
+ For a bit more philosophy on why golden tests are so useful, you (or your friendly agent)
32
+ should read [tbd](https://github.com/jlevy/tbd)’s guidelines doc:
33
+
34
+ ```bash
35
+ npx --yes get-tbd@latest guidelines golden-testing-guidelines
36
+ ```
37
+
20
38
  ## What It Does
21
39
 
22
- Write CLI tests as Markdown. tryscript runs commands, captures output, and compares against expected results. Tests become documentation; documentation becomes tests.
40
+ An example test:
23
41
 
24
42
  ````markdown
25
43
  ---
@@ -68,6 +86,16 @@ $ my-cli process data.json > output.txt && grep "success" output.txt
68
86
 
69
87
  The `[..]` matches any text on that line. The `...` matches zero or more lines. These "elision patterns" let tests handle dynamic output gracefully. Any shell command works - pipes, redirects, environment variables, etc.
70
88
 
89
+ ### Wildcard Categories
90
+
91
+ Tryscript supports three categories of wildcards, in order of preference:
92
+
93
+ 1. **Named patterns** (`[HASH]`, `[VERSION]`, `[CWD]`, etc.) -- Typed dynamic values with specific meaning. Preferred when the output has a known structure.
94
+ 2. **Unknown wildcards** (`[??]`, `???`) -- Temporary placeholders for output you haven't filled in yet. Intended to be expanded with `--expand` before finalizing tests.
95
+ 3. **Generic wildcards** (`[..]`, `...`) -- Intentional omission of unpredictable or irrelevant output. Use when the exact value doesn't matter for the test.
96
+
97
+ Use `--expand` to automatically fill in unknown wildcards with actual output after a successful run.
98
+
71
99
  ## Quick Start
72
100
 
73
101
  ```bash
@@ -90,7 +118,8 @@ npx tryscript run --update tests/
90
118
  ## Features
91
119
 
92
120
  - **Markdown format** - Tests are readable documentation
93
- - **Elision patterns** - Handle variable output: `[..]`, `...`, `[CWD]`, `[ROOT]`, `[EXE]`
121
+ - **Elision patterns** - Handle variable output: `[..]`, `...`, `[??]`, `???`, `[CWD]`, `[ROOT]`, `[EXE]`
122
+ - **Wildcard expansion** - Fill in `[??]`/`???` placeholders with actual output via `--expand`
94
123
  - **Custom patterns** - Define regex patterns for timestamps, versions, UUIDs
95
124
  - **Update mode** - Regenerate expected output with `--update`
96
125
  - **Sandbox mode** - Isolate tests in temp directories
@@ -113,6 +142,10 @@ For complete syntax reference, run `tryscript docs` or see the [reference docume
113
142
  | Option | Description |
114
143
  | --- | --- |
115
144
  | `--update` | Update test files with actual output |
145
+ | `--expand` | Expand unknown wildcards (`???`/`[??]`) with actual output |
146
+ | `--expand-generic` | Expand unknown + generic wildcards |
147
+ | `--expand-all` | Expand all wildcards (including named patterns) |
148
+ | `--capture-log <path>` | Write wildcard capture log to YAML file |
116
149
  | `--fail-fast` | Stop on first failure |
117
150
  | `--filter <regex>` | Filter tests by name |
118
151
  | `--verbose` | Show detailed output |
package/dist/bin.cjs CHANGED
@@ -1,20 +1,20 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
 
4
- const require_src = require('./src-1oEnK7GG.cjs');
4
+ const require_src = require('./src-BIZMxxIt.cjs');
5
5
  let node_url = require("node:url");
6
6
  let node_fs = require("node:fs");
7
7
  let node_path = require("node:path");
8
8
  let node_child_process = require("node:child_process");
9
9
  let node_fs_promises = require("node:fs/promises");
10
10
  let node_os = require("node:os");
11
+ let atomically = require("atomically");
11
12
  let commander = require("commander");
12
13
  let fast_glob = require("fast-glob");
13
14
  fast_glob = require_src.__toESM(fast_glob);
14
15
  let picocolors = require("picocolors");
15
16
  picocolors = require_src.__toESM(picocolors);
16
17
  let diff = require("diff");
17
- let atomically = require("atomically");
18
18
 
19
19
  //#region src/cli/lib/shared.ts
20
20
  /**
@@ -136,9 +136,10 @@ function reportSummary(summary, _options) {
136
136
  async function updateTestFile(file, results) {
137
137
  let content = file.rawContent;
138
138
  const changes = [];
139
- const blocksWithResults = file.blocks.map((block, i) => ({
139
+ const resultByBlock = new Map(results.map((result) => [result.block, result]));
140
+ const blocksWithResults = [...file.blocks].map((block) => ({
140
141
  block,
141
- result: results[i]
142
+ result: resultByBlock.get(block)
142
143
  })).reverse();
143
144
  for (const { block, result } of blocksWithResults) {
144
145
  if (!result) continue;
@@ -161,12 +162,14 @@ async function updateTestFile(file, results) {
161
162
  * Build an updated console block with new expected output.
162
163
  */
163
164
  function buildUpdatedBlock(block, result) {
164
- const lines = ["```console", ...block.command.split("\n").map((line, i) => {
165
+ const fence = "`".repeat(/^(`+)/.exec(block.rawContent)?.[1]?.length ?? 3);
166
+ const commandLines = block.command.split("\n").map((line, i) => {
165
167
  return i === 0 ? `$ ${line}` : `> ${line}`;
166
- })];
168
+ });
169
+ const lines = [`${fence}console`, ...commandLines];
167
170
  const trimmedOutput = result.actualOutput.trimEnd();
168
171
  if (trimmedOutput) lines.push(trimmedOutput);
169
- lines.push(`? ${result.actualExitCode}`, "```");
172
+ lines.push(`? ${result.actualExitCode}`, fence);
170
173
  return lines.join("\n");
171
174
  }
172
175
 
@@ -540,10 +543,32 @@ function mergeExternalCoverage(reportsDir, externalLcovPath) {
540
543
  * Register the run command.
541
544
  */
542
545
  function registerRunCommand(program) {
543
- program.command("run").description("Run golden tests").argument("[files...]", "Test files to run (default: **/*.tryscript.md)").option("--update", "Update golden files with actual output").option("--diff", "Show diff on failure (default: true)").option("--no-diff", "Hide diff on failure").option("--fail-fast", "Stop on first failure").option("--filter <pattern>", "Filter tests by name pattern").option("--verbose", "Show detailed output including passing test output").option("--quiet", "Suppress non-essential output (only show failures)").option("--coverage", "Enable code coverage collection (requires c8)").option("--coverage-dir <dir>", "Coverage output directory (default: coverage-tryscript)").option("--coverage-reporter <reporter...>", "Coverage reporters (default: text, html). Can be specified multiple times.").option("--coverage-exclude <pattern...>", "Patterns to exclude from coverage (c8 --exclude). Can be specified multiple times.").option("--coverage-exclude-node-modules", "Exclude node_modules from coverage (c8 --exclude-node-modules, default: true)").option("--no-coverage-exclude-node-modules", "Include node_modules in coverage (c8 --no-exclude-node-modules)").option("--coverage-exclude-after-remap", "Apply exclude logic after sourcemap remapping (c8 --exclude-after-remap)").option("--coverage-skip-full", "Hide files with 100% coverage (c8 --skip-full)").option("--coverage-allow-external", "Allow files from outside cwd (c8 --allowExternal)").option("--coverage-monocart", "Use monocart for accurate line counts, better for merging with vitest (c8 --experimental-monocart)").option("--merge-lcov <path>", "Merge coverage from an existing LCOV file (e.g., from vitest --coverage)").action(runCommand$1);
546
+ program.command("run").description("Run golden tests").argument("[files...]", "Test files to run (default: **/*.tryscript.md)").option("--update", "Update golden files with actual output").option("--diff", "Show diff on failure (default: true)").option("--no-diff", "Hide diff on failure").option("--fail-fast", "Stop on first failure").option("--filter <pattern>", "Filter tests by name pattern").option("--verbose", "Show detailed output including passing test output").option("--quiet", "Suppress non-essential output (only show failures)").option("--expand", "Expand unknown wildcards (??? and [??]) with actual output").option("--expand-generic", "Expand unknown and generic wildcards with actual output").option("--expand-all", "Expand all wildcards (including named patterns) with actual output").option("--capture-log <path>", "Write wildcard capture log to YAML file").option("--coverage", "Enable code coverage collection (requires c8)").option("--coverage-dir <dir>", "Coverage output directory (default: coverage-tryscript)").option("--coverage-reporter <reporter...>", "Coverage reporters (default: text, html). Can be specified multiple times.").option("--coverage-exclude <pattern...>", "Patterns to exclude from coverage (c8 --exclude). Can be specified multiple times.").option("--coverage-exclude-node-modules", "Exclude node_modules from coverage (c8 --exclude-node-modules, default: true)").option("--no-coverage-exclude-node-modules", "Include node_modules in coverage (c8 --no-exclude-node-modules)").option("--coverage-exclude-after-remap", "Apply exclude logic after sourcemap remapping (c8 --exclude-after-remap)").option("--coverage-skip-full", "Hide files with 100% coverage (c8 --skip-full)").option("--coverage-allow-external", "Allow files from outside cwd (c8 --allowExternal)").option("--coverage-monocart", "Use monocart for accurate line counts, better for merging with vitest (c8 --experimental-monocart)").option("--merge-lcov <path>", "Merge coverage from an existing LCOV file (e.g., from vitest --coverage)").action(runCommand$1);
547
+ }
548
+ /**
549
+ * Count unknown wildcard tokens (`???` and `[??]`) in expected output.
550
+ */
551
+ function countUnknownWildcards(expectedOutput) {
552
+ return (expectedOutput.match(/\[\?\?]/g) ?? []).length + (expectedOutput.match(/\?\?\?\n/g) ?? []).length;
544
553
  }
545
554
  async function runCommand$1(files, options) {
546
555
  const startTime = Date.now();
556
+ if ([
557
+ options.expand,
558
+ options.expandGeneric,
559
+ options.expandAll
560
+ ].filter(Boolean).length > 1) {
561
+ logError("--expand, --expand-generic, and --expand-all are mutually exclusive");
562
+ process.exit(1);
563
+ }
564
+ let expandLevel;
565
+ if (options.expand) expandLevel = "unknown";
566
+ else if (options.expandGeneric) expandLevel = "generic";
567
+ else if (options.expandAll) expandLevel = "all";
568
+ if (expandLevel && options.update) {
569
+ logError("--expand* flags and --update are mutually exclusive");
570
+ process.exit(1);
571
+ }
547
572
  const opts = {
548
573
  diff: options.diff !== false,
549
574
  verbose: options.verbose ?? false,
@@ -593,6 +618,8 @@ async function runCommand$1(files, options) {
593
618
  coverageEnv = getCoverageEnv(coverageCtx);
594
619
  }
595
620
  const fileResults = [];
621
+ const fileContexts = /* @__PURE__ */ new Map();
622
+ const filePatterns = /* @__PURE__ */ new Map();
596
623
  let shouldStop = false;
597
624
  for (const filePath of testFiles) {
598
625
  if (shouldStop) break;
@@ -608,6 +635,7 @@ async function runCommand$1(files, options) {
608
635
  if (blocksToRun.length === 0) continue;
609
636
  const ctx = await require_src.createExecutionContext(config, filePath, coverageEnv);
610
637
  const results = [];
638
+ let fileContext;
611
639
  try {
612
640
  for (const block of blocksToRun) {
613
641
  const result = await require_src.runBlock(block, ctx);
@@ -634,6 +662,12 @@ async function runCommand$1(files, options) {
634
662
  }
635
663
  }
636
664
  await require_src.runAfterHook(ctx);
665
+ fileContext = {
666
+ root: ctx.testDir,
667
+ cwd: ctx.cwd
668
+ };
669
+ fileContexts.set(filePath, fileContext);
670
+ filePatterns.set(filePath, config.patterns ?? {});
637
671
  } finally {
638
672
  await require_src.cleanupExecutionContext(ctx);
639
673
  }
@@ -649,7 +683,14 @@ async function runCommand$1(files, options) {
649
683
  const { updated, changes } = await updateTestFile(testFile, results);
650
684
  if (updated) console.error(colors.warn(` ${status.update} Updated: ${changes.join(", ")}`));
651
685
  }
686
+ if (expandLevel && fileContext) {
687
+ const { expanded, expandedCount, changes } = await require_src.expandTestFile(testFile, results, expandLevel, fileContext, config.patterns ?? {});
688
+ if (expanded) console.error(colors.warn(` ${status.update} Expanded ${expandedCount} wildcard(s): ${changes.join(", ")}`));
689
+ }
652
690
  }
691
+ let totalUnknownWildcards = 0;
692
+ for (const fr of fileResults) for (const block of fr.file.blocks) totalUnknownWildcards += countUnknownWildcards(block.expectedOutput);
693
+ if (totalUnknownWildcards > 0) logWarn(`${totalUnknownWildcards} unknown wildcard(s) found (??? or [??]). These are temporary and should be expanded. Use --expand to fill them in.`);
653
694
  const summary = {
654
695
  files: fileResults,
655
696
  totalPassed: fileResults.reduce((sum, f) => sum + f.results.filter((r) => r.passed).length, 0),
@@ -658,6 +699,15 @@ async function runCommand$1(files, options) {
658
699
  duration: Date.now() - startTime
659
700
  };
660
701
  reportSummary(summary, opts);
702
+ if (options.captureLog) try {
703
+ await require_src.writeCaptureLog(options.captureLog, fileResults, (file) => fileContexts.get(file.path) ?? {
704
+ root: process.cwd(),
705
+ cwd: process.cwd()
706
+ }, (file) => filePatterns.get(file.path) ?? {});
707
+ console.error(colors.info(`Capture log written to ${options.captureLog}`));
708
+ } catch (error) {
709
+ logError(`Failed to write capture log: ${error instanceof Error ? error.message : String(error)}`);
710
+ }
661
711
  if (coverageCtx) {
662
712
  console.error("\nGenerating coverage report...");
663
713
  try {