Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"cmp"
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand Down Expand Up @@ -435,6 +436,58 @@ func TestNugetDependencies(t *testing.T) {
}
}

func TestCargoDependencies(t *testing.T) {
t.Parallel()
ctx := t.Context()
client := &http.Client{}
plugins := loadFilteredPlugins(t)
for _, p := range plugins {
if p.Registry.Cargo == nil || len(p.Registry.Cargo.Deps) == 0 {
continue
}
t.Run(fmt.Sprintf("%s/%s@%s", p.Identity.Owner(), p.Identity.Plugin(), p.PluginVersion), func(t *testing.T) {
t.Parallel()
for _, dep := range p.Registry.Cargo.Deps {
url := fmt.Sprintf("https://crates.io/api/v1/crates/%s", dep.Name)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoError(t, err)
// crates.io requires a descriptive User-Agent; see
// https://github.com/bufbuild/plugins/issues/252.
req.Header.Set("User-Agent", "bufbuild (github.com/bufbuild/plugins)")
resp, err := client.Do(req)
require.NoError(t, err)
require.Equalf(t, http.StatusOK, resp.StatusCode, "failed to fetch crate %q", dep.Name)
var data struct {
Versions []struct {
Yanked bool `json:"yanked"`
Num string `json:"num"`
} `json:"versions"`
}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
require.NoError(t, resp.Body.Close())
var matched bool
for _, v := range data.Versions {
if v.Yanked {
continue
}
if cargoReqMatches(dep.VersionRequirement, v.Num) {
matched = true
break
}
}
assert.Truef(t, matched, "no non-yanked version of crate %q satisfies req %q", dep.Name, dep.VersionRequirement)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would've caught the previous upgrade issue:

--- FAIL: TestCargoDependencies (1.05s)
    --- FAIL: TestCargoDependencies/anthropics/connect-rust@v0.6.1 (0.86s)
        plugins_test.go:478:
                Error Trace:    /Users/pkw/dev/plugins/tests/plugins_test.go:478
                Error:          Should be true
                Test:           TestCargoDependencies/anthropics/connect-rust@v0.6.1
                Messages:       no non-yanked version of crate "buffa" satisfies req "0.6.1"
        plugins_test.go:478:
                Error Trace:    /Users/pkw/dev/plugins/tests/plugins_test.go:478
                Error:          Should be true
                Test:           TestCargoDependencies/anthropics/connect-rust@v0.6.1
                Messages:       no non-yanked version of crate "buffa-types" satisfies req "0.6.1"

}
})
}
}

func cargoReqMatches(req, version string) bool {
if strings.Count(req, ".") >= 2 {
return req == version
}
return strings.HasPrefix(version, req+".")
}

func TestPyPIDependencies(t *testing.T) {
t.Parallel()
ctx := t.Context()
Expand Down
Loading