From fc1485f803e3733b1338688ae135f0db02b00ff7 Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Mon, 8 Jan 2024 14:14:14 -0500 Subject: [PATCH] :bug: use window of correct size to grab content from xml files Signed-off-by: Pranav Gaikwad --- provider/internal/builtin/service_client.go | 14 +- .../internal/builtin/service_client_test.go | 62 +++++++ provider/internal/builtin/testdata/pom.xml | 165 ++++++++++++++++++ 3 files changed, 236 insertions(+), 5 deletions(-) create mode 100644 provider/internal/builtin/service_client_test.go create mode 100644 provider/internal/builtin/testdata/pom.xml diff --git a/provider/internal/builtin/service_client.go b/provider/internal/builtin/service_client.go index d269fbf6..ba9ea90e 100644 --- a/provider/internal/builtin/service_client.go +++ b/provider/internal/builtin/service_client.go @@ -324,15 +324,19 @@ func (b *builtinServiceClient) getLocation(ctx context.Context, path, content st line := strings.Trim(part, " ") line = strings.ReplaceAll(line, "\t", "") line = regexp.QuoteMeta(line) - if line != "" { - lines = append(lines, line) - } + lines = append(lines, line) + } + // remove leading and trailing empty lines + if len(lines) > 0 && lines[0] == "" { + lines = lines[1:] + } + if len(lines) > 0 && lines[len(lines)-1] == "" { + lines = lines[:len(lines)-1] } if len(lines) < 1 { - return location, fmt.Errorf("unable to get code location, no-op content") + return location, fmt.Errorf("unable to get code location, empty content") } pattern := fmt.Sprintf(".*?%s", strings.Join(lines, ".*?")) - cacheKey := fmt.Sprintf("%s-%s", path, pattern) b.cacheMutex.RLock() val, exists := b.locationCache[cacheKey] diff --git a/provider/internal/builtin/service_client_test.go b/provider/internal/builtin/service_client_test.go new file mode 100644 index 00000000..41825980 --- /dev/null +++ b/provider/internal/builtin/service_client_test.go @@ -0,0 +1,62 @@ +package builtin + +import ( + "context" + "reflect" + "sync" + "testing" + + "github.com/go-logr/logr/testr" + "github.com/konveyor/analyzer-lsp/provider" +) + +func newLocationForLine(line float64) provider.Location { + return provider.Location{ + StartPosition: provider.Position{ + Line: line, + }, + EndPosition: provider.Position{ + Line: line, + }, + } +} + +func Test_builtinServiceClient_getLocation(t *testing.T) { + tests := []struct { + name string + path string + content string + want provider.Location + wantErr bool + }{ + { + name: "search for innerText with leading and trailing newlines", + path: "./testdata/pom.xml", + content: "\n\t\t\tch.qos.logback\n\t\t\tlogback-classic\n\t\t\t1.1.7\n\t\t", + want: newLocationForLine(117), + }, + { + name: "search for innerText with spaces in between content", + path: "./testdata/pom.xml", + content: "\n\t0.0.1-SNAPSHOT\n\n\n\t\tOrder Management\n\t\twar\n", + want: newLocationForLine(6), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &builtinServiceClient{ + log: testr.New(t), + cacheMutex: sync.RWMutex{}, + locationCache: make(map[string]float64), + } + got, err := b.getLocation(context.TODO(), tt.path, tt.content) + if (err != nil) != tt.wantErr { + t.Errorf("builtinServiceClient.getLocation() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("builtinServiceClient.getLocation() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/provider/internal/builtin/testdata/pom.xml b/provider/internal/builtin/testdata/pom.xml new file mode 100644 index 00000000..5fd20fde --- /dev/null +++ b/provider/internal/builtin/testdata/pom.xml @@ -0,0 +1,165 @@ + + 4.0.0 + io.konveyor.demo + customers-tomcat + 0.0.1-SNAPSHOT + + Order Management + war + Remaining services for the legacy Order Management application + + + 1.8 + UTF-8 + UTF-8 + ${java.version} + ${java.version} + 5.3.7 + 9.0.46 + 2021.0.1 + 5.4.32.Final + 6.2.0.Final + + 42.2.20 + 2.12.3 + + 3.8.1 + 3.3.1 + 3.2.0 + + + + + + demo-config + Azure DevOps + https://pkgs.dev.azure.com/ShawnHurley21/demo-config-utils/_packaging/demo-config/maven/v1 + + + + + + + com.fasterxml.jackson + jackson-bom + ${jackson.version} + import + pom + + + org.springframework.data + spring-data-bom + ${spring-data.version} + import + pom + + + + + + org.apache.tomcat + tomcat-servlet-api + ${tomcat.version} + provided + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-databind + + + org.springframework.data + spring-data-jpa + + + + org.springframework + spring-jdbc + ${spring-framework.version} + + + org.springframework + spring-webmvc + ${spring-framework.version} + + + org.springframework + spring-web + ${spring-framework.version} + + + org.springframework.boot + spring-boot-starter-actuator + 2.5.0 + + + org.apache.tomcat + tomcat-jdbc + ${tomcat.version} + runtime + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + org.hibernate.validator + hibernate-validator + ${hibernate-validator.version} + + + ch.qos.logback + logback-classic + 1.1.7 + + + com.oracle.database.jdbc + ojdbc8 + 21.1.0.0 + + + org.postgresql + postgresql + 42.2.23 + + + + io.konveyor.demo + config-utils + 1.0.0 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + org.apache.maven.plugins + maven-resources-plugin + ${maven-resources-plugin.version} + + UTF-8 + + + + + +