Skip to content

Commit d198577

Browse files
committed
Add CapabilityCheck helper function
Signed-off-by: Javi Fontan <[email protected]>
1 parent a22d183 commit d198577

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

fs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,10 @@ func Capabilities(fs Basic) Capability {
182182

183183
return capable.Capabilities()
184184
}
185+
186+
// CapabilityCheck tests the filesystem for the provided capabilities and
187+
// returns true in case it supports all of them.
188+
func CapabilityCheck(fs Basic, capabilities Capability) bool {
189+
fsCaps := Capabilities(fs)
190+
return fsCaps&capabilities == capabilities
191+
}

fs_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package billy_test
2+
3+
import (
4+
"testing"
5+
6+
. "gopkg.in/src-d/go-billy.v4"
7+
"gopkg.in/src-d/go-billy.v4/memfs"
8+
9+
. "gopkg.in/check.v1"
10+
)
11+
12+
type FSSuite struct{}
13+
14+
func Test(t *testing.T) { TestingT(t) }
15+
16+
var _ = Suite(&FSSuite{})
17+
18+
func (s *FSSuite) TestCapabilities(c *C) {
19+
cases := []struct {
20+
caps Capability
21+
expected bool
22+
}{
23+
{CapLock, false},
24+
{CapRead, true},
25+
{CapRead | CapWrite, true},
26+
{CapRead | CapWrite | CapReadAndWrite | CapTruncate, true},
27+
{CapRead | CapWrite | CapReadAndWrite | CapTruncate | CapLock, false},
28+
{CapTruncate | CapLock, false},
29+
}
30+
31+
// This filesystem supports all capabilities except for CapLock
32+
mem := memfs.New()
33+
34+
for _, e := range cases {
35+
c.Assert(CapabilityCheck(mem, e.caps), Equals, e.expected)
36+
}
37+
}

0 commit comments

Comments
 (0)