github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/internal/gitignore_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 internal
    16  
    17  import (
    18  	"testing"
    19  
    20  	scalibrfs "github.com/google/osv-scalibr/fs"
    21  )
    22  
    23  func TestGitignoreMatch(t *testing.T) {
    24  	tests := []struct {
    25  		name      string
    26  		path      []string
    27  		wantMatch bool
    28  	}{
    29  		{
    30  			name:      "No_match",
    31  			path:      []string{"testdata", "path", "to", "file.py"},
    32  			wantMatch: false,
    33  		},
    34  		{
    35  			name:      "Match_specific_name",
    36  			path:      []string{"testdata", "path", "to", "ignore.txt"},
    37  			wantMatch: true,
    38  		},
    39  		{
    40  			name:      "Match_wildcard",
    41  			path:      []string{"testdata", "path", "to", "file-ignore"},
    42  			wantMatch: true,
    43  		},
    44  		{
    45  			name:      "Comments_ignored",
    46  			path:      []string{"testdata", "#file"},
    47  			wantMatch: false,
    48  		},
    49  	}
    50  
    51  	pattern, err := ParseDirForGitignore(scalibrfs.DirFS("."), "testdata")
    52  	if err != nil {
    53  		t.Fatalf("ParseDirForGitignore(testdata): %v", err)
    54  	}
    55  	patterns := []GitignorePattern{pattern}
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			got := GitignoreMatch(patterns, tt.path, false)
    59  			if got != tt.wantMatch {
    60  				t.Errorf("GitignoreMatch(%v): got %v, want %v", patterns, got, tt.wantMatch)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestParseDirForGitignoreFileDoesntExist(t *testing.T) {
    67  	_, err := ParseDirForGitignore(scalibrfs.DirFS("."), "testdata/nonexistent")
    68  	if err != nil {
    69  		t.Fatalf("ParseDirForGitignore(testdata/nonexistent): %v", err)
    70  	}
    71  }
    72  
    73  func TestFindParentGitignores(t *testing.T) {
    74  	tests := []struct {
    75  		name      string
    76  		path      []string
    77  		wantMatch bool
    78  	}{
    79  		{
    80  			name:      "No_match",
    81  			path:      []string{"testdata", "path", "to", "file.py"},
    82  			wantMatch: false,
    83  		},
    84  		{
    85  			name:      "Match_pattern_from_parent_dir",
    86  			path:      []string{"testdata", "path", "to", "ignore.txt"},
    87  			wantMatch: true,
    88  		},
    89  		{
    90  			name:      "Dont_match_pattern_from_child_dir",
    91  			path:      []string{"testdata", "subdir", "path", "to", "ignore2.txt"},
    92  			wantMatch: false,
    93  		},
    94  	}
    95  
    96  	patterns, err := ParseParentGitignores(scalibrfs.DirFS("."), "testdata/subdir")
    97  	if err != nil {
    98  		t.Fatalf("ParseParentGitignores(testdata): %v", err)
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			got := GitignoreMatch(patterns, tt.path, false)
   103  			if got != tt.wantMatch {
   104  				t.Errorf("GitignoreMatch(%v): got %v, want %v", patterns, got, tt.wantMatch)
   105  			}
   106  		})
   107  	}
   108  }