Skip to content

Commit 051b639

Browse files
authored
Encapsulate detekt-gradle JAVA_HOME logic (#411)
Ensure we use our hermetic install of Java for detekt-gradle tests, without having to specify it at the workflow level. Tested successfully in the cross-repo workflow.
1 parent f8aab4f commit 051b639

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

linters/detekt/detekt.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import path from "path";
33
import { customLinterCheckTest, linterCheckTest, TestCallback } from "tests";
44
import { TrunkLintDriver } from "tests/driver";
5-
import { osTimeoutMultiplier, skipOS, TEST_DATA } from "tests/utils";
5+
import { DOWNLOAD_CACHE, osTimeoutMultiplier, recurseLevels, skipOS, TEST_DATA } from "tests/utils";
66

77
// detekt tests can sometimes take a while.
88
jest.setTimeout(300000 * osTimeoutMultiplier); // 300s or 900s
@@ -38,6 +38,23 @@ const gradlePreCheck: TestCallback = (driver) => {
3838
fs.readdirSync(path.resolve(driver.getSandbox(), TEST_DATA, "detekt_gradle")).forEach((file) => {
3939
driver.moveFile(path.join(TEST_DATA, "detekt_gradle", file), file);
4040
});
41+
42+
const trunkYamlPath = ".trunk/trunk.yaml";
43+
const currentContents = driver.readFile(trunkYamlPath);
44+
const newContents = currentContents.concat(` definitions:
45+
- name: detekt-gradle
46+
runtime: java
47+
`);
48+
driver.writeFile(trunkYamlPath, newContents);
49+
50+
driver.runTrunkSync(["install"]);
51+
const javaPath = recurseLevels(path.resolve(DOWNLOAD_CACHE, "jdk-13"), 1);
52+
const finalContents = newContents.concat(` environment:
53+
- name: JAVA_HOME
54+
value: ${javaPath}
55+
optional: false
56+
`);
57+
driver.writeFile(trunkYamlPath, finalContents);
4158
// trunk-ignore-end(semgrep)
4259
};
4360

tests/driver/driver.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import * as os from "os";
55
import path from "path";
66
import * as git from "simple-git";
77
import { SetupSettings } from "tests/driver";
8-
import { ARGS, REPO_ROOT, TEST_DATA } from "tests/utils";
8+
import { ARGS, DOWNLOAD_CACHE, REPO_ROOT, TEMP_PREFIX, TEST_DATA } from "tests/utils";
99
import { getTrunkConfig, newTrunkYamlContents } from "tests/utils/trunk_config";
1010
import * as util from "util";
1111
import YAML from "yaml";
1212

1313
const execFilePromise = util.promisify(execFile);
1414

15-
const TEMP_PREFIX = "plugins_";
16-
1715
const TEMP_SUBDIR = "tmp";
1816

1917
const UNINITIALIZED_ERROR = `You have attempted to modify the sandbox before it was created.
@@ -25,10 +23,7 @@ export const executionEnv = (sandbox: string) => {
2523
return {
2624
...strippedEnv,
2725
// This keeps test downloads separate from manual trunk invocations
28-
TRUNK_DOWNLOAD_CACHE: path.resolve(
29-
fs.realpathSync(os.tmpdir()),
30-
`${TEMP_PREFIX}testing_download_cache`,
31-
),
26+
TRUNK_DOWNLOAD_CACHE: DOWNLOAD_CACHE,
3227
// This is necessary to prevent launcher collision of non-atomic operations
3328
TMPDIR: path.resolve(sandbox, TEMP_SUBDIR),
3429
};

tests/utils/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import Debug from "debug";
22
import fs from "fs";
3+
import * as os from "os";
34
import path from "path";
45
import semver from "semver";
56
import { CheckType, LandingState, LinterVersion, TaskFailure, TestingArguments } from "tests/types";
67

78
export const REPO_ROOT = path.resolve(__dirname, "../..");
89
export const TEST_DATA = "test_data";
10+
export const TEMP_PREFIX = "plugins_";
11+
export const DOWNLOAD_CACHE = path.resolve(
12+
fs.realpathSync(os.tmpdir()),
13+
`${TEMP_PREFIX}testing_download_cache`,
14+
);
915

1016
// As this file and folder increase in complexity, extract out functionality into other categories.
1117
// Avoid overpolluting a `utils` folder.
@@ -218,6 +224,24 @@ export const getVersionsForTest = (
218224
return [undefined];
219225
};
220226

227+
/**
228+
* Helper function to step N directories into a path. Throws if unavailable
229+
*/
230+
export const recurseLevels = (starterPath: string, n: number) => {
231+
let currentPath = starterPath;
232+
for (let i = 0; i < n; i++) {
233+
const contents = fs.readdirSync(currentPath);
234+
for (const file of contents) {
235+
if (fs.lstatSync(path.resolve(currentPath, file)).isDirectory()) {
236+
currentPath = path.resolve(currentPath, file);
237+
break;
238+
}
239+
throw new Error(`Could not find directory inside of ${currentPath}`);
240+
}
241+
}
242+
return currentPath;
243+
};
244+
221245
/**
222246
* Helper callback that skips a test if the OS is included in excludedOS.
223247
* Intended to be passed to `skipTestIf`.

0 commit comments

Comments
 (0)