Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Amazon Q): fix for test generation target file missing from payload. #5261

Merged
merged 21 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
526d8c9
Fix for Test generation zip to include target file, Regex pattern fix…
ashishrp-aws Jan 14, 2025
e182ec6
Merge branch 'main' into workspace-test
ashishrp-aws Jan 14, 2025
d943f8b
Merge branch 'main' into workspace-test
ashishrp-aws Jan 14, 2025
346cee3
Merge branch 'main' into workspace-test
ashishrp-aws Jan 14, 2025
556f3f1
Merge branch 'main' into workspace-test
ashishrp-aws Jan 15, 2025
a394c85
Removing FeatureDevSessionContext changes.
ashishrp-aws Jan 15, 2025
9276a46
Merge branch 'main' into workspace-test
ashishrp-aws Jan 15, 2025
e7544f0
Added comment explaining file inclusion.
ashishrp-aws Jan 15, 2025
f19f44a
Merge branch 'main' into workspace-test
ashishrp-aws Jan 15, 2025
7365162
Added Timeout and length check for pattern matching
ashishrp-aws Jan 15, 2025
37dc441
detekt correction.
ashishrp-aws Jan 15, 2025
4662cca
detekt Main correction for Nameshadowing.
ashishrp-aws Jan 15, 2025
62f896d
Merge branch 'main' into workspace-test
ashishrp-aws Jan 16, 2025
89545c2
Reverting the FeatureDevSessionContext pattern matching chnages.
ashishrp-aws Jan 16, 2025
1a8c609
Correcting the chnages for FeatureDevSessionContext
ashishrp-aws Jan 16, 2025
a48f031
Merge branch 'main' into workspace-test
ashishrp-aws Jan 17, 2025
7d2979c
Merge branch 'main' into workspace-test
ashishrp-aws Jan 17, 2025
86efb24
Merge branch 'main' into workspace-test
ashishrp-aws Jan 17, 2025
812c790
Merge branch 'main' into workspace-test
ashishrp-aws Jan 17, 2025
12fc617
Merge branch 'main' into workspace-test
ashishrp-aws Jan 20, 2025
fd92af6
Merge branch 'main' into workspace-test
ashishrp-aws Jan 21, 2025
File filter

Filter by extension

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
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Amazon Q /test: Fix for test generation payload creation to not filter out target file."
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class CodeTestChatController(

val requestData = ChatRequestData(
tabId = session.tabId,
message = "Generate unit tests for the following part of my code: ${message.prompt}",
message = "Generate unit tests for the following part of my code: ${message.prompt.ifBlank { fileInfo.fileName }}",
activeFileContext = activeFileContext,
userIntent = UserIntent.GENERATE_UNIT_TESTS,
triggerType = TriggerType.ContextMenu,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.RuleChain
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
Expand Down Expand Up @@ -75,6 +76,7 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest
@Test
fun testZipProject() {
addFilesToProjectModule(
".gitignore",
".gradle/cached.jar",
"src/MyClass.java",
"gradlew",
Expand All @@ -83,6 +85,19 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest
"settings.gradle",
"build.gradle",
"gradle/wrapper/gradle-wrapper.properties",
"builder/GetTestBuilder.java", //check for false positives
".aws-sam/build/function1",
".gem/specs.rb",
"archive.zip",
"output.bin",
"images/logo.png",
"assets/header.jpg",
"icons/menu.svg",
"license.txt",
"License.md",
"node_modules/express",
"build/outputs",
"dist/bundle.js"
)

val zipResult = featureDevSessionContext.getProjectZip()
Expand All @@ -102,11 +117,73 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest
"gradlew",
"gradlew.bat",
"README.md",
"settings.gradle",
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed that settings.gradle and build.gradle are being dropped. These should be included as a part of the zip for /dev.

Is this the result of changing the gitignore used for the test or a change in the actual behavior of the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can amend this for /dev.

"build.gradle",
"gradle/wrapper/gradle-wrapper.properties",
"builder/GetTestBuilder.java"
)

assertTrue(zippedFiles == expectedFiles)
}

@Test
fun `test basic pattern conversion`() {
val input = "*.txt"
val expected = "(?:^|.*/?)[^/]*[^/]\\.txt(?:/.*)?\$"
Copy link
Contributor

Choose a reason for hiding this comment

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

How do I validate if this is correct?

This appears to be a snapshot test of what is built versus a functional test that demonstrates that what is an example file that would've been dropped is no longer dropped

Copy link
Contributor Author

@ashishrp-aws ashishrp-aws Jan 16, 2025

Choose a reason for hiding this comment

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

we can test for pattern matching as well. That's why i included more files in the project zip test to validate it. Including false positives.

Copy link
Contributor

Choose a reason for hiding this comment

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

My point is that these tests do little to validate behaviors and protect against regressions. If we need to update the logic, how does one go about constructing the expected value?

Regardless, outside of the zip test above, I'd like to see more examples of each of these type of files and how they respond to the presence of such inputs in the gitignore.

assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test pattern with special characters`() {
val input = "test[abc].txt"
val expected = "(?:^|.*/?)test\\[abc\\]\\.txt(?:/.*)?$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test pattern with double asterisk`() {
val input = "**/build"
val expected = "(?:^|.*/?).[^/]*[^/][^/]/build(?:/.*)?\$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test pattern starting with slash`() {
val input = "/root/file.txt"
val expected = "^root/file\\.txt(?:/.*)?$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test pattern ending with slash`() {
val input = "build/"
val expected = "(?:^|.*/?)build/.*"
Copy link
Contributor

Choose a reason for hiding this comment

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

these patterns have polynomial complexity. should there be a filter on input to prevent DoS of user's machine?

Copy link
Contributor

Choose a reason for hiding this comment

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

also can we investigate what it would look like to use the same glob logic that git uses to match files? seems like that might be simpler than trying to build these patterns

Copy link
Contributor Author

@ashishrp-aws ashishrp-aws Jan 15, 2025

Choose a reason for hiding this comment

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

Added checks for polynomial complexity and makes sense for glob logic. will take a look into it. Wanted to drop regex matching entirely for /test. Only reason still being used is to restrict payload size which is leading to payload size errors.

assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test pattern with question mark`() {
val input = "file?.txt"
val expected = "(?:^|.*/?)file[^/]\\.txt(?:/.*)?$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test complex pattern with multiple special characters`() {
val input = "**/test-[0-9]*.{java,kt}"
val expected = "(?:^|.*/?).[^/]*[^/][^/]/test-\\[0-9\\][^/]*[^/]\\.\\{java\\,kt\\}(?:/.*)?\$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test empty pattern`() {
val input = ""
val expected = "(?:^|.*/?)(?:/.*)?$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}

@Test
fun `test pattern with all special regex characters`() {
val input = ".$+()[]{}^|"
val expected = "(?:^|.*/?)\\.\\\$\\+\\(\\)\\[\\]\\{\\}\\^\\|(?:/.*)?$"
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class CodeTestSessionConfig(
}

// 2. Add the "utgRequiredArtifactsDir" directory
val utgDir = "utgRequiredArtifactsDir"
val utgDir = "utgRequiredArtifactsDir/" // Note the trailing slash which adds it as a directory and not a file
LOG.debug { "Adding directory to ZIP: $utgDir" }
val utgEntry = ZipEntry(utgDir)
it.putNextEntry(utgEntry)
Expand All @@ -150,7 +150,7 @@ class CodeTestSessionConfig(
val buildAndExecuteLogDir = "buildAndExecuteLogDir"
val subDirs = listOf(buildAndExecuteLogDir, "repoMapData", "testCoverageDir")
subDirs.forEach { subDir ->
val subDirPathString = Path.of(utgDir, subDir).name
val subDirPathString = Path.of(utgDir, subDir).toString() + "/" // Added trailing slash similar to utgRequiredArtifactsDir
LOG.debug { "Adding empty directory to ZIP: $subDirPathString" }
val zipEntry = ZipEntry(subDirPathString)
it.putNextEntry(zipEntry)
Expand All @@ -168,6 +168,18 @@ class CodeTestSessionConfig(
var currentTotalLines = 0L
val languageCounts = mutableMapOf<CodeWhispererProgrammingLanguage, Int>()

// Adding Target File to make sure target file doesn't get filtered out.
selectedFile?.let { selected ->
files.add(selected.path)
currentTotalFileSize += selected.length
currentTotalLines += countLinesInVirtualFile(selected)
selected.programmingLanguage().let { language ->
if (language !is CodeWhispererUnknownLanguage) {
languageCounts[language] = (languageCounts[language] ?: 0) + 1
}
}
}

moduleLoop@ for (module in project.modules) {
val changeListManager = ChangeListManager.getInstance(module.project)
if (module.guessModuleDir() != null) {
Expand All @@ -176,7 +188,8 @@ class CodeTestSessionConfig(
val current = stack.pop()

if (!current.isDirectory) {
if (current.isFile && !changeListManager.isIgnoredFile(current) &&
if (current.isFile && current.path != selectedFile?.path &&
!changeListManager.isIgnoredFile(current) &&
runBlocking { !featureDevSessionContext.ignoreFile(current) } &&
runReadAction { !fileIndex.isInLibrarySource(current) }
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,40 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
}

// gitignore patterns are not regex, method update needed.
private fun convertGitIgnorePatternToRegex(pattern: String): String = pattern
fun convertGitIgnorePatternToRegex(pattern: String): String = pattern
// Escape special regex characters except * and ?
Copy link
Contributor

Choose a reason for hiding this comment

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

tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will add

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests

.replace(".", "\\.")
.replace("*", ".*")
.let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching)
.replace("+", "\\+")
.replace("(", "\\(")
.replace(")", "\\)")
.replace("[", "\\[")
.replace("]", "\\]")
.replace("{", "\\{")
.replace("}", "\\}")
.replace(",", "\\,")
.replace("^", "\\^")
.replace("$", "\\$")
.replace("|", "\\|")
// Convert gitignore glob patterns to regex
.replace("**", ".*?") // Match any directory depth
.replace("*", "[^/]*?") // Match any character except path separator
.replace("?", "[^/]") // Match single character except path separator
.let { pattern ->
when {
// If pattern starts with '/', anchor it to the start of the path
pattern.startsWith("/") -> "^${pattern.substring(1)}"
// If pattern doesn't start with '/', it can match anywhere in the path
else -> "(?:^|.*/?)$pattern"
}
}
.let { pattern ->
when {
// If pattern ends with '/', it should match directories
pattern.endsWith("/") -> "$pattern.*"
// Otherwise match exactly or with a trailing slash for directories
else -> "$pattern(?:/.*)?$"
}
}

var selectedSourceFolder: VirtualFile
set(newRoot) {
Expand Down
Loading