Skip to content

Commit e93d180

Browse files
Add support for setting test output file path
1 parent 2fedbf3 commit e93d180

5 files changed

Lines changed: 43 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to the "spec-runner" extension will be documented in this file.
44

5+
## [4.1.0]
6+
7+
- Add config item `ruby-spec-runner.outputFilePath` to aid with running rspec via guard.
8+
59
## [4.0.1]
610

711
- Fix issue with changeDirectoryToWorkspaceRoot setting and fish shell (fixes #11, thanks @steveclarke)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ This extension contributes the following settings:
6565
- `ruby-spec-runner.clearTerminalOnTestRun`: Clear the terminal before running a test command
6666
- `ruby-spec-runner.rubyDebugger`: Select which debugging extension to use
6767
- `ruby-spec-runner.rewriteTestPaths`: Change the test path that is run. See [Rewriting the test file path](#rewriting-the-test-file-path) for more info.
68+
- `ruby-spec-runner.outputFilePath`: Specify the test output file path. Having a predictable path can allow for test runs to be triggered by other programs (eg guard)
6869

6970
### Rewriting the test file path
7071

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"icon": "images/icon.png",
1111
"license": "MIT",
12-
"version": "4.0.1",
12+
"version": "4.1.0",
1313
"engines": {
1414
"vscode": "^1.94.0"
1515
},
@@ -270,6 +270,12 @@
270270
}
271271
}
272272
}
273+
},
274+
"ruby-spec-runner.outputFilePath": {
275+
"type": "string",
276+
"default": "",
277+
"markdownDescription": "Specify the absolute path (e.g. `/path/to/my/rspec-results.json`) for the test result output. Useful for triggering rspec manually (eg from Guard).\n\nUses a temp file if left blank. **Changes require vscode restart**",
278+
"scope": "window"
273279
}
274280
}
275281
}

src/SpecRunnerConfig.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ export class SpecRunnerConfig {
165165
));
166166
}
167167

168+
get outputFilePath(): string | undefined {
169+
const config = vscode.workspace.getConfiguration().get<string>('ruby-spec-runner.outputFilePath');
170+
return config?.trim() === '' ? undefined : config;
171+
}
172+
173+
get usingCustomOutputPath(): boolean {
174+
// eslint-disable-next-line eqeqeq
175+
return this.outputFilePath != undefined;
176+
}
177+
168178
private getBooleanConfig(key: string, defaultValue: boolean) {
169179
const result = vscode.workspace.getConfiguration().get(key) as boolean | undefined;
170180
// eslint-disable-next-line eqeqeq

src/rspec/SpecResultInterpreter.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class SpecResultInterpreter {
1212
private config: SpecRunnerConfig;
1313
private presenter: SpecResultPresenter;
1414

15-
private _tmpOutputFile!: tmp.FileResult;
15+
private _outputFile!: tmp.FileResult;
1616

1717
constructor(config: SpecRunnerConfig, context: vscode.ExtensionContext, presenter: SpecResultPresenter) {
1818
this.config = config;
@@ -160,7 +160,26 @@ export class SpecResultInterpreter {
160160
}
161161

162162
private get outputFile() {
163-
return this._tmpOutputFile ||= this.createTempFile();
163+
164+
return this._outputFile ||= (this.config.usingCustomOutputPath ? this.createOutputFile() : this.createTempFile());
165+
}
166+
167+
private createOutputFile() {
168+
if (!this.config.outputFilePath) {
169+
throw new Error('Output file path is not set');
170+
}
171+
172+
vscode.workspace.fs.writeFile(vscode.Uri.file(this.config.outputFilePath), Buffer.from(''))
173+
174+
const file: tmp.FileResult = {
175+
name: this.config.outputFilePath,
176+
fd: -1, // dummy fd since we're not using real tmp file
177+
removeCallback: () => {} // dummy callback since we don't need to remove custom files
178+
};
179+
180+
chokidar.watch(file.name).on('change', (_event, _path) => this.updateFromOutputFile());
181+
182+
return file;
164183
}
165184

166185
private createTempFile() {

0 commit comments

Comments
 (0)