tailscale/client/freedesktop/freedesktop_test.go
Brad Fitzpatrick 5ef3713c9f cmd/vet: add subtestnames analyzer; fix all existing violations
Add a new vet analyzer that checks t.Run subtest names don't contain
characters requiring quoting when re-running via "go test -run". This
enforces the style guide rule: don't use spaces or punctuation in
subtest names.

The analyzer flags:
- Direct t.Run calls with string literal names containing spaces,
  regex metacharacters, quotes, or other problematic characters
- Table-driven t.Run(tt.name, ...) calls where tt ranges over a
  slice/map literal with bad name field values

Also fix all 978 existing violations across 81 test files, replacing
spaces with hyphens and shortening long sentence-like names to concise
hyphenated forms.

Updates #19242

Change-Id: Ib0ad96a111bd8e764582d1d4902fe2599454ab65
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2026-04-05 15:52:51 -07:00

146 lines
2.1 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package freedesktop
import (
"strings"
"testing"
)
func TestEscape(t *testing.T) {
tests := []struct {
name, input, want string
}{
{
name: "no-illegal-chars",
input: "/home/user",
want: "/home/user",
},
{
name: "empty-string",
input: "",
want: "\"\"",
},
{
name: "space",
input: " ",
want: "\" \"",
},
{
name: "tab",
input: "\t",
want: "\"\t\"",
},
{
name: "newline",
input: "\n",
want: "\"\n\"",
},
{
name: "double-quote",
input: "\"",
want: "\"\\\"\"",
},
{
name: "single-quote",
input: "'",
want: "\"'\"",
},
{
name: "backslash",
input: "\\",
want: "\"\\\\\"",
},
{
name: "greater-than",
input: ">",
want: "\">\"",
},
{
name: "less-than",
input: "<",
want: "\"<\"",
},
{
name: "tilde",
input: "~",
want: "\"~\"",
},
{
name: "pipe",
input: "|",
want: "\"|\"",
},
{
name: "ampersand",
input: "&",
want: "\"&\"",
},
{
name: "semicolon",
input: ";",
want: "\";\"",
},
{
name: "dollar",
input: "$",
want: "\"\\$\"",
},
{
name: "asterisk",
input: "*",
want: "\"*\"",
},
{
name: "question-mark",
input: "?",
want: "\"?\"",
},
{
name: "hash",
input: "#",
want: "\"#\"",
},
{
name: "open-paren",
input: "(",
want: "\"(\"",
},
{
name: "close-paren",
input: ")",
want: "\")\"",
},
{
name: "backtick",
input: "`",
want: "\"\\`\"",
},
{
name: "char-without-escape",
input: "/home/user\t",
want: "\"/home/user\t\"",
},
{
name: "char-with-escape",
input: "/home/user\\",
want: "\"/home/user\\\\\"",
},
{
name: "all-illegal-chars",
input: "/home/user" + needsEscape,
want: "\"/home/user \t\n\\\"'\\\\><~|&;\\$*?#()\\`\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Quote(tt.input)
if strings.Compare(got, tt.want) != 0 {
t.Errorf("expected %s, got %s", tt.want, got)
}
})
}
}