Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb5438a

Browse files
committed
Add tests for sshConfigExecEscape
1 parent6f07ec2 commitdb5438a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎cli/configssh_internal_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
funcTest_sshConfigExecEscape(t*testing.T) {
14+
t.Parallel()
15+
16+
tests:= []struct {
17+
namestring
18+
pathstring
19+
wantErrbool
20+
}{
21+
{"no spaces","simple",false},
22+
{"spaces","path with spaces",false},
23+
{"quotes","path with\"quotes\"",false},
24+
{"backslashes","path with\\backslashes",false},
25+
{"tabs","path with\ttabs",false},
26+
{"newline fails","path with\nnewline",true},
27+
}
28+
for_,tt:=rangetests {
29+
tt:=tt
30+
t.Run(tt.name,func(t*testing.T) {
31+
t.Parallel()
32+
33+
dir:=filepath.Join(t.TempDir(),tt.path)
34+
err:=os.MkdirAll(dir,0o755)
35+
require.NoError(t,err)
36+
bin:=filepath.Join(dir,"coder")
37+
err=os.WriteFile(bin, []byte("#!/bin/sh\necho yay\n"),0o755)//nolint:gosec
38+
require.NoError(t,err)
39+
40+
escaped,err:=sshConfigExecEscape(bin)
41+
iftt.wantErr {
42+
require.Error(t,err)
43+
return
44+
}
45+
require.NoError(t,err)
46+
47+
b,err:=exec.Command("/bin/sh","-c",escaped).Output()
48+
require.NoError(t,err)
49+
got:=strings.TrimSpace(string(b))
50+
require.Equal(t,"yay",got)
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp