Skip to content

Commit

Permalink
fix: update unit test and remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
acke committed Sep 16, 2024
1 parent cc02eb7 commit f4e88fd
Showing 1 changed file with 12 additions and 88 deletions.
100 changes: 12 additions & 88 deletions src/test/kotlin/io/snyk/plugin/ui/jcef/ApplyFixHandlerTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.snyk.plugin.ui.jcef

import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import org.junit.Test
import junit.framework.TestCase.assertEquals
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import io.mockk.unmockkAll
import io.snyk.plugin.resetSettings

class DiffPatchTest {

class DiffPatchTest : BasePlatformTestCase(){
private val originalFileContent = """
/*
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
Expand Down Expand Up @@ -46,8 +52,10 @@ class DiffPatchTest {

@Test
fun `test applying patch`() {
val diffPatch = parseDiff(responseDiff)
val patchedContent = applyPatch(originalFileContent, diffPatch)
val applyFixHandler = ApplyFixHandler(project)

val diffPatch = applyFixHandler.parseDiff(responseDiff)
val patchedContent = applyFixHandler.applyPatch(originalFileContent, diffPatch)

val expectedPatchedContent = """
/*
Expand Down Expand Up @@ -76,88 +84,4 @@ class DiffPatchTest {

assertEquals(expectedPatchedContent, patchedContent)
}

private fun applyPatch(fileContent: String, diffPatch: DiffPatch): String {
val lines = fileContent.lines().toMutableList()

for (hunk in diffPatch.hunks) {
var originalLineIndex = hunk.startLineOriginal - 1 // Convert to 0-based index

for (change in hunk.changes) {
when (change) {
is Change.Addition -> {
lines.add(originalLineIndex, change.line)
originalLineIndex++
}
is Change.Deletion -> {
if (originalLineIndex < lines.size && lines[originalLineIndex].trim() == change.line) {
lines.removeAt(originalLineIndex)
}
}
is Change.Context -> {
originalLineIndex++ // Move past unchanged context lines
}
}
}
}
return lines.joinToString("\n")
}

private fun parseDiff(diff: String): DiffPatch {
val lines = diff.lines()
val originalFile = lines.first { it.startsWith("---") }.substringAfter("--- ")
val fixedFile = lines.first { it.startsWith("+++") }.substringAfter("+++ ")

val hunks = mutableListOf<Hunk>()
var currentHunk: Hunk? = null
val changes = mutableListOf<Change>()

for (line in lines) {
when {
line.startsWith("@@") -> {
// Parse hunk header (e.g., @@ -4,9 +4,14 @@)
val hunkHeader = line.substringAfter("@@ ").substringBefore(" @@").split(" ")
val original = hunkHeader[0].substring(1).split(",")
val fixed = hunkHeader[1].substring(1).split(",")

val startLineOriginal = original[0].toInt()
val numLinesOriginal = original.getOrNull(1)?.toInt() ?: 1
val startLineFixed = fixed[0].toInt()
val numLinesFixed = fixed.getOrNull(1)?.toInt() ?: 1

if (currentHunk != null) {
hunks.add(currentHunk.copy(changes = changes.toList()))
changes.clear()
}
currentHunk = Hunk(
startLineOriginal = startLineOriginal,
numLinesOriginal = numLinesOriginal,
startLineFixed = startLineFixed,
numLinesFixed = numLinesFixed,
changes = emptyList()
)
}

line.startsWith("---") || line.startsWith("+++") -> {
// Skip file metadata lines (--- and +++)
continue
}

line.startsWith("-") -> changes.add(Change.Deletion(line.substring(1).trim()))
line.startsWith("+") -> changes.add(Change.Addition(line.substring(1).trim()))
else -> changes.add(Change.Context(line.trim()))
}
}

// Add the last hunk
if (currentHunk != null) {
hunks.add(currentHunk.copy(changes = changes.toList()))
}

return DiffPatch(
originalFile = originalFile,
fixedFile = fixedFile,
hunks = hunks
)
}
}

0 comments on commit f4e88fd

Please sign in to comment.