Skip to content

Commit

Permalink
Merge pull request #522 from zong-zhe/fix-parse-modspec
Browse files Browse the repository at this point in the history
fix: fix parse error for ModSpec from str
  • Loading branch information
Peefy authored Nov 5, 2024
2 parents f5ac204 + 9c86e81 commit bc053bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ func (ps *ModSpec) FromString(registryStr string) error {
}

if parts[1] == "" {
return errors.New("invalid package reference")
ps.Name = parts[0]
return nil
}

ps.Name = parts[0]
Expand Down
29 changes: 29 additions & 0 deletions pkg/downloader/source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package downloader

import (
"testing"

"gotest.tools/v3/assert"
)

func TestParseModSpecFromStr(t *testing.T) {
tests := []struct {
input string
expected ModSpec
}{
{"subhelloworld:0.0.1", ModSpec{Name: "subhelloworld", Version: "0.0.1"}},
{"subhelloworld", ModSpec{Name: "subhelloworld", Version: ""}},
{"subhelloworld:", ModSpec{Name: "subhelloworld", Version: ""}},
{":0.0.1", ModSpec{Name: "", Version: "0.0.1"}},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
modspec := ModSpec{}
err := modspec.FromString(tt.input)
assert.NilError(t, err)
assert.Equal(t, modspec.Name, tt.expected.Name)
assert.Equal(t, modspec.Version, tt.expected.Version)
})
}
}

0 comments on commit bc053bc

Please sign in to comment.