-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
* fix command line arguments broken on launchd after #420 * fix test on mock handler * addressing code review comments
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package schedule | ||
|
||
import "strings" | ||
|
||
type CommandArguments struct { | ||
args []string | ||
} | ||
|
||
func NewCommandArguments(args []string) CommandArguments { | ||
return CommandArguments{ | ||
args: args, | ||
} | ||
} | ||
|
||
func (ca CommandArguments) RawArgs() []string { | ||
result := make([]string, len(ca.args)) | ||
copy(result, ca.args) | ||
return result | ||
} | ||
|
||
// String returns the arguments as a string, with quotes around arguments that contain spaces | ||
func (ca CommandArguments) String() string { | ||
if len(ca.args) == 0 { | ||
return "" | ||
} | ||
|
||
var n int | ||
for _, elem := range ca.args { | ||
n += len(elem) + 3 // add 2 if quotes are needed, plus 1 for the space | ||
} | ||
|
||
b := new(strings.Builder) | ||
b.Grow(n) | ||
ca.writeString(b, ca.args[0]) | ||
for _, s := range ca.args[1:] { | ||
b.WriteString(" ") | ||
ca.writeString(b, s) | ||
} | ||
return b.String() | ||
} | ||
|
||
func (ca CommandArguments) writeString(b *strings.Builder, str string) { | ||
if strings.Contains(str, " ") { | ||
b.WriteString(`"`) | ||
b.WriteString(str) | ||
b.WriteString(`"`) | ||
} else { | ||
b.WriteString(str) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package schedule | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRawArgs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
}{ | ||
{"empty args", []string{}}, | ||
{"simple args", []string{"arg1", "arg2"}}, | ||
{"args with spaces", []string{"C:\\Program Files\\app.exe", "--config", "C:\\My Documents\\config.toml"}}, | ||
{"args with special chars", []string{"--name", "my-task!", "--config=test.conf"}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ca := NewCommandArguments(tt.args) | ||
rawArgs := ca.RawArgs() | ||
if len(rawArgs) != len(tt.args) { | ||
t.Errorf("expected %d raw arguments, got %d", len(tt.args), len(rawArgs)) | ||
} | ||
for i, arg := range tt.args { | ||
if rawArgs[i] != arg { | ||
t.Errorf("expected raw argument %d to be %s, got %s", i, arg, rawArgs[i]) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestString(t *testing.T) { | ||
tests := []struct { | ||
args []string | ||
expected string | ||
}{ | ||
{[]string{}, ""}, | ||
{[]string{"arg1"}, "arg1"}, | ||
{[]string{"arg1 with space"}, `"arg1 with space"`}, | ||
{[]string{"arg1", "arg2"}, "arg1 arg2"}, | ||
{[]string{"arg1", "arg with spaces"}, `arg1 "arg with spaces"`}, | ||
{[]string{"arg1", "arg with spaces", "anotherArg"}, `arg1 "arg with spaces" anotherArg`}, | ||
{[]string{"--config", "C:\\Program Files\\config.toml"}, `--config "C:\Program Files\config.toml"`}, | ||
{[]string{"--config", "C:\\Users\\John Doe\\Documents\\config.toml", "--name", "backup task"}, | ||
`--config "C:\Users\John Doe\Documents\config.toml" --name "backup task"`}, | ||
{[]string{"--config", "C:\\My Files\\config.toml", "--no-ansi"}, | ||
`--config "C:\My Files\config.toml" --no-ansi`}, | ||
} | ||
|
||
for _, test := range tests { | ||
ca := NewCommandArguments(test.args) | ||
result := ca.String() | ||
if result != test.expected { | ||
t.Errorf("expected %s, got %s", test.expected, result) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.