| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | |
| 9 | "gotest.tools/v3/assert" |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | func TestPathDepth(t *testing.T) { |
| 19 | tests := []struct { |
| 20 | path string |
| 21 | windows bool |
| 22 | expected int |
| 23 | }{ |
| 24 | |
| 25 | {"/Users/jan", false, 3}, |
| 26 | {`C:\Users\jan`, true, 3}, |
| 27 | {"/Users/jan/proj", false, 4}, |
| 28 | {`C:\Users\jan\proj`, true, 4}, |
| 29 | {"/", false, 2}, |
| 30 | {`C:\`, true, 2}, |
| 31 | |
| 32 | |
| 33 | {"C:/Users/jan/proj", true, 4}, |
| 34 | |
| 35 | |
| 36 | |
| 37 | {`/x\y\z`, false, 2}, |
| 38 | {`/foo\..\..\..\bar`, false, 2}, |
| 39 | |
| 40 | |
| 41 | {`\\server\share`, true, 3}, |
| 42 | {`\\server\share\proj`, true, 4}, |
| 43 | |
| 44 | |
| 45 | |
| 46 | {`\\server\\share`, true, 3}, |
| 47 | {`\\\server\share`, true, 3}, |
| 48 | |
| 49 | |
| 50 | {`\\?\C:\`, true, 2}, |
| 51 | {`\\?\C:\Users\jan\proj`, true, 4}, |
| 52 | {`\\.\C:\`, true, 2}, |
| 53 | {`\\?\UNC\server\share`, true, 3}, |
| 54 | |
| 55 | |
| 56 | |
| 57 | {`\\server\share\`, true, 3}, |
| 58 | {`C:\Users\jan\proj\`, true, 4}, |
| 59 | |
| 60 | |
| 61 | {"", false, 1}, |
| 62 | } |
| 63 | for _, tt := range tests { |
| 64 | t.Run(tt.path, func(t *testing.T) { |
| 65 | assert.Equal(t, pathDepth(tt.path, tt.windows), tt.expected) |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestParseRsyncStats(t *testing.T) { |
| 71 | tests := []struct { |
| 72 | name string |
| 73 | output string |
| 74 | expected *rsyncStats |
| 75 | }{ |
| 76 | { |
| 77 | name: "mixed output", |
| 78 | output: ` |
| 79 | >f+++++++++ new-file.txt |
| 80 | cd+++++++++ dir/ |
| 81 | cL+++++++++ new-symlink -> target |
| 82 | *deleting deleted.txt |
| 83 | `, |
| 84 | expected: &rsyncStats{ |
| 85 | Added: 3, |
| 86 | Deleted: 1, |
| 87 | Modified: 0, |
| 88 | }, |
| 89 | }, |
| 90 | { |
| 91 | name: "metadata-only changes", |
| 92 | output: ` |
| 93 | .d..t...... ./ |
| 94 | .f...p..... existing.txt |
| 95 | `, |
| 96 | expected: &rsyncStats{ |
| 97 | Added: 0, |
| 98 | Deleted: 0, |
| 99 | Modified: 0, |
| 100 | Metadata: 2, |
| 101 | }, |
| 102 | }, |
| 103 | { |
| 104 | name: "many changes", |
| 105 | output: ` |
| 106 | <f+++++++++ file1 |
| 107 | <f+++++++++ file2 |
| 108 | *deleting file3 |
| 109 | *deleting file4 |
| 110 | *deleting file5 |
| 111 | <f..T...... file6 |
| 112 | `, |
| 113 | expected: &rsyncStats{ |
| 114 | Added: 2, |
| 115 | Deleted: 3, |
| 116 | Modified: 1, |
| 117 | }, |
| 118 | }, |
| 119 | } |
| 120 | |
| 121 | for _, tt := range tests { |
| 122 | t.Run(tt.name, func(t *testing.T) { |
| 123 | got := parseRsyncStats(tt.output) |
| 124 | assert.DeepEqual(t, got, tt.expected) |
| 125 | }) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestRsyncStatsString(t *testing.T) { |
| 130 | tests := []struct { |
| 131 | name string |
| 132 | stats *rsyncStats |
| 133 | expected string |
| 134 | }{ |
| 135 | { |
| 136 | name: "empty", |
| 137 | stats: &rsyncStats{}, |
| 138 | expected: "", |
| 139 | }, |
| 140 | { |
| 141 | name: "all stats", |
| 142 | stats: &rsyncStats{ |
| 143 | Added: 1, |
| 144 | Deleted: 2, |
| 145 | Modified: 3, |
| 146 | Metadata: 4, |
| 147 | }, |
| 148 | expected: "added: 1, deleted: 2, modified: 3, metadata: 4", |
| 149 | }, |
| 150 | } |
| 151 | |
| 152 | for _, tt := range tests { |
| 153 | t.Run(tt.name, func(t *testing.T) { |
| 154 | assert.Equal(t, tt.stats.String(), tt.expected) |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestWindowsShellCommandFlag(t *testing.T) { |
| 160 | tests := []struct { |
| 161 | shell string |
| 162 | want string |
| 163 | }{ |
| 164 | {"cmd.exe", "/c"}, |
| 165 | {"CMD.EXE", "/c"}, |
| 166 | {`C:\Windows\System32\cmd.exe`, "/c"}, |
| 167 | {`C:/Windows/System32/cmd.exe`, "/c"}, |
| 168 | {"powershell.exe", "-Command"}, |
| 169 | {"pwsh.exe", "-Command"}, |
| 170 | {`C:\Program Files\PowerShell\7\pwsh.exe`, "-Command"}, |
| 171 | } |
| 172 | for _, tt := range tests { |
| 173 | t.Run(tt.shell, func(t *testing.T) { |
| 174 | assert.Equal(t, tt.want, windowsShellCommandFlag(tt.shell)) |
| 175 | }) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestWindowsQuoteShell(t *testing.T) { |
| 180 | tests := []struct { |
| 181 | shell string |
| 182 | want string |
| 183 | }{ |
| 184 | {"cmd.exe", "cmd.exe"}, |
| 185 | {`C:\Windows\System32\cmd.exe`, `C:\Windows\System32\cmd.exe`}, |
| 186 | {`C:\Program Files\PowerShell\7\pwsh.exe`, `"C:\Program Files\PowerShell\7\pwsh.exe"`}, |
| 187 | {`"C:\already\quoted.exe"`, `"C:\already\quoted.exe"`}, |
| 188 | } |
| 189 | for _, tt := range tests { |
| 190 | t.Run(tt.shell, func(t *testing.T) { |
| 191 | assert.Equal(t, tt.want, windowsQuoteShell(tt.shell)) |
| 192 | }) |
| 193 | } |
| 194 | } |