github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/shellquote_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package util 7 8 import "testing" 9 10 func TestShellEscape(t *testing.T) { 11 tests := []struct { 12 name string 13 toEscape string 14 want string 15 }{ 16 { 17 "Simplest case - nothing to escape", 18 "a/b/c/d", 19 "a/b/c/d", 20 }, { 21 "Prefixed tilde - with normal stuff - should not escape", 22 "~/src/go/gitea/gitea", 23 "~/src/go/gitea/gitea", 24 }, { 25 "Typical windows path with spaces - should get doublequote escaped", 26 `C:\Program Files\GitBundle v1.13 - I like lots of spaces\gitea`, 27 `"C:\\Program Files\\GitBundle v1.13 - I like lots of spaces\\gitea"`, 28 }, { 29 "Forward-slashed windows path with spaces - should get doublequote escaped", 30 "C:/Program Files/GitBundle v1.13 - I like lots of spaces/gitea", 31 `"C:/Program Files/GitBundle v1.13 - I like lots of spaces/gitea"`, 32 }, { 33 "Prefixed tilde - but then a space filled path", 34 "~git/GitBundle v1.13/gitea", 35 `~git/"GitBundle v1.13/gitea"`, 36 }, { 37 "Bangs are unfortunately not predictable so need to be singlequoted", 38 "C:/Program Files/GitBundle!/gitea", 39 `'C:/Program Files/GitBundle!/gitea'`, 40 }, { 41 "Newlines are just irritating", 42 "/home/git/GitBundle\n\nWHY-WOULD-YOU-DO-THIS\n\nGitea/gitea", 43 "'/home/git/GitBundle\n\nWHY-WOULD-YOU-DO-THIS\n\nGitea/gitea'", 44 }, { 45 "Similarly we should nicely handle multiple single quotes if we have to single-quote", 46 "'!''!'''!''!'!'", 47 `\''!'\'\''!'\'\'\''!'\'\''!'\''!'\'`, 48 }, { 49 "Double quote < ...", 50 "~/<gitea", 51 "~/\"<gitea\"", 52 }, { 53 "Double quote > ...", 54 "~/gitea>", 55 "~/\"gitea>\"", 56 }, { 57 "Double quote and escape $ ...", 58 "~/$gitea", 59 "~/\"\\$gitea\"", 60 }, { 61 "Double quote {...", 62 "~/{gitea", 63 "~/\"{gitea\"", 64 }, { 65 "Double quote }...", 66 "~/gitea}", 67 "~/\"gitea}\"", 68 }, { 69 "Double quote ()...", 70 "~/(gitea)", 71 "~/\"(gitea)\"", 72 }, { 73 "Double quote and escape `...", 74 "~/gitea`", 75 "~/\"gitea\\`\"", 76 }, { 77 "Double quotes can handle a number of things without having to escape them but not everything ...", 78 "~/<gitea> ${gitea} `gitea` [gitea] (gitea) \"gitea\" \\gitea\\ 'gitea'", 79 "~/\"<gitea> \\${gitea} \\`gitea\\` [gitea] (gitea) \\\"gitea\\\" \\\\gitea\\\\ 'gitea'\"", 80 }, { 81 "Single quotes don't need to escape except for '...", 82 "~/<gitea> ${gitea} `gitea` (gitea) !gitea! \"gitea\" \\gitea\\ 'gitea'", 83 "~/'<gitea> ${gitea} `gitea` (gitea) !gitea! \"gitea\" \\gitea\\ '\\''gitea'\\'", 84 }, 85 } 86 for _, tt := range tests { 87 t.Run(tt.name, func(t *testing.T) { 88 if got := ShellEscape(tt.toEscape); got != tt.want { 89 t.Errorf("ShellEscape(%q):\nGot: %s\nWanted: %s", tt.toEscape, got, tt.want) 90 } 91 }) 92 } 93 }