github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/internal/parent_dir_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  	"fmt"
    19  	"path/filepath"
    20  	"testing"
    21  )
    22  
    23  func TestParentDir(t *testing.T) {
    24  	tests := []struct {
    25  		path string
    26  		n    int
    27  		want string
    28  	}{
    29  		{path: ".", n: 3, want: "."},
    30  		{path: "a/b/c/d", n: 3, want: "a/b/c"},
    31  		{path: "a/b/c/d", n: 1, want: "a"},
    32  		{path: "a/b", n: 3, want: "a/b"},
    33  		{path: "a", n: 3, want: "a"},
    34  		{path: "asdf/yolo/foo/test/bla", n: 3, want: "asdf/yolo/foo"},
    35  	}
    36  
    37  	for _, test := range tests {
    38  		t.Run(fmt.Sprintf("ParentDir(%q, %d), want %q", test.path, test.n, test.want), func(t *testing.T) {
    39  			got := ParentDir(filepath.FromSlash(test.path), test.n)
    40  			if filepath.ToSlash(got) != test.want {
    41  				t.Errorf("ParentDir(%q, %d) = %q, want %q", test.path, test.n, got, test.want)
    42  			}
    43  		})
    44  	}
    45  }