tryscript 0.0.1 → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Joshua Levy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,223 +1,132 @@
1
1
  # tryscript
2
2
 
3
- Golden testing for CLI applications - a TypeScript port of [trycmd](https://github.com/assert-rs/trycmd).
4
-
5
- ## Requirements
6
-
7
- - **Node.js 20+** (tested with v20, v22, v24)
8
-
9
- ## Installation
3
+ [![CI](https://github.com/jlevy/tryscript/actions/workflows/ci.yml/badge.svg)](https://github.com/jlevy/tryscript/actions/workflows/ci.yml)
4
+ [![Coverage](https://raw.githubusercontent.com/jlevy/tryscript/main/badges/packages/tryscript/coverage-total.svg)](https://raw.githubusercontent.com/jlevy/tryscript/main/badges/packages/tryscript/coverage-total.svg)
5
+ [![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)
10
7
 
11
- ```bash
12
- npm install tryscript
13
- # or
14
- pnpm add tryscript
15
- ```
16
-
17
- ## Quick Start
18
-
19
- Create a test file with the `.tryscript.md` extension:
20
-
21
- ```markdown
22
- # Test: Help command
23
-
24
- \`\`\`console
25
- $ my-cli --help
26
- Usage: my-cli [options]
27
-
28
- Options:
29
- --version Show version
30
- --help Show help
31
- ? 0
32
- \`\`\`
33
- ```
34
-
35
- Run the tests:
36
-
37
- ```bash
38
- npx tryscript tests/
39
- ```
40
-
41
- ## Test File Format
42
-
43
- Test files are Markdown documents with console code blocks. Each code block represents a test case:
8
+ Golden testing for CLI applications - a TypeScript port of [trycmd](https://github.com/assert-rs/trycmd).
44
9
 
45
- ```markdown
46
- \`\`\`console
47
- $ <command>
48
- <expected output>
49
- ? <exit code>
50
- \`\`\`
51
- ```
10
+ ## What It Does
52
11
 
53
- ### Example
12
+ Write CLI tests as Markdown. tryscript runs commands, captures output, and compares against expected results. Tests become documentation; documentation becomes tests.
54
13
 
55
- ```markdown
56
- # Test: Echo command
14
+ ````markdown
15
+ # Test: Basic echo
57
16
 
58
- \`\`\`console
17
+ ```console
59
18
  $ echo "hello world"
60
19
  hello world
61
20
  ? 0
62
- \`\`\`
21
+ ```
63
22
 
64
- # Test: Exit with error
23
+ # Test: Grep with pattern matching
65
24
 
66
- \`\`\`console
67
- $ exit 1
68
- ? 1
69
- \`\`\`
25
+ ```console
26
+ $ ls -la | grep ".md"
27
+ [..]README.md
28
+ ...
29
+ ? 0
70
30
  ```
31
+ ````
71
32
 
72
- ## Elision Patterns
33
+ The `[..]` matches any text on that line. The `...` matches zero or more lines. These "elision patterns" let tests handle dynamic output gracefully.
73
34
 
74
- Use elision patterns to match dynamic or platform-specific output:
35
+ ## Quick Start
75
36
 
76
- | Pattern | Description | Example |
77
- | -------- | ---------------------------------------- | -------------------------- |
78
- | `[..]` | Match any characters on the current line | `Built in [..]ms` |
79
- | `...` | Match zero or more complete lines | `...\nDone` |
80
- | `[EXE]` | Match `.exe` on Windows, empty otherwise | `my-cli[EXE] --help` |
81
- | `[ROOT]` | Match the test's root directory | `[ROOT]/output.txt` |
82
- | `[CWD]` | Match the current working directory | `[CWD]/file.txt` |
37
+ ```bash
38
+ # Install
39
+ pnpm add -D tryscript
83
40
 
84
- ### Example with Elision
41
+ # Run tests
42
+ npx tryscript run tests/
85
43
 
86
- ```markdown
87
- \`\`\`console
88
- $ time-command
89
- Elapsed: [..]ms
90
- ? 0
91
- \`\`\`
44
+ # Update expected output when behavior changes
45
+ npx tryscript run --update tests/
92
46
  ```
93
47
 
94
- ## Configuration
48
+ ## Features
95
49
 
96
- ### YAML Frontmatter
50
+ - **Markdown format** - Tests are readable documentation
51
+ - **Elision patterns** - Handle variable output: `[..]`, `...`, `[CWD]`, `[ROOT]`, `[EXE]`
52
+ - **Custom patterns** - Define regex patterns for timestamps, versions, UUIDs
53
+ - **Update mode** - Regenerate expected output with `--update`
54
+ - **Sandbox mode** - Isolate tests in temp directories
55
+ - **Code coverage** - Track coverage from subprocess execution with `--coverage`
97
56
 
98
- Add configuration at the top of your test file:
57
+ ## Example Test File
99
58
 
100
- ```markdown
59
+ ````markdown
101
60
  ---
102
- bin: ./my-cli
103
61
  env:
104
62
  NO_COLOR: "1"
105
- timeout: 5000
63
+ sandbox: true
106
64
  ---
107
65
 
108
- # Test: Custom binary
109
-
110
- \`\`\`console
111
- $ my-cli --version
112
- 1.0.0
113
- ? 0
114
- \`\`\`
115
- ```
116
-
117
- ### Config File
118
-
119
- Create `tryscript.config.ts` in your project root:
66
+ # Test: CLI help
120
67
 
121
- ```typescript
122
- import { defineConfig } from 'tryscript';
68
+ ```console
69
+ $ my-cli --help
70
+ Usage: my-cli [options] <command>
123
71
 
124
- export default defineConfig({
125
- bin: './dist/cli.js',
126
- env: {
127
- NO_COLOR: '1',
128
- },
129
- timeout: 30000,
130
- patterns: {
131
- VERSION: '\\d+\\.\\d+\\.\\d+',
132
- UUID: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
133
- },
134
- });
72
+ Options:
73
+ --version Show version
74
+ --help Show this help
75
+ ...
76
+ ? 0
135
77
  ```
136
78
 
137
- ## CLI Options
79
+ # Test: Version output
138
80
 
81
+ ```console
82
+ $ my-cli --version
83
+ my-cli v[..]
84
+ ? 0
139
85
  ```
140
- tryscript [options] [files...]
141
86
 
142
- Arguments:
143
- files Test files to run (default: **/*.tryscript.md)
87
+ # Test: Error handling
144
88
 
145
- Options:
146
- --version Show version number
147
- --update Update golden files with actual output
148
- --diff Show diff on failure (default: true)
149
- --no-diff Hide diff on failure
150
- --fail-fast Stop on first failure
151
- --filter <pattern> Filter tests by name pattern
152
- --verbose Show detailed output
153
- --quiet Suppress non-essential output
154
- --help Show help
89
+ ```console
90
+ $ my-cli unknown-command 2>&1
91
+ Error: unknown command 'unknown-command'
92
+ ? 1
155
93
  ```
94
+ ````
156
95
 
157
- ## Update Mode
158
-
159
- When your CLI output changes, update all test files at once:
96
+ ## CLI Reference
160
97
 
161
98
  ```bash
162
- npx tryscript --update
99
+ tryscript run [files...] # Run golden tests
100
+ tryscript docs # Show syntax quick reference
101
+ tryscript readme # Show this documentation
102
+ tryscript --help # Show all options
163
103
  ```
164
104
 
165
- This rewrites test files with the actual output from running the commands.
105
+ For complete syntax reference, run `tryscript docs` or see the [reference documentation](https://github.com/jlevy/tryscript/blob/main/docs/tryscript-reference.md).
166
106
 
167
- ## Programmatic API
107
+ ### Common Options
168
108
 
169
- ```typescript
170
- import { parseTestFile, runBlock, createExecutionContext, matchOutput } from 'tryscript';
109
+ | Option | Description |
110
+ | --- | --- |
111
+ | `--update` | Update test files with actual output |
112
+ | `--fail-fast` | Stop on first failure |
113
+ | `--filter <regex>` | Filter tests by name |
114
+ | `--verbose` | Show detailed output |
115
+ | `--coverage` | Collect code coverage (requires c8) |
171
116
 
172
- const content = await fs.readFile('test.tryscript.md', 'utf-8');
173
- const testFile = parseTestFile(content, 'test.tryscript.md');
174
-
175
- const ctx = await createExecutionContext({}, 'test.tryscript.md');
176
- for (const block of testFile.blocks) {
177
- const result = await runBlock(block, ctx);
178
- const matches = matchOutput(
179
- result.actualOutput,
180
- block.expectedOutput,
181
- { root: ctx.tempDir, cwd: ctx.tempDir },
182
- );
183
- console.log(`${block.name}: ${matches ? 'PASS' : 'FAIL'}`);
184
- }
185
- ```
186
-
187
- ## Measuring Coverage
188
-
189
- When testing CLI tools as subprocesses, standard coverage tools don't track execution. Use [c8](https://github.com/bcoe/c8) which leverages Node's V8 coverage collection:
117
+ ## Development
190
118
 
191
119
  ```bash
192
- npm install -D c8
193
- ```
194
-
195
- Add scripts to your `package.json`:
196
-
197
- ```json
198
- {
199
- "scripts": {
200
- "test:golden": "tryscript 'tests/**/*.tryscript.md'",
201
- "test:golden:coverage": "c8 --src src --all --include 'dist/**' tryscript 'tests/**/*.tryscript.md'"
202
- }
203
- }
120
+ # Clone and install
121
+ git clone https://github.com/jlevy/tryscript.git
122
+ cd tryscript
123
+ pnpm install
124
+
125
+ # Build and test
126
+ pnpm build
127
+ pnpm test
204
128
  ```
205
129
 
206
- Key c8 flags:
207
- - `--src src` — Map coverage back to source files
208
- - `--all` — Include files with 0% coverage
209
- - `--include 'dist/**'` — Track your built CLI output
210
-
211
- This captures coverage from actual CLI usage—the most realistic testing possible.
212
-
213
- ## Comparison with trycmd
214
-
215
- tryscript is a TypeScript port of the Rust [trycmd](https://github.com/assert-rs/trycmd) crate. Key differences:
216
-
217
- - **Language**: TypeScript/Node.js instead of Rust
218
- - **Format**: Uses console code blocks (trycmd uses `.toml` or `.trycmd` files)
219
- - **Integration**: Works with Node.js test frameworks (Vitest, Jest)
220
-
221
130
  ## License
222
131
 
223
132
  MIT