github.com/kaydxh/golang@v0.0.131/go/path/filepath/path_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package filepath_test
    23  
    24  import (
    25  	"fmt"
    26  	"path/filepath"
    27  	"testing"
    28  
    29  	filepath_ "github.com/kaydxh/golang/go/path/filepath"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestGetParentRelPath(t *testing.T) {
    34  	testCases := []struct {
    35  		filePath string
    36  		expected string
    37  	}{
    38  		{
    39  			filePath: "/data/home/deploy/bin/run.exe",
    40  			expected: "bin/run.exe",
    41  		},
    42  		{
    43  			filePath: "/run.exe",
    44  			expected: "/run.exe",
    45  		},
    46  	}
    47  
    48  	for _, testCase := range testCases {
    49  		t.Run(testCase.filePath, func(t *testing.T) {
    50  			rel := filepath_.GetParentRelPath(testCase.filePath)
    51  			assert.Equal(t, testCase.expected, rel)
    52  			t.Logf("rel: %v", rel)
    53  
    54  		})
    55  	}
    56  }
    57  
    58  func TestAbsPath(t *testing.T) {
    59  	testCases := []struct {
    60  		filePath string
    61  		expected string
    62  	}{
    63  		{
    64  			filePath: "",
    65  			expected: "",
    66  		},
    67  		{
    68  			filePath: ".",
    69  			expected: "",
    70  		},
    71  	}
    72  
    73  	for _, testCase := range testCases {
    74  		t.Run(testCase.filePath, func(t *testing.T) {
    75  			abs, err := filepath.Abs(testCase.filePath)
    76  			if err != nil {
    77  				t.Fatalf("abs err: %v", err)
    78  			}
    79  			t.Logf("abs: %v", abs)
    80  
    81  		})
    82  	}
    83  }
    84  
    85  func TestJoinPaths(t *testing.T) {
    86  	testCases := []struct {
    87  		absolutePath string
    88  		relativePath string
    89  		expected     string
    90  	}{
    91  		{
    92  			absolutePath: "/data",
    93  			relativePath: "./relative",
    94  			expected:     "",
    95  		},
    96  		{
    97  			absolutePath: "./data",
    98  			relativePath: "./relative",
    99  			expected:     "",
   100  		},
   101  		{
   102  			absolutePath: "./data",
   103  			relativePath: "./relative/",
   104  			expected:     "",
   105  		},
   106  	}
   107  
   108  	for i, testCase := range testCases {
   109  		t.Run(fmt.Sprintf("test-case-%d", i), func(t *testing.T) {
   110  			fullPath, err := filepath_.JoinPaths(testCase.absolutePath, testCase.relativePath)
   111  			if err != nil {
   112  				t.Fatalf("joinPaths err: %v", err)
   113  			}
   114  			t.Logf("fullPath : %v", fullPath)
   115  		})
   116  	}
   117  }