|
| 1 | +// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package deps implements a data source that extracts dependencies from |
| 5 | +// a filesystem or file. |
| 6 | +package deps |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestValidateArgs(t *testing.T) { |
| 16 | + h := depsDataSourceHandler{} |
| 17 | + t.Parallel() |
| 18 | + for _, tc := range []struct { |
| 19 | + name string |
| 20 | + args any |
| 21 | + mustErr bool |
| 22 | + }{ |
| 23 | + {name: "no-args", args: nil, mustErr: false}, |
| 24 | + {name: "wrong-type", args: struct{}{}, mustErr: true}, |
| 25 | + {name: "no-path", args: map[string]any{"ecosystems": []string{"npm"}}, mustErr: false}, |
| 26 | + {name: "blank-path", args: map[string]any{"path": "", "ecosystems": []string{"npm"}}, mustErr: false}, |
| 27 | + {name: "path-set", args: map[string]any{"path": "directory/", "ecosystems": []string{"npm"}}, mustErr: false}, |
| 28 | + {name: "no-ecosystems", args: map[string]any{"path": "directory/"}, mustErr: false}, |
| 29 | + {name: "ecosystems-empty", args: map[string]any{"path": "directory/", "ecosystems": []string{}}, mustErr: false}, |
| 30 | + {name: "ecosystems-nil", args: map[string]any{"path": "directory/", "ecosystems": nil}, mustErr: false}, |
| 31 | + } { |
| 32 | + t.Run(tc.name, func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + res := h.ValidateArgs(tc.args) |
| 35 | + if tc.mustErr { |
| 36 | + require.Error(t, res) |
| 37 | + return |
| 38 | + } |
| 39 | + require.NoError(t, res) |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestValidateEcosystems(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + for _, tc := range []struct { |
| 47 | + name string |
| 48 | + list any |
| 49 | + mustErr bool |
| 50 | + }{ |
| 51 | + {"empty-list", nil, false}, |
| 52 | + {"valid-list-0", []string{}, false}, |
| 53 | + {"valid-list-1", []string{"npm"}, false}, |
| 54 | + {"valid-list-1+", []string{"npm", "pypi", "cargo"}, false}, |
| 55 | + {"invalid-type", []string{"npm", "Hello!", "cargo"}, true}, |
| 56 | + {"other-something", []struct{}{}, true}, |
| 57 | + } { |
| 58 | + t.Run(tc.name, func(t *testing.T) { |
| 59 | + errs := validateEcosystems(tc.list) |
| 60 | + if tc.mustErr { |
| 61 | + require.Error(t, errors.Join(errs...)) |
| 62 | + return |
| 63 | + } |
| 64 | + require.Len(t, errs, 0) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments