Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import static org.hamcrest.MatcherAssert.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.difflib.DiffUtils;
import com.github.difflib.UnifiedDiffUtils;
import com.github.difflib.patch.Patch;
import io.codemodder.codemods.DefaultCodemods;
import io.codemodder.codetf.CodeTFChangesetEntry;
import io.codemodder.codetf.CodeTFReport;
import io.codemodder.codetf.CodeTFResult;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -23,6 +28,12 @@ final class WebGoat822Test extends GitRepositoryTest {

@Test
void it_injects_dependency_even_when_no_poms_included() throws Exception {

// save original pom contents
Path pom = repoDir.toPath().resolve("webgoat-lessons/insecure-deserialization/pom.xml");
assertThat(Files.exists(pom), is(true));
List<String> originalPomContentLines = Files.readAllLines(pom);

DefaultCodemods.main(
new String[] {
"--path-include",
Expand All @@ -47,6 +58,13 @@ void it_injects_dependency_even_when_no_poms_included() throws Exception {
"webgoat-lessons/insecure-deserialization/src/main/java/org/owasp/webgoat/deserialization/InsecureDeserializationTask.java"));
assertThat(
changeset.get(1).getPath(), equalTo("webgoat-lessons/insecure-deserialization/pom.xml"));

// verify that we can apply the pom diff back to the original pom as a patch
String newPomContents = Files.readString(pom);
List<String> pomPatchContents = changeset.get(1).getDiff().lines().toList();
Patch<String> pomPatch = UnifiedDiffUtils.parseUnifiedDiff(pomPatchContents);
List<String> newPomContentLines = DiffUtils.patch(originalPomContentLines, pomPatch);
assertThat(String.join("\n", newPomContentLines), equalTo(newPomContents.trim()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;
import org.mozilla.universalchardet.UniversalDetector;

/** Provides Maven dependency management functions to codemods. */
public final class MavenProvider implements ProjectProvider {
Expand Down Expand Up @@ -109,7 +110,12 @@ public PomUpdateResult updatePom(
null))
.collect(Collectors.toList());

var originalPomContents = Files.readAllLines(pomPath, Charset.defaultCharset());
String charsetDetected = UniversalDetector.detectCharset(pomPath);
if (charsetDetected == null) {
charsetDetected = "UTF-8";
}
Charset charset = Charset.forName(charsetDetected);
var originalPomContents = Files.readAllLines(pomPath, charset);

final Path newPomFile = Files.createTempFile("pom", ".xml");
Files.copy(pomPath, newPomFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
Expand All @@ -120,6 +126,7 @@ public PomUpdateResult updatePom(
AtomicReference<Collection<DependencyGAV>> foundDependenciesMapped =
new AtomicReference<>(getDependenciesFrom(pomPath));

final AtomicReference<String> detectedEndline = new AtomicReference<>(null);
mappedDependencies.forEach(
newDependency -> {
DependencyGAV newDependencyGAV =
Expand All @@ -143,6 +150,7 @@ public PomUpdateResult updatePom(
.withSkipIfNewer(true)
.withUseProperties(true)
.build();
detectedEndline.set(projectModel.getEndl());

boolean result = POMOperator.modify(projectModel);

Expand All @@ -161,7 +169,7 @@ public PomUpdateResult updatePom(
}
});

var finalPomContents = Files.readAllLines(newPomFile, Charset.defaultCharset());
var finalPomContents = Files.readAllLines(newPomFile, charset);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the kicker: POMOperator itself also detects both charsets and newlines (and trailing newline if thats the case) - for each and every file. They're held inside an instance of POMDocument - at the codemodder level, we only return bytes to be written

there's another problem: In the newer version of POMOperator, it might output several files at once - I suggest you target against the feature/multi-pom-support branch instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (finalPomContents.equals(originalPomContents)) {
return new PomUpdateResult(Optional.empty(), skippedDependencies);
}
Expand All @@ -177,7 +185,11 @@ public PomUpdateResult updatePom(
UnifiedDiffUtils.generateUnifiedDiff(
relativePomPath, relativePomPath, originalPomContents, patch, 3);

String diff = String.join("\n", patchDiff);
String endline = detectedEndline.get();
if (endline == null || endline.isEmpty()) {
endline = "\n";
}
String diff = String.join(endline, patchDiff);
CodeTFChangesetEntry entry = new CodeTFChangesetEntry(relativePomPath, diff, List.of(change));

// overwrite existing pom
Expand Down