github.com/google/osv-scalibr@v0.4.1/binary/scalibr/scalibr_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"path/filepath"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  func TestRun(t *testing.T) {
    24  	tempDir := func(t *testing.T) string {
    25  		t.Helper()
    26  		return t.TempDir()
    27  	}
    28  
    29  	testCases := []struct {
    30  		desc      string
    31  		setupFunc func(t *testing.T) string
    32  		args      []string
    33  		want      int
    34  	}{
    35  		{
    36  			desc:      "scan subcommand",
    37  			setupFunc: tempDir,
    38  			args:      []string{"scalibr", "scan", "--root", "{dir}", "--result", filepath.Join("{dir}", "result.textproto")},
    39  			want:      0,
    40  		},
    41  		{
    42  			desc:      "no subcommand",
    43  			setupFunc: tempDir,
    44  			args:      []string{"scalibr", "--root", "{dir}", "--result", filepath.Join("{dir}", "result.textproto")},
    45  			want:      0,
    46  		},
    47  		{
    48  			desc:      "extract with supported cdx-component-type",
    49  			setupFunc: tempDir,
    50  			args:      []string{"scalibr", "--root", "{dir}", "--o", "cdx-json=" + filepath.Join("{dir}", "bom.cdx.json"), "--extractors", "dotnet/depsjson", "--cdx-component-type", "library"},
    51  			want:      0,
    52  		},
    53  		{
    54  			desc:      "scan subcommand with arg before flags",
    55  			setupFunc: tempDir,
    56  			args:      []string{"scalibr", "scan", "unknown", "--root", "{dir}", "--result", filepath.Join("{dir}", "result.textproto")},
    57  			want:      1, // "Error parsing CLI args: either --result or --o needs to be set"
    58  		},
    59  		{
    60  			desc:      "unknown subcommand",
    61  			setupFunc: tempDir,
    62  			// 'unknown' should be treated as the first argument to 'scan', which should fail as it's equivalent to the above test.
    63  			args: []string{"scalibr", "unknown", "--root", "{dir}", "--result", filepath.Join("{dir}", "result.textproto")},
    64  			want: 1,
    65  		},
    66  		{
    67  			desc:      "extract with unknown cdx-component-type",
    68  			setupFunc: tempDir,
    69  			args:      []string{"scalibr", "--root", "{dir}", "--o", "cdx-json=" + filepath.Join("{dir}", "bom.cdx.json"), "--extractors", "dotnet/depsjson", "--cdx-component-type", "anything"},
    70  			want:      1,
    71  		},
    72  	}
    73  
    74  	for _, tc := range testCases {
    75  		t.Run(tc.desc, func(t *testing.T) {
    76  			dir := tc.setupFunc(t)
    77  			args := make([]string, len(tc.args))
    78  			for i, arg := range tc.args {
    79  				args[i] = strings.ReplaceAll(arg, "{dir}", dir)
    80  			}
    81  			if got := run(args); got != tc.want {
    82  				t.Errorf("run(%v) returned unexpected exit code, got %d want %d", args, got, tc.want)
    83  			}
    84  		})
    85  	}
    86  }