From 155889a479df69df626e4e4002195659317b7e3f Mon Sep 17 00:00:00 2001 From: VaibhavMalik4187 Date: Fri, 1 Dec 2023 17:31:06 +0530 Subject: [PATCH] Added missing tests for locationresolver Wrote new tests to cover the unhandled statements in the `NewFixPathLocationResolver` and `ResolveLocation` functions. Signed-off-by: VaibhavMalik4187 --- core/cautils/fileutils_test.go | 2 +- .../locationresolver/locationresolver_test.go | 20 +++++++++++++++++++ examples/online-boutique/invalid.yaml | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 examples/online-boutique/invalid.yaml diff --git a/core/cautils/fileutils_test.go b/core/cautils/fileutils_test.go index b132f75a..ee08e2aa 100644 --- a/core/cautils/fileutils_test.go +++ b/core/cautils/fileutils_test.go @@ -27,7 +27,7 @@ func TestListFiles(t *testing.T) { files, errs := listFiles(filesPath) assert.Equal(t, 0, len(errs)) - assert.Equal(t, 12, len(files)) + assert.Equal(t, 13, len(files)) } func TestLoadResourcesFromFiles(t *testing.T) { diff --git a/core/pkg/resultshandling/locationresolver/locationresolver_test.go b/core/pkg/resultshandling/locationresolver/locationresolver_test.go index 2a3551e5..f1e7c2bb 100644 --- a/core/pkg/resultshandling/locationresolver/locationresolver_test.go +++ b/core/pkg/resultshandling/locationresolver/locationresolver_test.go @@ -37,6 +37,12 @@ func TestResolveLocation(t *testing.T) { resolver, _ := NewFixPathLocationResolver(yamlFilePath) + for fixPath, _ := range fixPathToExpectedLineAndColumn { + location, err := resolver.ResolveLocation(fixPath, 100000) + assert.Contains(t, err.Error(), "node index [100000] out of range ") + assert.Empty(t, location) + } + for fixPath, expected := range fixPathToExpectedLineAndColumn { location, err := resolver.ResolveLocation(fixPath, 0) assert.NoError(t, err) @@ -62,3 +68,17 @@ func TestResolveLocation(t *testing.T) { assert.ErrorContains(t, err, "invalid input") } + +func TestFixPathLocationResolver_NonExistentYaml(t *testing.T) { + yamlFilePath := filepath.Join(onlineBoutiquePath(), "adservice_invalid.yaml") + resolver, err := NewFixPathLocationResolver(yamlFilePath) + assert.Nil(t, resolver) + assert.NotNil(t, err) +} + +func TestFixPathLocationResolver_InvalidYaml(t *testing.T) { + yamlFilePath := filepath.Join(onlineBoutiquePath(), "invalid.yaml") + resolver, err := NewFixPathLocationResolver(yamlFilePath) + assert.Nil(t, resolver) + assert.NotNil(t, err) +} diff --git a/examples/online-boutique/invalid.yaml b/examples/online-boutique/invalid.yaml new file mode 100644 index 00000000..b516b2c4 --- /dev/null +++ b/examples/online-boutique/invalid.yaml @@ -0,0 +1 @@ +@ \ No newline at end of file