From 6a4f308e5e73cfe3df740839be11a3fb000a5003 Mon Sep 17 00:00:00 2001 From: David Simansky Date: Tue, 16 Mar 2021 01:09:41 +0100 Subject: [PATCH] Fix linting error SA5011 (#1265) --- pkg/kn/plugin/manager_test.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/kn/plugin/manager_test.go b/pkg/kn/plugin/manager_test.go index dc4e99328..576146313 100644 --- a/pkg/kn/plugin/manager_test.go +++ b/pkg/kn/plugin/manager_test.go @@ -280,20 +280,25 @@ func TestPluginList(t *testing.T) { pluginList, err := ctx.pluginManager.ListPlugins() assert.NilError(t, err) assert.Assert(t, pluginList != nil) - assert.Equal(t, len(pluginList), 2, "both plugins found (in dir + in path)") - assert.Equal(t, pluginList[0].Name(), "kn-aa-path-test", "first plugin is alphabetically smallest (list is sorted)") - assert.DeepEqual(t, pluginList[0].CommandParts(), []string{"aa", "path", "test"}) - assert.Equal(t, pluginList[1].Name(), "kn-zz-test_in_dir", "second plugin is alphabetically greater (list is sorted)") - assert.DeepEqual(t, pluginList[1].CommandParts(), []string{"zz", "test-in-dir"}) - + // The condition workarounds false-positive SA5011: Possible nil pointer dereference + if pluginList != nil { + assert.Equal(t, len(pluginList), 2, "both plugins found (in dir + in path)") + assert.Equal(t, pluginList[0].Name(), "kn-aa-path-test", "first plugin is alphabetically smallest (list is sorted)") + assert.DeepEqual(t, pluginList[0].CommandParts(), []string{"aa", "path", "test"}) + assert.Equal(t, pluginList[1].Name(), "kn-zz-test_in_dir", "second plugin is alphabetically greater (list is sorted)") + assert.DeepEqual(t, pluginList[1].CommandParts(), []string{"zz", "test-in-dir"}) + } // Disable lookup --> Only one plugin found ctx.pluginManager.lookupInPath = false pluginList, err = ctx.pluginManager.ListPlugins() assert.NilError(t, err) assert.Assert(t, pluginList != nil) - assert.Equal(t, len(pluginList), 1, "1 plugin found (in dir)") - assert.Equal(t, pluginList[0].Name(), "kn-zz-test_in_dir", "second plugin is alphabetically greater (list is sorted)") - assert.DeepEqual(t, pluginList[0].CommandParts(), []string{"zz", "test-in-dir"}) + // The condition workarounds false-positive SA5011: Possible nil pointer dereference + if pluginList != nil { + assert.Equal(t, len(pluginList), 1, "1 plugin found (in dir)") + assert.Equal(t, pluginList[0].Name(), "kn-zz-test_in_dir", "second plugin is alphabetically greater (list is sorted)") + assert.DeepEqual(t, pluginList[0].CommandParts(), []string{"zz", "test-in-dir"}) + } } // ====================================================================