From 1463df12e3c4d18bb147e65266772b714a7a0786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Garc=C3=ADa=20Veytia=20=28Puerco=29?= Date: Tue, 3 Dec 2024 21:45:04 -0600 Subject: [PATCH] Unit tests for deps DS utility funcs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adolfo GarcĂ­a Veytia (Puerco) --- internal/datasources/deps/handler_test.go | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 internal/datasources/deps/handler_test.go diff --git a/internal/datasources/deps/handler_test.go b/internal/datasources/deps/handler_test.go new file mode 100644 index 0000000000..2161a22ca7 --- /dev/null +++ b/internal/datasources/deps/handler_test.go @@ -0,0 +1,68 @@ +// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors +// SPDX-License-Identifier: Apache-2.0 + +// Package deps implements a data source that extracts dependencies from +// a filesystem or file. +package deps + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestValidateArgs(t *testing.T) { + h := depsDataSourceHandler{} + t.Parallel() + for _, tc := range []struct { + name string + args any + mustErr bool + }{ + {name: "no-args", args: nil, mustErr: false}, + {name: "wrong-type", args: struct{}{}, mustErr: true}, + {name: "no-path", args: map[string]any{"ecosystems": []string{"npm"}}, mustErr: false}, + {name: "blank-path", args: map[string]any{"path": "", "ecosystems": []string{"npm"}}, mustErr: false}, + {name: "path-set", args: map[string]any{"path": "directory/", "ecosystems": []string{"npm"}}, mustErr: false}, + {name: "no-ecosystems", args: map[string]any{"path": "directory/"}, mustErr: false}, + {name: "ecosystems-empty", args: map[string]any{"path": "directory/", "ecosystems": []string{}}, mustErr: false}, + {name: "ecosystems-nil", args: map[string]any{"path": "directory/", "ecosystems": nil}, mustErr: false}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + res := h.ValidateArgs(tc.args) + if tc.mustErr { + require.Error(t, res) + return + } + require.NoError(t, res) + }) + } +} + +func TestValidateEcosystems(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name string + list any + mustErr bool + }{ + {"empty-list", nil, false}, + {"valid-list-0", []string{}, false}, + {"valid-list-1", []string{"npm"}, false}, + {"valid-list-1+", []string{"npm", "pypi", "cargo"}, false}, + {"invalid-type", []string{"npm", "Hello!", "cargo"}, true}, + {"other-something", []struct{}{}, true}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + errs := validateEcosystems(tc.list) + if tc.mustErr { + require.Error(t, errors.Join(errs...)) + return + } + require.Len(t, errs, 0) + }) + } +}