github.com/chyroc/anb@v0.3.0/internal/helper_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestSplitShellCommand(t *testing.T) {
    10  	tests := []struct {
    11  		name string
    12  		args string
    13  		want []string
    14  	}{
    15  		{name: "1", args: `a b c`, want: []string{"a", "b", "c"}},
    16  		{name: "2", args: `a b "c d e"`, want: []string{"a", "b", "c d e"}},
    17  		{name: "3", args: `a b " c d e "`, want: []string{"a", "b", " c d e "}},
    18  		{name: "4", args: `  a   b   c `, want: []string{"a", "b", "c"}},
    19  		{name: "5", args: `a b 'c'`, want: []string{"a", "b", "c"}},
    20  		{name: "6", args: `a "b" 'c'`, want: []string{"a", "b", "c"}},
    21  		{name: "7", args: `a "b " 'c '`, want: []string{"a", "b ", "c "}},
    22  		{name: "8", args: `\`, want: []string{`\`}},
    23  		{name: "9", args: "\\", want: []string{`\`}},
    24  		{name: "10", args: `\ ""`, want: []string{`\`, ""}},
    25  		{name: "11", args: `\ "" "c "`, want: []string{`\`, "", "c "}},
    26  		{name: "12", args: `\ "" "c\ "`, want: []string{`\`, "", "c\\ "}},
    27  		{name: "12", args: "'a' 'b c' '\"'", want: []string{`a`, `b c`, `"`}},
    28  	}
    29  	for _, tt := range tests {
    30  		t.Run(tt.name, func(t *testing.T) {
    31  			assert.Equal(t, tt.want, SplitShellCommand(tt.args), tt.name+" / "+tt.args)
    32  		})
    33  	}
    34  }
    35  
    36  func TestGetRemoteRevPath(t *testing.T) {
    37  	type args struct {
    38  		localRootPath     string
    39  		remoteRootPath    string
    40  		localPath         string
    41  		keepRemoteBaseDir bool
    42  	}
    43  	tests := []struct {
    44  		name       string
    45  		args       args
    46  		want       string
    47  		errContain string
    48  	}{
    49  		{
    50  			name: "1",
    51  			args: args{localRootPath: "./", remoteRootPath: "/root", localPath: "a.txt", keepRemoteBaseDir: false},
    52  			want: "/root/a.txt",
    53  		},
    54  		{
    55  			name: "2",
    56  			args: args{localRootPath: ".", remoteRootPath: "/root", localPath: "a.txt", keepRemoteBaseDir: false},
    57  			want: "/root/a.txt",
    58  		},
    59  		{
    60  			name: "3",
    61  			args: args{localRootPath: "./dir", remoteRootPath: "/root", localPath: "dir/a.txt", keepRemoteBaseDir: false},
    62  			want: "/root/a.txt",
    63  		},
    64  
    65  		{
    66  			name: "3",
    67  			args: args{localRootPath: "./dir", remoteRootPath: "/root", localPath: "dir/root/a.txt", keepRemoteBaseDir: true},
    68  			want: "/root/a.txt",
    69  		},
    70  	}
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			t.Run(tt.name, func(t *testing.T) {
    74  				assert.Equal(t, tt.want, GetRemoteRevPath(tt.args.localRootPath, tt.args.remoteRootPath, tt.args.localPath, tt.args.keepRemoteBaseDir), tt.name+" / "+tt.args.localRootPath)
    75  			})
    76  		})
    77  	}
    78  }