github.com/pkg/sftp@v1.13.6/server_nowindows_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package sftp
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func TestServer_toLocalPath(t *testing.T) {
    11  	tests := []struct {
    12  		name        string
    13  		withWorkDir string
    14  		p           string
    15  		want        string
    16  	}{
    17  		{
    18  			name: "empty path with no workdir",
    19  			p:    "",
    20  			want: "",
    21  		},
    22  		{
    23  			name: "relative path with no workdir",
    24  			p:    "file",
    25  			want: "file",
    26  		},
    27  		{
    28  			name: "absolute path with no workdir",
    29  			p:    "/file",
    30  			want: "/file",
    31  		},
    32  		{
    33  			name:        "workdir and empty path",
    34  			withWorkDir: "/home/user",
    35  			p:           "",
    36  			want:        "/home/user",
    37  		},
    38  		{
    39  			name:        "workdir and relative path",
    40  			withWorkDir: "/home/user",
    41  			p:           "file",
    42  			want:        "/home/user/file",
    43  		},
    44  		{
    45  			name:        "workdir and relative path with .",
    46  			withWorkDir: "/home/user",
    47  			p:           ".",
    48  			want:        "/home/user",
    49  		},
    50  		{
    51  			name:        "workdir and relative path with . and file",
    52  			withWorkDir: "/home/user",
    53  			p:           "./file",
    54  			want:        "/home/user/file",
    55  		},
    56  		{
    57  			name:        "workdir and absolute path",
    58  			withWorkDir: "/home/user",
    59  			p:           "/file",
    60  			want:        "/file",
    61  		},
    62  		{
    63  			name:        "workdir and non-unixy path prefixes workdir",
    64  			withWorkDir: "/home/user",
    65  			p:           "C:\\file",
    66  			// This may look like a bug but it is the result of passing
    67  			// invalid input (a non-unixy path) to the server.
    68  			want: "/home/user/C:\\file",
    69  		},
    70  	}
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			// We don't need to initialize the Server further to test
    74  			// toLocalPath behavior.
    75  			s := &Server{}
    76  			if tt.withWorkDir != "" {
    77  				if err := WithServerWorkingDirectory(tt.withWorkDir)(s); err != nil {
    78  					t.Fatal(err)
    79  				}
    80  			}
    81  
    82  			if got := s.toLocalPath(tt.p); got != tt.want {
    83  				t.Errorf("Server.toLocalPath() = %q, want %q", got, tt.want)
    84  			}
    85  		})
    86  	}
    87  }