Skip to content

Commit 78cadea

Browse files
authored
Adding coverage to tools package (#85)
Add coverage for package secret Linter issues Consolidate tools tests into tools test file Clean-up test structure update contributing
1 parent b95e3fd commit 78cadea

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ make format
2323
make lint
2424
```
2525

26+
## Testing
27+
28+
### Running Tests
29+
30+
```bash
31+
# Run all unit tests
32+
make test
33+
34+
# Run integration tests
35+
make integration
36+
```
37+
38+
### Unit Test Coverage
39+
40+
```bash
41+
# Generate HTML coverage report for ALL packages in one view
42+
go test -cover -coverprofile=coverage.out ./... -short
43+
go tool cover -html=coverage.out -o coverage.html && open coverage.html
44+
```
45+
2646
## Open a Pull Request
2747

2848
1. Fork the repository
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package secret
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestGetSecretKey(t *testing.T) {
12+
result := getSecretKey("mykey")
13+
assert.Equal(t, "sm_mykey", result)
14+
}
15+
16+
func TestParseArg(t *testing.T) {
17+
// Test key=value parsing
18+
secret, err := ParseArg("key=value", SetOpts{Provider: Credstore})
19+
require.NoError(t, err)
20+
assert.Equal(t, "key", secret.key)
21+
assert.Equal(t, "value", secret.val)
22+
23+
// Test key-only for non-direct providers
24+
secret, err = ParseArg("keyname", SetOpts{Provider: "oauth/github"})
25+
require.NoError(t, err)
26+
assert.Equal(t, "keyname", secret.key)
27+
assert.Empty(t, secret.val)
28+
29+
// Test error on key=value with non-direct provider
30+
_, err = ParseArg("key=value", SetOpts{Provider: "oauth/github"})
31+
assert.Error(t, err)
32+
}
33+
34+
func TestIsDirectValueProvider(t *testing.T) {
35+
assert.True(t, isDirectValueProvider(""))
36+
assert.True(t, isDirectValueProvider(Credstore))
37+
assert.False(t, isDirectValueProvider("oauth/github"))
38+
}
39+
40+
func TestIsValidProvider(t *testing.T) {
41+
// Valid providers
42+
assert.True(t, IsValidProvider(""))
43+
assert.True(t, IsValidProvider(Credstore))
44+
assert.True(t, IsValidProvider("oauth/github"))
45+
assert.True(t, IsValidProvider("oauth/google"))
46+
47+
// Invalid providers
48+
assert.False(t, IsValidProvider("invalid"))
49+
assert.False(t, IsValidProvider("oauth"))
50+
}
51+
52+
func TestIsErrDecryption(t *testing.T) {
53+
// Test decryption error detection
54+
decryptErr := errors.New("gpg: decryption failed: No secret key")
55+
assert.True(t, isErrDecryption(decryptErr))
56+
57+
// Test other errors
58+
otherErr := errors.New("some other error")
59+
assert.False(t, isErrDecryption(otherErr))
60+
61+
// Test nil
62+
assert.False(t, isErrDecryption(nil))
63+
}

cmd/docker-mcp/tools/tools_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/docker/docker/api/types/volume"
11+
"github.com/modelcontextprotocol/go-sdk/mcp"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314

@@ -318,3 +319,57 @@ func withSampleCatalog() option {
318319
writeFile(t, filepath.Join(home, ".docker/mcp/catalogs/docker-mcp.yaml"), []byte(catalogContent))
319320
}
320321
}
322+
323+
// Unit tests for call
324+
325+
func TestCallNoToolName(t *testing.T) {
326+
err := Call(context.Background(), "2", []string{}, false, []string{})
327+
require.Error(t, err)
328+
assert.Equal(t, "no tool name provided", err.Error())
329+
}
330+
331+
func TestToText(t *testing.T) {
332+
// Test basic functionality - joining multiple text contents
333+
response := &mcp.CallToolResult{
334+
Content: []mcp.Content{
335+
&mcp.TextContent{Text: "First"},
336+
&mcp.TextContent{Text: "Second"},
337+
},
338+
}
339+
result := toText(response)
340+
assert.Equal(t, "First\nSecond", result)
341+
}
342+
343+
func TestParseArgs(t *testing.T) {
344+
// Test key=value parsing
345+
result := parseArgs([]string{"key1=value1", "key2=value2"})
346+
expected := map[string]any{"key1": "value1", "key2": "value2"}
347+
assert.Equal(t, expected, result)
348+
349+
// Test duplicate keys become arrays
350+
result = parseArgs([]string{"tag=red", "tag=blue"})
351+
expected = map[string]any{"tag": []any{"red", "blue"}}
352+
assert.Equal(t, expected, result)
353+
}
354+
355+
// Unit tests for list
356+
357+
func TestToolDescription(t *testing.T) {
358+
// Test that title annotation takes precedence over description
359+
tool := &mcp.Tool{
360+
Description: "Longer description",
361+
Annotations: &mcp.ToolAnnotations{Title: "Short Title"},
362+
}
363+
result := toolDescription(tool)
364+
assert.Equal(t, "Short Title", result)
365+
}
366+
367+
func TestDescriptionSummary(t *testing.T) {
368+
// Test key behavior: stops at first sentence
369+
result := descriptionSummary("First sentence. Second sentence.")
370+
assert.Equal(t, "First sentence.", result)
371+
372+
// Test key behavior: stops at "Error Responses:"
373+
result = descriptionSummary("Tool description.\nError Responses:\n- 404 if not found")
374+
assert.Equal(t, "Tool description.", result)
375+
}

0 commit comments

Comments
 (0)