From 4e02564e6ed22fe82b964c0961cb2e7e52cb8c94 Mon Sep 17 00:00:00 2001 From: Brian Phillips Date: Fri, 10 Jan 2025 16:50:13 -0500 Subject: [PATCH] JAVA-8886 :construction: updated inputs, now applies real agent to test app tests --- .../gradle/plugin/ContrastGradlePlugin.java | 4 +++- .../gradle/plugin/ResolveAgentTask.java | 17 ++++++----------- .../gradle/plugin/e2e/EndToEndTests.java | 3 ++- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastGradlePlugin.java b/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastGradlePlugin.java index 13cf48a..9bbdba3 100644 --- a/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastGradlePlugin.java +++ b/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastGradlePlugin.java @@ -29,7 +29,8 @@ public void apply(final Project target) { resolveAgentTask.configure( task -> { - task.setFileString("contrast.jar"); + task.setFileString( + target.getLayout().getBuildDirectory().getAsFile().get().getPath() + "/contrast.jar"); task.setAgent( new File( target.getLayout().getBuildDirectory().getAsFile().get().getPath(), @@ -43,6 +44,7 @@ public void apply(final Project target) { .register( "contrastCheck", task -> { + task.dependsOn(resolveAgentTask); target .getTasks() .withType(Test.class) diff --git a/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ResolveAgentTask.java b/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ResolveAgentTask.java index 79ac036..a7da9ca 100644 --- a/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ResolveAgentTask.java +++ b/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ResolveAgentTask.java @@ -51,7 +51,7 @@ public void setAgent(File agent) { void resolveAgent() { System.out.println("running RESOLVE AGENT"); final ContrastSDK sdk = ContrastSDKService.getInstance(config); - agent = retrieveAgent(sdk, config.getJarPath(), config.getOrgUuid(), getProject()); + agent = retrieveAgent(sdk, getFileString(), config.getOrgUuid(), getProject()); } /** Use ContrastSDK to download agent and return the path where agent jar is stored */ @@ -67,20 +67,15 @@ public static File retrieveAgent( if (jarPath != null) { final Path agent = Paths.get(jarPath); if (!Files.exists(agent)) { - throw new GradleException("Unable to find java agent at " + jarPath); + logger.debug("Unable to find java agent at " + jarPath); + } else { + logger.debug("Agent provided via configuration retrieved"); + return new File(agent.toString()); } - logger.debug("Agent provided via configuration retrieved"); - return new File(agent.toString()); } logger.debug("No agent path provided, checking for cached agent"); - final Path agent = Paths.get(project.getProjectDir().getPath()).resolve(AGENT_NAME); - if (Files.exists(agent)) { - System.out.println("Agent jar found at " + project.getProjectDir().getPath()); - return new File(agent.toString()); - } - logger.debug("Attempting to retrieve agent from TeamServer"); // If no jar is provided, and no jarpath configured, attempt to retrieve the agent from TS final byte[] bytes; @@ -97,7 +92,7 @@ public static File retrieveAgent( logger.debug("Project dir already exists"); } - downloadedAgent = target.resolve(AGENT_NAME); + downloadedAgent = target.resolve(jarPath); Files.write(downloadedAgent, bytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE); diff --git a/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java b/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java index e234644..1e39d4e 100644 --- a/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java +++ b/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java @@ -49,6 +49,7 @@ void verify_attaches_agent_to_tests() throws IOException { final GradleRunner testRunner = GradleRunner.create(); testRunner.forwardOutput(); testRunner.withPluginClasspath(); + testRunner.withArguments("fakeTask", "contrastCheck"); // outputs debug logs to stdout for testing testRunner.withDebug(true); testRunner.withProjectDir(projectDir); @@ -58,7 +59,7 @@ void verify_attaches_agent_to_tests() throws IOException { .getTasks() .forEach( buildTask -> { - assertEquals(buildTask.getOutcome(), TaskOutcome.SUCCESS); + assertEquals(buildTask.getOutcome(), TaskOutcome.UP_TO_DATE); }); for (final String arg : AGENT_ARGS) {