Skip to content

Commit

Permalink
feat: for Gradle 7 disable configuration cache by default (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
ola magdziarek authored Jan 5, 2023
1 parent 4bf60a6 commit ca67209
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 20 deletions.
31 changes: 28 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,19 @@ async function getAllDepsWithPlugin(
root: string,
targetFile: string,
options: Options,
gradleVersion: string,
): Promise<JsonDepsScriptResult> {
const command = getCommand(root, targetFile);
const { injectedPluginFilePath, cleanupCallback } = await injectedPlugin(
'init.gradle',
);
const args = buildArgs(root, targetFile, injectedPluginFilePath, options);
const args = buildArgs(
root,
targetFile,
injectedPluginFilePath,
options,
gradleVersion,
);

const fullCommandText = 'gradle command: ' + command + ' ' + args.join(' ');
debugLog('Executing ' + fullCommandText);
Expand Down Expand Up @@ -488,7 +495,12 @@ async function getAllDeps(
}

try {
const extractedJSON = await getAllDepsWithPlugin(root, targetFile, options);
const extractedJSON = await getAllDepsWithPlugin(
root,
targetFile,
options,
gradleVersion,
);
const versionBuildInfo = getVersionBuildInfo(gradleVersion);
if (versionBuildInfo) {
extractedJSON.versionBuildInfo = versionBuildInfo;
Expand Down Expand Up @@ -674,8 +686,9 @@ function buildArgs(
targetFile: string | null,
initGradlePath: string,
options: Options,
gradleVersion: string,
) {
const args: string[] = [];
let args: string[] = [];
const taskName = options.gradleNormalizeDeps
? 'snykNormalizedResolvedDepsJson'
: 'snykResolvedDepsJson';
Expand Down Expand Up @@ -729,6 +742,10 @@ function buildArgs(
args.push(...options.args);
}

// Gradle 7 introduced configuration caching which we don't support yet
// If it is enabled in a gradle.properties file, it can be disabled on the command line with --no-configuration-cache
if (gradleVersion.match(/Gradle 7/)) args.push('--no-configuration-cache');

// There might be a legacy --configuration option in 'args'.
// It has been superseded by --configuration-matching option for Snyk CLI (see buildArgs),
// but we are handling it to support the legacy setups.
Expand All @@ -745,6 +762,14 @@ function buildArgs(
}
});

const unsupportedArgs = ['--configuration-cache'];
args = args.filter((arg) => {
if (unsupportedArgs.includes(arg)) {
debugLog(`Argument ${arg} not currently supported by Snyk.`);
return false;
} else return true;
});

return args.filter(Boolean);
}

Expand Down
109 changes: 92 additions & 17 deletions test/functional/gradle-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { exportsForTests as testableMethods } from '../../lib';

const JEST_TIMEOUT = 15000;
const gradleVersion = 'Gradle 6';

describe('Gradle Plugin', () => {
it('check build args (plain console output)', () => {
const result = testableMethods.buildArgs('.', null, '/tmp/init.gradle', {});
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{},
gradleVersion,
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
Expand All @@ -18,10 +25,16 @@ describe('Gradle Plugin', () => {
});

it('check build args with array (new configuration arg)', async () => {
const result = testableMethods.buildArgs('.', null, '/tmp/init.gradle', {
'configuration-matching': 'confRegex',
args: ['--build-file', 'build.gradle'],
});
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{
'configuration-matching': 'confRegex',
args: ['--build-file', 'build.gradle'],
},
gradleVersion,
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
Expand All @@ -38,11 +51,17 @@ describe('Gradle Plugin', () => {
});

it('check build args with array (new configuration arg) with --deamon', async () => {
const result = testableMethods.buildArgs('.', null, '/tmp/init.gradle', {
daemon: true,
'configuration-matching': 'confRegex',
args: ['--build-file', 'build.gradle'],
});
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{
daemon: true,
'configuration-matching': 'confRegex',
args: ['--build-file', 'build.gradle'],
},
gradleVersion,
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
Expand All @@ -58,9 +77,15 @@ describe('Gradle Plugin', () => {
});

it('check build args with array (legacy configuration arg)', async () => {
const result = testableMethods.buildArgs('.', null, '/tmp/init.gradle', {
args: ['--build-file', 'build.gradle', '--configuration=compile'],
});
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{
args: ['--build-file', 'build.gradle', '--configuration=compile'],
},
gradleVersion,
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
Expand All @@ -79,14 +104,21 @@ describe('Gradle Plugin', () => {
it(
'check build args with scan all subprojects',
async () => {
const result = testableMethods.buildArgs('.', null, '/tmp/init.gradle', {
allSubProjects: true,
args: ['--build-file', 'build.gradle', '--configuration', 'compile'],
});
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{
allSubProjects: true,
args: ['--build-file', 'build.gradle', '--configuration', 'compile'],
},
gradleVersion,
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
'--no-daemon',

'-Dorg.gradle.parallel=',
'-Dorg.gradle.console=plain',
'-I',
Expand All @@ -98,4 +130,47 @@ describe('Gradle Plugin', () => {
},
JEST_TIMEOUT,
);

it('make sure configuration cache is switched off even if requested', () => {
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{
args: ['--configuration-cache'],
},
gradleVersion,
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
'--no-daemon',
'-Dorg.gradle.parallel=',
'-Dorg.gradle.console=plain',
'-PonlySubProject=.',
'-I',
'/tmp/init.gradle',
]);
});

it('make sure configuration cache is switched off for Gradle 7', () => {
const result = testableMethods.buildArgs(
'.',
null,
'/tmp/init.gradle',
{},
'Gradle 7',
);
expect(result).toEqual([
'snykResolvedDepsJson',
'-q',
'--no-daemon',
'-Dorg.gradle.parallel=',
'-Dorg.gradle.console=plain',
'-PonlySubProject=.',
'-I',
'/tmp/init.gradle',
'--no-configuration-cache',
]);
});
});

0 comments on commit ca67209

Please sign in to comment.