Skip to content

Commit

Permalink
fix: fix parse error for ModSpec from str
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 5, 2024
1 parent f5ac204 commit 9c86e81
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 9c86e81

Please sign in to comment.