Skip to content

Commit d221fde

Browse files
authored
Use a custom executable to hook to test explorer (#3499)
1 parent 15c335a commit d221fde

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

exe/ruby-lsp-test-exec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Append to RUBYOPT the necessary requires to hook our custom test reporters so that results are automatically
5+
# reflected in the test explorer
6+
rubyopt = [
7+
*ENV["RUBYOPT"],
8+
"-rbundler/setup",
9+
"-r#{File.expand_path("../lib/ruby_lsp/test_reporters/minitest_reporter", __dir__)}",
10+
"-r#{File.expand_path("../lib/ruby_lsp/test_reporters/test_unit_reporter", __dir__)}",
11+
].join(" ")
12+
13+
# Replace this process with whatever command was passed. We only want to set RUBYOPT.
14+
# The way you use this executable is by prefixing your test command with `ruby-lsp-test-exec`, like so:
15+
# ruby-lsp-test-exec bundle exec ruby -Itest test/example_test.rb
16+
# ruby-lsp-test-exec bundle exec ruby -Ispec spec/example_spec.rb
17+
# ruby-lsp-test-exec bundle exec rspec spec/example_spec.rb
18+
exec({ "RUBYOPT" => rubyopt }, *ARGV)

jekyll/test_explorer.markdown

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ Automatically detecting what type of external argument is required for each test
107107
Code's test explorer doesn't have support for arguments when running tests out of the box and neither do its test
108108
items accept metadata. This scenario will not be supported by the Ruby LSP.
109109

110+
## Connecting terminal tests to the explorer
111+
112+
When running tests in the terminal through a code lens or test explorer, the Ruby LSP uses the `ruby-lsp-test-exec`
113+
executable, which hooks the test run to the extension so that we can show test results in the explorer.
114+
115+
By running tests with this executable, even manually written test commands will also have their results reported
116+
to the test explorer. For example, all of the following will report test statuses to the extension:
117+
118+
```shell
119+
ruby-lsp-test-exec bundle exec ruby -Itest test/example_test.rb
120+
ruby-lsp-test-exec bundle exec ruby -Ispec spec/example_spec.rb
121+
ruby-lsp-test-exec bundle exec rspec spec/example_spec.rb
122+
```
123+
110124
## Customization
111125

112126
When tests are running through any execution mode, we set the `RUBY_LSP_TEST_RUNNER` environment variable to allow

ruby-lsp.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
1515

1616
s.files = Dir.glob("lib/**/*.rb") + ["README.md", "VERSION", "LICENSE.txt"] + Dir.glob("static_docs/**/*.md")
1717
s.bindir = "exe"
18-
s.executables = ["ruby-lsp", "ruby-lsp-check", "ruby-lsp-launcher"]
18+
s.executables = ["ruby-lsp", "ruby-lsp-check", "ruby-lsp-launcher", "ruby-lsp-test-exec"]
1919
s.require_paths = ["lib"]
2020

2121
# Dependencies must be kept in sync with the checks in the extension side on workspace.ts

vscode/src/streamingRunner.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class StreamingRunner implements vscode.Disposable {
114114
abortController,
115115
);
116116
} else if (mode === Mode.RunInTerminal) {
117-
this.runInTerminal(command, env, workspace.workspaceFolder);
117+
this.runInTerminal(command, workspace.workspaceFolder);
118118
} else {
119119
// eslint-disable-next-line @typescript-eslint/no-floating-promises
120120
this.launchDebugger(command, env, workspace);
@@ -164,7 +164,6 @@ export class StreamingRunner implements vscode.Disposable {
164164
// Run the given test in the terminal
165165
private runInTerminal(
166166
command: string,
167-
env: NodeJS.ProcessEnv,
168167
workspaceFolder: vscode.WorkspaceFolder,
169168
) {
170169
const cwd = workspaceFolder.uri.fsPath;
@@ -177,17 +176,14 @@ export class StreamingRunner implements vscode.Disposable {
177176
});
178177
}
179178

180-
// We need to send RUBYOPT since that hooks up the custom LSP test reporters and the user's shell may override it
181-
if (process.platform === "win32") {
182-
terminal.sendText(`$env:RUBYOPT="${env.RUBYOPT}"; Clear-Host`);
183-
} else {
184-
terminal.sendText(`export RUBYOPT="${env.RUBYOPT}"; clear`);
185-
}
186-
187179
this.terminals.set(name, terminal);
188180

189181
terminal.show();
190-
terminal.sendText(command);
182+
const exec =
183+
path.basename(cwd) === "ruby-lsp" && os.platform() !== "win32"
184+
? "exe/ruby-lsp-test-exec"
185+
: "ruby-lsp-test-exec";
186+
terminal.sendText(`${exec} ${command}`);
191187
}
192188

193189
// Spawns the test process and redirects any stdout or stderr output to the test run output

0 commit comments

Comments
 (0)