Skip to content

Commit f817ce8

Browse files
committed
test: add PathValid unit test
1 parent 3bfd0aa commit f817ce8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

internal/os/filesystem/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func pathValid(path string) (bool, error) {
4444
return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
4545
}
4646

47+
// PathValid determines whether all elements of a path exist
48+
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-7
49+
// for a remote path, determines whether connection is ok
50+
// e.g. in a SMB server connection, if password is changed, connection will be lost, this func will return false
4751
func (APIImplementor) PathValid(path string) (bool, error) {
4852
return pathValid(path)
4953
}

internal/os/filesystem/api_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build windows
2+
3+
package filesystem
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestPathValid(t *testing.T) {
12+
tests := []struct {
13+
remotepath string
14+
expectedResult bool
15+
expectError bool
16+
}{
17+
{
18+
"c:",
19+
true,
20+
false,
21+
},
22+
{
23+
"invalid-path",
24+
false,
25+
false,
26+
},
27+
}
28+
29+
for _, test := range tests {
30+
result, err := pathValid(test.remotepath)
31+
assert.Equal(t, result, test.expectedResult, "Expect result not equal with pathValid(%s) return: %q, expected: %q, error: %v",
32+
test.remotepath, result, test.expectedResult, err)
33+
if test.expectError {
34+
assert.NotNil(t, err, "Expect error during pathValid(%s)", test.remotepath)
35+
} else {
36+
assert.Nil(t, err, "Expect error is nil during pathValid(%s)", test.remotepath)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)