Skip to content

Commit

Permalink
Only allow full semantic versions. (#56)
Browse files Browse the repository at this point in the history
Also require tags to have a 'v' in front.
  • Loading branch information
floitsch authored Sep 19, 2024
1 parent 9637186 commit 273e738
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 95 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/toitlang/tpkg
go 1.20

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/alessio/shellescape v1.4.1
github.com/alexflint/go-filemutex v1.1.0
github.com/go-git/go-git/v5 v5.8.1
Expand Down Expand Up @@ -30,14 +31,11 @@ require (
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
Expand All @@ -57,7 +55,6 @@ require (
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
72 changes: 8 additions & 64 deletions go.sum

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions pkg/tpkg/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sort"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/hashicorp/go-version"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -385,8 +386,12 @@ func ScrapeDescriptionGit(ctx context.Context, url string, v string, allowsLocal
}

originalVersion := v
v = strings.TrimPrefix(v, "v")
_, err := version.NewVersion(v)
if !strings.HasPrefix(v, "v") {
return nil, ui.ReportError("Invalid version: '%s', not starting with 'v'", originalVersion)
}
v = v[1:]
// Check that it is a valid semantic version.
_, err := semver.StrictNewVersion(v)
if err != nil {
return nil, ui.ReportError("Invalid version: '%s'", originalVersion)
}
Expand Down
6 changes: 3 additions & 3 deletions tests/assets/pkg/DeepPackage/gold/test.gold
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
pkg registry add --local deep <TEST>/nested_registry
Exit Code: 0
===================
pkg describe --out-dir=<TEST>/nested_registry github.com/toitware/test-pkg.git/foo 1.0.0
pkg describe --out-dir=<TEST>/nested_registry github.com/toitware/test-pkg.git/foo v1.0.0
Exit Code: 0
Info: Wrote '<TEST>/nested_registry/packages/github.com/toitware/test-pkg.git/foo/1.0.0/desc.yaml'
===================
pkg describe --out-dir=<TEST>/nested_registry github.com/toitware/test-pkg.git/foo 2.3.0
pkg describe --out-dir=<TEST>/nested_registry github.com/toitware/test-pkg.git/foo v2.3.0
Exit Code: 0
Info: Wrote '<TEST>/nested_registry/packages/github.com/toitware/test-pkg.git/foo/2.3.0/desc.yaml'
===================
pkg describe --out-dir=<TEST>/nested_registry github.com/toitware/test-pkg.git/bar/gee 1.0.1
pkg describe --out-dir=<TEST>/nested_registry github.com/toitware/test-pkg.git/bar/gee v1.0.1
Exit Code: 0
Info: Wrote '<TEST>/nested_registry/packages/github.com/toitware/test-pkg.git/bar/gee/1.0.1/desc.yaml'
===================
Expand Down
7 changes: 7 additions & 0 deletions tests/assets/pkg/ScrapeGit/gold/bad_sem_version.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pkg describe https://github.com/toitware/toit-ignore 1.0
Exit Code: 1
Error: Invalid version: '1.0', not starting with 'v'
===================
pkg describe https://github.com/toitware/toit-ignore v1.0
Exit Code: 1
Error: Invalid version: 'v1.0'
10 changes: 9 additions & 1 deletion tests/assets/pkg/ScrapeGit/gold/bad_version.gold
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
pkg describe https://github.com/toitware/toit-morse 1.0.0
Exit Code: 1
Error: Invalid version: '1.0.0', not starting with 'v'
===================
pkg describe https://github.com/toitware/toit-morse bad-version
Exit Code: 1
Error: Invalid version: 'bad-version'
Error: Invalid version: 'bad-version', not starting with 'v'
===================
pkg describe https://github.com/toitware/toit-morse vbad-version
Exit Code: 1
Error: Invalid version: 'vbad-version'
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/deep.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkg describe https://github.com/toitware/test-pkg.git/foo 1.0.0
pkg describe https://github.com/toitware/test-pkg.git/foo v1.0.0
Exit Code: 0
foo:
description: nested package foo
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/https_morse.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkg describe https://github.com/toitware/toit-morse 1.0.6
pkg describe https://github.com/toitware/toit-morse v1.0.6
Exit Code: 0
morse:
description: Functions for International (ITU) Morse code.
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/https_morse_dot_git.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkg describe https://github.com/toitware/toit-morse.git 1.0.6
pkg describe https://github.com/toitware/toit-morse.git v1.0.6
Exit Code: 0
morse:
description: Functions for International (ITU) Morse code.
Expand Down
4 changes: 2 additions & 2 deletions tests/assets/pkg/ScrapeGit/gold/local_dep.gold
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pkg describe https://github.com/toitware/test-pkg.git/local_dep 1.0.0
pkg describe https://github.com/toitware/test-pkg.git/local_dep v1.0.0
Exit Code: 1
Error: Dependency to local path: 'some/path'
===================
pkg describe --allow-local-deps https://github.com/toitware/test-pkg.git/local_dep 1.0.0
pkg describe --allow-local-deps https://github.com/toitware/test-pkg.git/local_dep v1.0.0
Exit Code: 0
local_dep:
description: nested package local_dep with local dependency
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/morse-upper.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkg describe githUb.com/toitware/toit-MoRse 1.0.6
pkg describe githUb.com/toitware/toit-MoRse v1.0.6
Exit Code: 0
morse:
description: Functions for International (ITU) Morse code.
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/morse.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkg describe github.com/toitware/toit-morse 1.0.6
pkg describe github.com/toitware/toit-morse v1.0.6
Exit Code: 0
morse:
description: Functions for International (ITU) Morse code.
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/not_found.gold
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pkg describe https://toit.io/testing/not_exist 1.0.0
pkg describe https://toit.io/testing/not_exist v1.0.0
Exit Code: 1
Error: Error while cloning 'toit.io/testing/not_exist' with tag 'v1.0.0': repository not found
2 changes: 1 addition & 1 deletion tests/assets/pkg/ScrapeGit/gold/write.gold
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pkg describe --out-dir=foo
Exit Code: 1
Error: The --out-dir flag requires a URL and version
===================
pkg describe https://github.com/toitware/toit-morse 1.0.6 --out-dir=<TEST>/out
pkg describe https://github.com/toitware/toit-morse v1.0.6 --out-dir=<TEST>/out
Exit Code: 0
Info: Wrote '<TEST>/out/packages/github.com/toitware/toit-morse/1.0.6/desc.yaml'
31 changes: 19 additions & 12 deletions tests/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,40 +1436,47 @@ func test_toitPkg(t *tedi.T) {

t.Run("ScrapeGit", func(t *tedi.T, pt PkgTest) {
pt.GoldToit("morse", [][]string{
{"pkg", "describe", "github.com/toitware/toit-morse", "1.0.6"},
{"pkg", "describe", "github.com/toitware/toit-morse", "v1.0.6"},
})
pt.GoldToit("morse-upper", [][]string{
{"pkg", "describe", "githUb.com/toitware/toit-MoRse", "1.0.6"},
{"pkg", "describe", "githUb.com/toitware/toit-MoRse", "v1.0.6"},
})
pt.GoldToit("https_morse", [][]string{
{"pkg", "describe", "https://github.com/toitware/toit-morse", "1.0.6"},
{"pkg", "describe", "https://github.com/toitware/toit-morse", "v1.0.6"},
})
pt.GoldToit("https_morse_dot_git", [][]string{
{"pkg", "describe", "https://github.com/toitware/toit-morse.git", "1.0.6"},
{"pkg", "describe", "https://github.com/toitware/toit-morse.git", "v1.0.6"},
})

pt.GoldToit("not_found", [][]string{
{"pkg", "describe", "https://toit.io/testing/not_exist", "1.0.0"},
{"pkg", "describe", "https://toit.io/testing/not_exist", "v1.0.0"},
})

pt.GoldToit("bad_version", [][]string{
{"pkg", "describe", "https://github.com/toitware/toit-morse", "1.0.0"},
{"pkg", "describe", "https://github.com/toitware/toit-morse", "bad-version"},
{"pkg", "describe", "https://github.com/toitware/toit-morse", "vbad-version"},
})

pt.GoldToit("bad_sem_version", [][]string{
{"pkg", "describe", "https://github.com/toitware/toit-ignore", "1.0"},
{"pkg", "describe", "https://github.com/toitware/toit-ignore", "v1.0"},
})

pt.GoldToit("deep", [][]string{
{"pkg", "describe", "https://github.com/toitware/test-pkg.git/foo", "1.0.0"},
{"pkg", "describe", "https://github.com/toitware/test-pkg.git/foo", "v1.0.0"},
})

pt.GoldToit("local_dep", [][]string{
{"pkg", "describe", "https://github.com/toitware/test-pkg.git/local_dep", "1.0.0"},
{"pkg", "describe", "--allow-local-deps", "https://github.com/toitware/test-pkg.git/local_dep", "1.0.0"},
{"pkg", "describe", "https://github.com/toitware/test-pkg.git/local_dep", "v1.0.0"},
{"pkg", "describe", "--allow-local-deps", "https://github.com/toitware/test-pkg.git/local_dep", "v1.0.0"},
})

outDir := filepath.Join(pt.dir, "out")
pt.GoldToit("write", [][]string{
{"pkg", "describe", ".", "--out-dir=foo"},
{"pkg", "describe", "--out-dir=foo"},
{"pkg", "describe", "https://github.com/toitware/toit-morse", "1.0.6", "--out-dir=" + outDir},
{"pkg", "describe", "https://github.com/toitware/toit-morse", "v1.0.6", "--out-dir=" + outDir},
})
descPath := filepath.Join(pt.dir, "out", "packages", "github.com", "toitware", "toit-morse", "1.0.6", "desc.yaml")
assert.FileExists(t, descPath)
Expand Down Expand Up @@ -1499,9 +1506,9 @@ func test_toitPkg(t *tedi.T) {
registryDir := filepath.Join(pt.dir, "nested_registry")
pt.GoldToit("test", [][]string{
{"pkg", "registry", "add", "--local", "deep", registryDir},
{"pkg", "describe", "--out-dir=" + registryDir, "github.com/toitware/test-pkg.git/foo", "1.0.0"},
{"pkg", "describe", "--out-dir=" + registryDir, "github.com/toitware/test-pkg.git/foo", "2.3.0"},
{"pkg", "describe", "--out-dir=" + registryDir, "github.com/toitware/test-pkg.git/bar/gee", "1.0.1"},
{"pkg", "describe", "--out-dir=" + registryDir, "github.com/toitware/test-pkg.git/foo", "v1.0.0"},
{"pkg", "describe", "--out-dir=" + registryDir, "github.com/toitware/test-pkg.git/foo", "v2.3.0"},
{"pkg", "describe", "--out-dir=" + registryDir, "github.com/toitware/test-pkg.git/bar/gee", "v1.0.1"},
{"pkg", "list"},
{"pkg", "install"},
{"exec", "test.toit"},
Expand Down

0 comments on commit 273e738

Please sign in to comment.