-
Notifications
You must be signed in to change notification settings - Fork 58
fix(manifest): keep the Maven extension build out of the developer's home directory #1461
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
Merged
+135
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/cli/test/unit/scripts/maven-extension-build-jar.test.mts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /** | ||
| * Unit tests for the Maven extension build script's cache-root resolution. | ||
| * | ||
| * Purpose: build-jar.sh creates the Maven home up front, then runs Maven from | ||
| * inside the extension directory. Those two steps must agree on which | ||
| * directory they mean, so the resolved home has to be absolute before the cd. | ||
| * | ||
| * Related Files: | ||
| * - src/commands/manifest/scripts/maven-extension/build-jar.sh (implementation) | ||
| */ | ||
|
|
||
| import { | ||
| mkdtempSync, | ||
| readFileSync, | ||
| realpathSync, | ||
| statSync, | ||
| writeFileSync, | ||
| } from 'node:fs' | ||
| import os from 'node:os' | ||
| import path from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
|
|
||
| import { safeDelete } from '@socketsecurity/lib-stable/fs/safe' | ||
| import { spawn } from '@socketsecurity/lib-stable/process/spawn/child' | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
|
||
| const BUILD_JAR_PATH = path.join( | ||
| __dirname, | ||
| '../../../src/commands/manifest/scripts/maven-extension/build-jar.sh', | ||
| ) | ||
|
|
||
| /** | ||
| * The script's leading cache-root resolution, up to the subshell that invokes | ||
| * Maven. Running just this part exercises the shipped lines without needing a | ||
| * JDK, the Maven wrapper, or the network. | ||
| */ | ||
| function readResolutionPrelude(): string { | ||
| const lines = readFileSync(BUILD_JAR_PATH, 'utf8').split('\n') | ||
| const subshellAt = lines.indexOf('(') | ||
| if (subshellAt === -1) { | ||
| throw new Error('build-jar.sh no longer opens a subshell with a bare "("') | ||
| } | ||
| return lines.slice(0, subshellAt).join('\n') | ||
| } | ||
|
|
||
| /** | ||
| * Run the prelude with `cwd` as the working directory and report the | ||
| * `maven_home` it settled on. | ||
| */ | ||
| async function resolveMavenHome( | ||
| cwd: string, | ||
| env: Record<string, string | undefined>, | ||
| ): Promise<string> { | ||
| const preludePath = path.join(cwd, 'prelude.sh') | ||
| writeFileSync( | ||
| preludePath, | ||
| `${readResolutionPrelude()}\nprintf '%s\\n' "$maven_home"\n`, | ||
| ) | ||
|
|
||
| const result = await spawn('bash', [preludePath], { | ||
| cwd, | ||
| env: { ...process.env, ...env }, | ||
| }) | ||
| return result.stdout.trim() | ||
| } | ||
|
|
||
| describe('maven-extension build-jar.sh cache root', () => { | ||
| let workDir: string | ||
|
|
||
| beforeEach(() => { | ||
| workDir = realpathSync( | ||
| mkdtempSync(path.join(os.tmpdir(), 'build-jar-test-')), | ||
| ) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await safeDelete(workDir) | ||
| }) | ||
|
|
||
| it('anchors a relative SOCKET_CLI_MAVEN_HOME to the caller, not the extension directory', async () => { | ||
| const mavenHome = await resolveMavenHome(workDir, { | ||
| SOCKET_CLI_MAVEN_HOME: 'relative/maven-home', | ||
| }) | ||
|
|
||
| // Absolute, so the cd into the extension directory cannot re-anchor it. | ||
| expect(path.isAbsolute(mavenHome)).toBe(true) | ||
| expect(realpathSync(mavenHome)).toBe( | ||
| path.join(workDir, 'relative/maven-home'), | ||
| ) | ||
| // The directory the script created is the one Maven will be pointed at. | ||
| expect(statSync(mavenHome).isDirectory()).toBe(true) | ||
| }) | ||
|
|
||
| it('anchors a relative TMPDIR to the caller', async () => { | ||
| const mavenHome = await resolveMavenHome(workDir, { | ||
| SOCKET_CLI_MAVEN_HOME: undefined, | ||
| TMPDIR: 'relative-tmp', | ||
| }) | ||
|
|
||
| expect(path.isAbsolute(mavenHome)).toBe(true) | ||
| expect(realpathSync(mavenHome)).toBe( | ||
| path.join(workDir, 'relative-tmp/socket-cli-maven-home'), | ||
| ) | ||
| }) | ||
|
|
||
| it('leaves an absolute SOCKET_CLI_MAVEN_HOME alone', async () => { | ||
| const absoluteHome = path.join(workDir, 'absolute/maven-home') | ||
|
|
||
| const mavenHome = await resolveMavenHome(workDir, { | ||
| SOCKET_CLI_MAVEN_HOME: absoluteHome, | ||
| }) | ||
|
|
||
| expect(realpathSync(mavenHome)).toBe(absoluteHome) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.