Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #398 from dos65/fix_javahome_macos
Browse files Browse the repository at this point in the history
Fix javaHome selection for MacOs
  • Loading branch information
tgodzik authored Jan 16, 2022
2 parents 44a0b21 + a4532c4 commit 7762b12
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 66 deletions.
43 changes: 13 additions & 30 deletions src/__tests__/getJavaHome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,15 @@ describe("getJavaHome", () => {
const javaPaths = [
{
binPath: path.join("/", "test", "usr", "bin", "java"),
realPath: path.join(
"/",
"test",
"usr",
"lib",
"jvm",
"java-11-openjdk-amd64",
"bin",
"java"
),
realPath: path.join(java8Jdk.path, "bin", "java"),
},
];
const PATH = path.join("/", "test", "usr", "bin");
mockLocateJavaHome([java8Jdk, java11Jdk]);
mockFs(javaPaths);
process.env = { PATH };
const javaHome = await getJavaHome(undefined);
const expected = path.join(
"/",
"test",
"usr",
"lib",
"jvm",
"java-11-openjdk-amd64"
);
expect(javaHome).toBe(expected);
const javaHome = await require("../getJavaHome").getJavaHome(undefined);
expect(javaHome).toBe(java8Jdk.path);
});

it("prefers configuration to JAVA_HOME and installed Java", async () => {
Expand Down Expand Up @@ -105,42 +89,42 @@ describe("getJavaHome", () => {
});

const java8Jdk = {
path: "/path/to/java8jdk",
path: path.join("/", "path", "to", "java8jdk"),
version: "1.8.0",
security: 1,
isJDK: true,
};

const java8Jre = {
path: "/path/to/java8jdk",
path: path.join("/", "path", "to", "java8jdk"),
version: "1.8.0",
security: 1,
isJDK: false,
};

const java11Jdk = {
path: "/path/to/java11jdk",
path: path.join("/", "path", "to", "java11jdk"),
version: "1.11.0",
security: 1,
isJDK: true,
};

const java17Jdk = {
path: "/path/to/java17jdk",
path: path.join("/", "path", "to", "java17jdk"),
version: "1.17.0",
security: 1,
isJDK: true,
};

const java11Jre = {
path: "/path/to/java11jdk",
path: path.join("/", "path", "to", "java11jdk"),
version: "1.11.0",
security: 1,
isJDK: false,
};

const java11JdkNewPatch = {
path: "/path/to/java11jdk/high/security",
path: path.join("/", "path", "to", "java11jdk", "high", "securiry"),
version: "1.11.0",
security: 192,
isJDK: true,
Expand All @@ -159,17 +143,16 @@ function mockLocateJavaHome(
...j,
is64Bit: true,
executables: {
java: j.path + "/bin/java",
javac: j.path + "/bin/javac",
javap: j.path + "/bin/javap",
java: path.join(j.path, "bin", "java"),
javac: path.join(j.path, "bin", "javac"),
javap: path.join(j.path, "bin", "javap"),
},
}))
);
});
}

function mockFs(javaLinks: { binPath: String; realPath: String }[]): void {
jest.resetModules();
jest
.spyOn(require("fs"), "existsSync")
.mockImplementation((path: unknown) => {
Expand Down
77 changes: 41 additions & 36 deletions src/getJavaHome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ export function getJavaHome(
configuredJavaHome: string | undefined
): Promise<string> {
return toPromise(
pipe(
fromConfig(configuredJavaHome),
TE.orElse(fromEnv),
TE.orElse(fromPath),
TE.orElse(locate)
)
pipe(fromConfig(configuredJavaHome), TE.orElse(fromEnv), TE.orElse(locate))
);
}

Expand All @@ -55,43 +50,16 @@ function fromEnv(): TaskEither<unknown, string> {
return javaHome ? TE.right(javaHome) : TE.left({});
}

function fromPath(): TaskEither<unknown, string> {
const value = process.env["PATH"];
if (value) {
const result = value
.split(path.delimiter)
.map((p) => path.join(p, "java"))
.filter((p) => fs.existsSync(p));

if (result.length > 0) {
const realpath = fs.realpathSync(result[0]);
if (realpath.endsWith(path.join("bin", "java"))) {
const normalized = path.normalize(path.join(realpath, "..", ".."));
return TE.right(normalized);
} else {
return TE.left({});
}
} else {
return TE.left({});
}
} else {
return TE.left({});
}
}

function locate(): TaskEither<Error, string> {
return pipe(
locateJavaHome({ version: ">=1.8 <=17" }),
chain((javaHomes) => {
if (!javaHomes || javaHomes.length === 0) {
return TE.left(new Error("No suitable Java version found"));
} else {
javaHomes.sort((a, b) => {
const byVersion = -semver.compare(a.version, b.version);
if (byVersion === 0) return b.security - a.security;
else return byVersion;
});
const jdkHome = javaHomes.find((j) => j.isJDK);
const jdkHomes = javaHomes.filter((j) => j.isJDK);
const fromBinPath = matchesBinFromPath(jdkHomes);
const jdkHome = fromBinPath ? fromBinPath : latestJdk(jdkHomes);
if (jdkHome) {
return TE.right(jdkHome.path);
} else {
Expand All @@ -102,6 +70,43 @@ function locate(): TaskEither<Error, string> {
);
}

function matchesBinFromPath(
jdkHomes: IJavaHomeInfo[]
): IJavaHomeInfo | undefined {
const value = process.env["PATH"];
if (value && jdkHomes.length > 0) {
const result = value
.split(path.delimiter)
.map((p) => path.join(p, "java"))
.filter((p) => fs.existsSync(p));

if (result.length > 0) {
const realpath = fs.realpathSync(result[0]);
const matched = jdkHomes.find((home) => {
const javaBin = path.join(home.path, "bin", "java");
return javaBin == realpath;
});
return matched;
} else {
return undefined;
}
} else {
return undefined;
}
}

function latestJdk(jdkHomes: IJavaHomeInfo[]): IJavaHomeInfo | undefined {
if (jdkHomes.length > 0) {
return jdkHomes.sort((a, b) => {
const byVersion = -semver.compare(a.version, b.version);
if (byVersion === 0) return b.security - a.security;
else return byVersion;
})[0];
} else {
return undefined;
}
}

function locateJavaHome(
opts: ILocateJavaHomeOptions
): TaskEither<Error, IJavaHomeInfo[] | undefined> {
Expand Down

0 comments on commit 7762b12

Please sign in to comment.