Skip to content

Make JRuby version checking more robust #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,24 @@ public JRubyVersion getVersion() {
try {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
this.newArguments().addArg("-v").execute(os);
final String[] versionParts = os.toString(StandardCharsets.UTF_8.toString()).split(" ");
final String jrubyVersion = versionParts[1];
final String languageVersion = extractLanguageVersion(versionParts[2]);
return new JRubyVersion(jrubyVersion, languageVersion);
String output = os.toString(StandardCharsets.UTF_8.toString());
final String[] lines = output.split("\n");
for (String line : lines) {
line = line.trim();
if (line.startsWith("jruby")) {
final String[] versionParts = output.split(" ");
final String jrubyVersion = versionParts[1];
final String languageVersion = extractLanguageVersion(versionParts[2]);
return new JRubyVersion(jrubyVersion, languageVersion);
}
}
logger.warn("Could not identify version from -v output: \n" + output);
} catch (Exception e) {
logger.warn("Could not identify version: " + e.getMessage());
return null;
}

// only in cases where we cannot parse the version
return null;
}

private String extractLanguageVersion(String versionPart) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.saumya.mojo.ruby.script;

import org.jruby.Main;
import org.jruby.runtime.Constants;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
Expand All @@ -14,6 +15,8 @@

public class ScriptFactoryTest {

private static final String BASE_JRUBY_VERSION_STRING = "jruby " + Constants.VERSION + " (" + Constants.RUBY_VERSION + ")";

@Test
public void should_execute_script_with_only_args_and_return_in_output_stream() throws ScriptException, IOException {
Main main = new Main();
Expand All @@ -25,7 +28,7 @@ public void should_execute_script_with_only_args_and_return_in_output_stream() t
.execute(outputStream);

final String output = outputStream.toString();
assertThat(output).startsWith("jruby 9.4.13.0 (3.1.4)");
assertThat(output).startsWith(BASE_JRUBY_VERSION_STRING);
}

@Test
Expand All @@ -40,7 +43,7 @@ public void should_execute_script_with_only_args_and_return_in_file() throws Scr

byte[] fileBytes = Files.readAllBytes(outputFile.toPath());
final String output = new String(fileBytes, StandardCharsets.UTF_8);
assertThat(output).startsWith("jruby 9.4.13.0 (3.1.4)");
assertThat(output).startsWith(BASE_JRUBY_VERSION_STRING);
}

@Test
Expand All @@ -49,7 +52,19 @@ public void should_return_jruby_version() throws ScriptException, IOException {

JRubyVersion version = gemScriptFactory.getVersion();

assertThat(version.getVersion()).isEqualTo("9.4.13.0");
assertThat(version.getLanguage()).isEqualTo("3.1.4");
assertThat(version.getVersion()).isEqualTo(Constants.VERSION);
assertThat(version.getLanguage()).isEqualTo(Constants.RUBY_VERSION);
}

@Test
public void should_return_jruby_version_with_jdk_options_set() throws ScriptException, IOException {
final GemScriptFactory gemScriptFactory = gemScriptFactory();

gemScriptFactory.addEnv("JDK_JAVA_OPTIONS", "-Xmx1G");

JRubyVersion version = gemScriptFactory.getVersion();

assertThat(version.getVersion()).isEqualTo(Constants.VERSION);
assertThat(version.getLanguage()).isEqualTo(Constants.RUBY_VERSION);
}
}