go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/path/filepath/ancestors_test.go (about)

     1  // Copyright (c) 2024 Matt Way
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to
     5  // deal in the Software without restriction, including without limitation the
     6  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     7  // sell copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    18  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    19  // IN THE THE SOFTWARE.
    20  
    21  package filepath_test
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	"go.mway.dev/x/path/filepath"
    28  )
    29  
    30  func TestAncestors(t *testing.T) {
    31  	cases := map[string]struct {
    32  		givePath  string
    33  		wantPaths []string
    34  	}{
    35  		"empty": {
    36  			givePath:  "",
    37  			wantPaths: nil,
    38  		},
    39  		"dot": {
    40  			givePath:  ".",
    41  			wantPaths: nil,
    42  		},
    43  		"dotdot": {
    44  			givePath:  "..",
    45  			wantPaths: nil,
    46  		},
    47  		"root": {
    48  			givePath:  "/",
    49  			wantPaths: nil,
    50  		},
    51  		"single": {
    52  			givePath:  "foo",
    53  			wantPaths: nil,
    54  		},
    55  		"absolute": {
    56  			givePath: "/foo/bar/baz/bat",
    57  			wantPaths: []string{
    58  				"/foo/bar/baz",
    59  				"/foo/bar",
    60  				"/foo",
    61  			},
    62  		},
    63  		"relative": {
    64  			givePath: "foo/bar/baz/bat",
    65  			wantPaths: []string{
    66  				"foo/bar/baz",
    67  				"foo/bar",
    68  				"foo",
    69  			},
    70  		},
    71  		"unresolved absolute": {
    72  			givePath: "/foo/bar/../baz/bat",
    73  			wantPaths: []string{
    74  				"/foo/baz",
    75  				"/foo",
    76  			},
    77  		},
    78  		"unresolved relative": {
    79  			givePath: "foo/bar/../baz/bat",
    80  			wantPaths: []string{
    81  				"foo/baz",
    82  				"foo",
    83  			},
    84  		},
    85  	}
    86  
    87  	for name, tt := range cases {
    88  		t.Run(name, func(t *testing.T) {
    89  			require.Equal(t, tt.wantPaths, filepath.Ancestors(tt.givePath))
    90  		})
    91  	}
    92  }