Skip to content

Commit

Permalink
fix(gradle): invoke gradlew directly, not with /bin/sh (#4630) (#4671)
Browse files Browse the repository at this point in the history
Co-Authored-By: IKEDA Sho <suicaicoca@gmail.com>
  • Loading branch information
2 people authored and rarkins committed Oct 18, 2019
1 parent 2784016 commit 4d6b98f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/manager/gradle/index.ts
@@ -1,4 +1,4 @@
import { exists } from 'fs-extra';
import { access, constants, exists } from 'fs-extra';
import upath from 'upath';

import { exec } from '../../util/exec';
Expand Down Expand Up @@ -139,7 +139,10 @@ async function getGradleCommandLine(
cwd: string
): Promise<string> {
let cmd: string;
const gradlewExists = await exists(upath.join(cwd, 'gradlew'));
const gradlewPath = upath.join(cwd, 'gradlew');
const gradlewExists = await exists(gradlewPath);
const gradlewExecutable = gradlewExists && (await canExecute(gradlewPath));

if (config.binarySource === 'docker') {
cmd = `docker run --rm `;
// istanbul ignore if
Expand All @@ -148,6 +151,8 @@ async function getGradleCommandLine(
}
cmd += `-v ${cwd}:${cwd} -w ${cwd} `;
cmd += `renovate/gradle gradle`;
} else if (gradlewExecutable) {
cmd = './gradlew';
} else if (gradlewExists) {
cmd = 'sh gradlew';
} else {
Expand All @@ -156,4 +161,13 @@ async function getGradleCommandLine(
return cmd + ' ' + GRADLE_DEPENDENCY_REPORT_OPTIONS;
}

async function canExecute(path: string): Promise<boolean> {
try {
await access(path, constants.X_OK);
return true;
} catch {
return false;
}
}

export const language = 'java';
14 changes: 14 additions & 0 deletions test/manager/gradle/index.spec.ts
Expand Up @@ -30,6 +30,7 @@ describe('manager/gradle', () => {
fs.readFile.mockResolvedValue(updatesDependenciesReport as any);
fs.mkdir.mockResolvedValue();
fs.exists.mockResolvedValue(true);
fs.access.mockResolvedValue(undefined);
exec.mockResolvedValue({ stdout: 'gradle output', stderr: '' } as never);
platform.getFile.mockResolvedValue('some content');
});
Expand Down Expand Up @@ -109,6 +110,19 @@ describe('manager/gradle', () => {
it('should execute gradlew when available', async () => {
await manager.extractAllPackageFiles(config, ['build.gradle']);

expect(exec.mock.calls[0][0]).toBe(
'./gradlew --init-script renovate-plugin.gradle renovate'
);
expect(exec.mock.calls[0][1]).toMatchObject({
cwd: 'localDir',
timeout: 20000,
});
});

it('should run gradlew through `sh` when available but not executable', async () => {
fs.access.mockRejectedValue(undefined);
await manager.extractAllPackageFiles(config, ['build.gradle']);

expect(exec.mock.calls[0][0]).toBe(
'sh gradlew --init-script renovate-plugin.gradle renovate'
);
Expand Down

0 comments on commit 4d6b98f

Please sign in to comment.