Skip to content
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

Use erl to retrieve OTP_RELEASE & ERLANG_SDK_HOME if no environment variable is set when testing #3600

Merged
Merged
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
83 changes: 80 additions & 3 deletions jps-builder/tests/org/elixir_lang/jps/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,62 @@

import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;


/**
* Created by zyuyou on 15/7/17.
*/
public class BuilderTest extends JpsBuildTestCase {
private static final String TEST_MODULE_NAME = "m";
private JpsSdk<SdkProperties> elixirSdk;

private static String otpReleaseVersion = null;

/**
* Retrieves the OTP Release version, eg 24.3.4.6
* This method first checks for an environment variable OTP_RELEASE (used in CI).
* If not set, it attempts to infer the version path from the erl command.
*
* @return A String representing the OTP Release version.
* @throws AssertionError if ERLANG_SDK_HOME is not set and cannot be inferred from PATH.
*/
private static String otpRelease() {
if (otpReleaseVersion != null) {
return otpReleaseVersion;
}

String otpRelease = System.getenv("OTP_RELEASE");
if (otpRelease != null && !otpRelease.isEmpty()) {
otpReleaseVersion = otpRelease;
return otpRelease;
}

assertNotNull("OTP_RELEASE is not set", otpRelease);
try {
Process process = Runtime.getRuntime().exec(new String[]{
"erl",
"-eval",
"{ok, Version} = file:read_file(filename:join([code:root_dir(), \"releases\", erlang:system_info(otp_release), \"OTP_VERSION\"])), io:fwrite(Version), halt().",
"-noshell"
});

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
otpRelease = reader.readLine();
}

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Failed to get OTP release. Exit code: " + exitCode);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Failed to get OTP release", e);
}

if (otpRelease == null || otpRelease.isEmpty()) {
throw new RuntimeException("Failed to get OTP release");
}

otpReleaseVersion = otpRelease;
return otpRelease;
}

Expand All @@ -42,11 +85,45 @@ private static String elixirSdkHome() {
return sdkHomeFromEbinDirectory(ebinDirectory());
}

/**
* Retrieves the Erlang SDK home directory.
* This method first checks for an environment variable ERLANG_SDK_HOME.
* If not set, it attempts to infer the SDK home from the erl command.
*
* @return A String representing the path to the Erlang SDK home directory.
* @throws AssertionError if ERLANG_SDK_HOME is not set and cannot be retrieved from the erl command.
*/
@NotNull
private static String erlangSdkHome() {
String erlangSdkHome = System.getenv("ERLANG_SDK_HOME");

assertNotNull("ERLANG_SDK_HOME is not set", erlangSdkHome);
if (erlangSdkHome != null && !erlangSdkHome.isEmpty()) {
return erlangSdkHome;
}

try {
Process process = Runtime.getRuntime().exec(new String[]{
"erl",
"-eval",
"io:format(\"~s~n\", [code:root_dir()]), halt().",
"-noshell"
});

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
erlangSdkHome = reader.readLine();
}

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Failed to get Erlang SDK home. Exit code: " + exitCode);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Failed to get Erlang SDK home", e);
}

if (erlangSdkHome == null || erlangSdkHome.isEmpty()) {
throw new RuntimeException("Failed to get Erlang SDK home");
}

return erlangSdkHome;
}
Expand Down
Loading