kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/path/path_test.go (about)

     1  package path
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  )
     7  
     8  func TestSanitizePath(t *testing.T) {
     9  	tests := []struct {
    10  		name     string
    11  		input    string
    12  		expected string
    13  	}{
    14  		{
    15  			name:     "Path with null character",
    16  			input:    "test\x00file",
    17  			expected: "test_file",
    18  		},
    19  	}
    20  
    21  	if runtime.GOOS == "windows" {
    22  		tests = append(tests, struct {
    23  			name     string
    24  			input    string
    25  			expected string
    26  		}{
    27  			name:     "Windows style path",
    28  			input:    "C:\\Program Files\\Test<:>*|",
    29  			expected: "C:\\Program Files\\Test_____",
    30  		},
    31  		)
    32  	} else {
    33  		tests = append(tests, struct {
    34  			name     string
    35  			input    string
    36  			expected string
    37  		}{
    38  			name:     "Path without invalid characters",
    39  			input:    "/usr/local/bin/test",
    40  			expected: "/usr/local/bin/test",
    41  		})
    42  	}
    43  
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			output := SanitizePath(tt.input)
    47  			if output != tt.expected {
    48  				t.Errorf("expected %s, got %s", tt.expected, output)
    49  			}
    50  		})
    51  	}
    52  }