|
| 1 | +# Quarto Test Patterns |
| 2 | + |
| 3 | +This document describes the standard patterns for writing smoke tests in the Quarto CLI test suite. |
| 4 | + |
| 5 | +## Test Structure Overview |
| 6 | + |
| 7 | +Quarto uses Deno for testing with custom verification helpers located in: |
| 8 | +- `tests/test.ts` - Core test runner (`testQuartoCmd`) |
| 9 | +- `tests/verify.ts` - Verification helpers (`fileExists`, `pathDoNotExists`, etc.) |
| 10 | +- `tests/utils.ts` - Utility functions (`docs()`, `outputForInput()`, etc.) |
| 11 | + |
| 12 | +## Common Test Patterns |
| 13 | + |
| 14 | +### Project Rendering Tests |
| 15 | + |
| 16 | +For testing project rendering (especially website projects): |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { docs } from "../../utils.ts"; |
| 20 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 21 | +import { existsSync } from "../../../src/deno_ral/fs.ts"; |
| 22 | +import { testQuartoCmd } from "../../test.ts"; |
| 23 | +import { fileExists, pathDoNotExists, noErrors } from "../../verify.ts"; |
| 24 | + |
| 25 | +const projectDir = docs("project/my-test"); // Relative path via docs() |
| 26 | +const outputDir = join(projectDir, "_site"); // Append output dir for websites |
| 27 | + |
| 28 | +testQuartoCmd( |
| 29 | + "render", |
| 30 | + [projectDir], |
| 31 | + [ |
| 32 | + noErrors, // Check for no errors |
| 33 | + fileExists(join(outputDir, "index.html")), // Verify expected file exists |
| 34 | + pathDoNotExists(join(outputDir, "ignored.html")), // Verify file doesn't exist |
| 35 | + ], |
| 36 | + { |
| 37 | + teardown: async () => { |
| 38 | + if (existsSync(outputDir)) { |
| 39 | + await Deno.remove(outputDir, { recursive: true }); |
| 40 | + } |
| 41 | + }, |
| 42 | + }, |
| 43 | +); |
| 44 | +``` |
| 45 | + |
| 46 | +**Key points:** |
| 47 | +- Use `docs()` helper to create relative paths from `tests/docs/` |
| 48 | +- For website projects, output goes to `_site` subdirectory |
| 49 | +- Use absolute paths with `join()` for file verification |
| 50 | +- Clean up output directories in teardown |
| 51 | + |
| 52 | +### Extension Template Tests |
| 53 | + |
| 54 | +For testing `quarto use template`: |
| 55 | + |
| 56 | +```typescript |
| 57 | +import { testQuartoCmd } from "../../test.ts"; |
| 58 | +import { fileExists, noErrorsOrWarnings, pathDoNotExists } from "../../verify.ts"; |
| 59 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 60 | +import { ensureDirSync } from "../../../src/deno_ral/fs.ts"; |
| 61 | + |
| 62 | +const tempDir = Deno.makeTempDirSync(); |
| 63 | + |
| 64 | +// Create mock template source |
| 65 | +const templateSourceDir = join(tempDir, "template-source"); |
| 66 | +ensureDirSync(templateSourceDir); |
| 67 | +Deno.writeTextFileSync(join(templateSourceDir, "template.qmd"), "..."); |
| 68 | +Deno.writeTextFileSync(join(templateSourceDir, "config.yml"), "..."); |
| 69 | + |
| 70 | +const templateFolder = "my-test-template"; |
| 71 | +const workingDir = join(tempDir, templateFolder); |
| 72 | +ensureDirSync(workingDir); |
| 73 | + |
| 74 | +testQuartoCmd( |
| 75 | + "use", |
| 76 | + ["template", templateSourceDir, "--no-prompt"], |
| 77 | + [ |
| 78 | + noErrorsOrWarnings, |
| 79 | + fileExists(`${templateFolder}.qmd`), // Relative - template file renamed to folder name |
| 80 | + pathDoNotExists(join(workingDir, "README.md")), // Absolute - excluded file |
| 81 | + ], |
| 82 | + { |
| 83 | + cwd: () => workingDir, // Set working directory |
| 84 | + teardown: () => { |
| 85 | + try { |
| 86 | + Deno.removeSync(tempDir, { recursive: true }); |
| 87 | + } catch { |
| 88 | + // Ignore cleanup errors |
| 89 | + } |
| 90 | + return Promise.resolve(); |
| 91 | + } |
| 92 | + } |
| 93 | +); |
| 94 | +``` |
| 95 | + |
| 96 | +**Key points:** |
| 97 | +- Use `Deno.makeTempDirSync()` for isolated test environment |
| 98 | +- Create mock template source with test files |
| 99 | +- Template files get renamed to match target directory name |
| 100 | +- Use `cwd()` function to set working directory for command execution |
| 101 | +- Clean up entire temp directory including source files |
| 102 | +- File verification uses relative paths when checking files in `cwd()` |
| 103 | + |
| 104 | +## Verification Helpers |
| 105 | + |
| 106 | +### Core Verifiers |
| 107 | + |
| 108 | +```typescript |
| 109 | +// No errors in output |
| 110 | +noErrors |
| 111 | + |
| 112 | +// No errors or warnings |
| 113 | +noErrorsOrWarnings |
| 114 | + |
| 115 | +// File exists at path |
| 116 | +fileExists(path: string) |
| 117 | + |
| 118 | +// Path does not exist |
| 119 | +pathDoNotExists(path: string) |
| 120 | + |
| 121 | +// Folder exists at path |
| 122 | +folderExists(path: string) |
| 123 | + |
| 124 | +// Directory contains only allowed paths |
| 125 | +directoryEmptyButFor(dir: string, allowedFiles: string[]) |
| 126 | +``` |
| 127 | + |
| 128 | +### Path Helpers |
| 129 | + |
| 130 | +```typescript |
| 131 | +// Create relative path from tests/docs/ |
| 132 | +docs(path: string): string |
| 133 | +// Example: docs("project/site") → "tests/docs/project/site" |
| 134 | + |
| 135 | +// Get expected output for input file |
| 136 | +outputForInput(input: string, to: string, projectOutDir?: string, projectRoot?: string) |
| 137 | + |
| 138 | +// Find project directory |
| 139 | +findProjectDir(input: string, until?: RegExp) |
| 140 | + |
| 141 | +// Find project output directory (_site, _book, etc.) |
| 142 | +findProjectOutputDir(projectdir: string) |
| 143 | +``` |
| 144 | + |
| 145 | +## Output Directory Patterns |
| 146 | + |
| 147 | +Different project types use different output directories: |
| 148 | + |
| 149 | +```typescript |
| 150 | +// Website project |
| 151 | +const outputDir = join(projectDir, "_site"); |
| 152 | + |
| 153 | +// Book project |
| 154 | +const outputDir = join(projectDir, "_book"); |
| 155 | + |
| 156 | +// Manuscript project |
| 157 | +const outputDir = join(projectDir, "_manuscript"); |
| 158 | + |
| 159 | +// Plain project (no type specified) |
| 160 | +// Output goes directly in project directory |
| 161 | +const outputDir = projectDir; |
| 162 | +``` |
| 163 | + |
| 164 | +## Test File Organization |
| 165 | + |
| 166 | +Tests follow this directory structure: |
| 167 | + |
| 168 | +``` |
| 169 | +tests/ |
| 170 | +├── docs/ # Test fixtures |
| 171 | +│ └── project/ |
| 172 | +│ └── my-test/ |
| 173 | +│ ├── _quarto.yml |
| 174 | +│ ├── index.qmd |
| 175 | +│ └── other-files.qmd |
| 176 | +├── smoke/ # Smoke tests |
| 177 | +│ ├── project/ |
| 178 | +│ │ └── project-my-test.test.ts |
| 179 | +│ ├── render/ |
| 180 | +│ ├── use/ |
| 181 | +│ └── ... |
| 182 | +├── test.ts # Test runner |
| 183 | +├── verify.ts # Verification helpers |
| 184 | +└── utils.ts # Utility functions |
| 185 | +``` |
| 186 | + |
| 187 | +## Common Patterns |
| 188 | + |
| 189 | +### Cleanup Pattern |
| 190 | + |
| 191 | +Always clean up generated files in teardown: |
| 192 | + |
| 193 | +```typescript |
| 194 | +teardown: async () => { |
| 195 | + if (existsSync(outputPath)) { |
| 196 | + await Deno.remove(outputPath, { recursive: true }); |
| 197 | + } |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +### Multiple Test Cases |
| 202 | + |
| 203 | +When testing multiple scenarios, declare constants at module level: |
| 204 | + |
| 205 | +```typescript |
| 206 | +const tempDir = Deno.makeTempDirSync(); |
| 207 | + |
| 208 | +// Test case 1 |
| 209 | +const folder1 = "test-case-1"; |
| 210 | +const workingDir1 = join(tempDir, folder1); |
| 211 | +ensureDirSync(workingDir1); |
| 212 | +testQuartoCmd(...); |
| 213 | + |
| 214 | +// Test case 2 |
| 215 | +const folder2 = "test-case-2"; |
| 216 | +const workingDir2 = join(tempDir, folder2); |
| 217 | +ensureDirSync(workingDir2); |
| 218 | +testQuartoCmd(...); |
| 219 | +``` |
| 220 | + |
| 221 | +### Path Construction |
| 222 | + |
| 223 | +- **Absolute paths**: Use `join()` for all path operations |
| 224 | +- **Relative to docs**: Use `docs()` helper |
| 225 | +- **Relative to cwd**: Use plain strings or template literals in template tests |
| 226 | + |
| 227 | +## Examples from Codebase |
| 228 | + |
| 229 | +### Project Ignore Test |
| 230 | +See `tests/smoke/project/project-ignore-dirs.test.ts` for testing directory exclusion patterns. |
| 231 | + |
| 232 | +### Website Rendering Test |
| 233 | +See `tests/smoke/project/project-website.test.ts` for website project rendering patterns. |
| 234 | + |
| 235 | +### Template Usage Test |
| 236 | +See `tests/smoke/use/template.test.ts` for extension template patterns. |
| 237 | + |
| 238 | +## Best Practices |
| 239 | + |
| 240 | +1. **Always clean up**: Use teardown to remove generated files |
| 241 | +2. **Use helpers**: Leverage `docs()`, `fileExists()`, etc. instead of manual checks |
| 242 | +3. **Absolute paths**: Use `join()` for all path construction to handle platform differences |
| 243 | +4. **Test isolation**: Use temp directories for tests that create files |
| 244 | +5. **Clear names**: Use descriptive variable names like `projectDir`, `outputDir`, `templateFolder` |
| 245 | +6. **Comment intent**: Add comments explaining what should/shouldn't happen |
| 246 | +7. **Handle errors**: Wrap cleanup in try-catch to avoid test suite failures from cleanup issues |
| 247 | + |
| 248 | +## Testing File Exclusion |
| 249 | + |
| 250 | +When testing that files are excluded (like AI config files): |
| 251 | + |
| 252 | +```typescript |
| 253 | +// Test that files are NOT rendered |
| 254 | +testQuartoCmd( |
| 255 | + "render", |
| 256 | + [projectDir], |
| 257 | + [ |
| 258 | + noErrors, |
| 259 | + fileExists(join(outputDir, "expected.html")), // Should exist |
| 260 | + pathDoNotExists(join(outputDir, "excluded.html")), // Should NOT exist |
| 261 | + ], |
| 262 | + // ... |
| 263 | +); |
| 264 | +``` |
| 265 | + |
| 266 | +Run test **without fix** first to verify it fails, then verify it passes with fix. |
0 commit comments