mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2026-02-03 01:45:09 +01:00
feat(spec): version field (#85)
Adds a `version` field to the `jsonnetfile.json`, so that `jb` can automatically recognize too old / too new schema versions, instead of panicking.
This commit is contained in:
parent
efe0c9e864
commit
bcd89fd33d
23 changed files with 454 additions and 102 deletions
|
|
@ -23,47 +23,167 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec/deps"
|
||||
v1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
|
||||
)
|
||||
|
||||
const notExist = "/this/does/not/exist"
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
jsonnetfileContent := `
|
||||
{
|
||||
"legacyImports": false,
|
||||
"dependencies": [
|
||||
{
|
||||
"source": {
|
||||
"git": {
|
||||
"remote": "https://github.com/foobar/foobar",
|
||||
"subdir": ""
|
||||
}
|
||||
},
|
||||
"version": "master"
|
||||
const v0JSON = `{
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "grafana-builder",
|
||||
"source": {
|
||||
"git": {
|
||||
"remote": "https://github.com/grafana/jsonnet-libs",
|
||||
"subdir": "grafana-builder"
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
},
|
||||
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
|
||||
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
|
||||
},
|
||||
{
|
||||
"name": "prometheus-mixin",
|
||||
"source": {
|
||||
"git": {
|
||||
"remote": "https://github.com/prometheus/prometheus",
|
||||
"subdir": "documentation/prometheus-mixin"
|
||||
}
|
||||
},
|
||||
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
|
||||
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
jsonnetFileExpected := spec.JsonnetFile{
|
||||
LegacyImports: false,
|
||||
Dependencies: map[string]deps.Dependency{
|
||||
"github.com/foobar/foobar": {
|
||||
Source: deps.Source{
|
||||
GitSource: &deps.Git{
|
||||
Scheme: deps.GitSchemeHTTPS,
|
||||
Host: "github.com",
|
||||
User: "foobar",
|
||||
Repo: "foobar",
|
||||
Subdir: "",
|
||||
},
|
||||
var v0Jsonnetfile = v1.JsonnetFile{
|
||||
Dependencies: map[string]deps.Dependency{
|
||||
"grafana-builder": {
|
||||
Source: deps.Source{
|
||||
GitSource: &deps.Git{
|
||||
Scheme: deps.GitSchemeHTTPS,
|
||||
Host: "github.com",
|
||||
User: "grafana",
|
||||
Repo: "jsonnet-libs",
|
||||
Subdir: "grafana-builder",
|
||||
},
|
||||
Version: "master",
|
||||
}},
|
||||
},
|
||||
Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
|
||||
Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=",
|
||||
},
|
||||
"prometheus-mixin": {
|
||||
Source: deps.Source{
|
||||
GitSource: &deps.Git{
|
||||
Scheme: deps.GitSchemeHTTPS,
|
||||
Host: "github.com",
|
||||
User: "prometheus",
|
||||
Repo: "prometheus",
|
||||
Subdir: "documentation/prometheus-mixin",
|
||||
},
|
||||
},
|
||||
Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
|
||||
Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=",
|
||||
},
|
||||
},
|
||||
LegacyImports: true,
|
||||
}
|
||||
|
||||
const v1JSON = `{
|
||||
"version": 1,
|
||||
"dependencies": [
|
||||
{
|
||||
"source": {
|
||||
"git": {
|
||||
"remote": "https://github.com/grafana/jsonnet-libs",
|
||||
"subdir": "grafana-builder"
|
||||
}
|
||||
},
|
||||
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
|
||||
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
|
||||
},
|
||||
{
|
||||
"name": "prometheus",
|
||||
"source": {
|
||||
"git": {
|
||||
"remote": "https://github.com/prometheus/prometheus",
|
||||
"subdir": "documentation/prometheus-mixin"
|
||||
}
|
||||
},
|
||||
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
|
||||
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
|
||||
}
|
||||
],
|
||||
"legacyImports": false
|
||||
}`
|
||||
|
||||
var v1Jsonnetfile = v1.JsonnetFile{
|
||||
Dependencies: map[string]deps.Dependency{
|
||||
"github.com/grafana/jsonnet-libs/grafana-builder": {
|
||||
Source: deps.Source{
|
||||
GitSource: &deps.Git{
|
||||
Scheme: deps.GitSchemeHTTPS,
|
||||
Host: "github.com",
|
||||
User: "grafana",
|
||||
Repo: "jsonnet-libs",
|
||||
Subdir: "/grafana-builder",
|
||||
},
|
||||
},
|
||||
Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
|
||||
Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=",
|
||||
},
|
||||
"github.com/prometheus/prometheus/documentation/prometheus-mixin": {
|
||||
LegacyNameCompat: "prometheus",
|
||||
Source: deps.Source{
|
||||
GitSource: &deps.Git{
|
||||
Scheme: deps.GitSchemeHTTPS,
|
||||
Host: "github.com",
|
||||
User: "prometheus",
|
||||
Repo: "prometheus",
|
||||
Subdir: "/documentation/prometheus-mixin",
|
||||
},
|
||||
},
|
||||
Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
|
||||
Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=",
|
||||
},
|
||||
},
|
||||
LegacyImports: false,
|
||||
}
|
||||
|
||||
func TestVersions(t *testing.T) {
|
||||
tests := []struct {
|
||||
Name string
|
||||
JSON string
|
||||
Jsonnetfile v1.JsonnetFile
|
||||
Error error
|
||||
}{
|
||||
{
|
||||
Name: "v0",
|
||||
JSON: v0JSON,
|
||||
Jsonnetfile: v0Jsonnetfile,
|
||||
},
|
||||
{
|
||||
Name: "v1",
|
||||
JSON: v1JSON,
|
||||
Jsonnetfile: v1Jsonnetfile,
|
||||
},
|
||||
{
|
||||
Name: "v100",
|
||||
JSON: `{"version": 100}`,
|
||||
Jsonnetfile: v1.New(),
|
||||
Error: jsonnetfile.ErrUpdateJB,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
jf, err := jsonnetfile.Unmarshal([]byte(tc.JSON))
|
||||
assert.Equal(t, tc.Error, err)
|
||||
assert.Equal(t, tc.Jsonnetfile, jf)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadV1(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "jb-load-jsonnetfile")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -71,12 +191,12 @@ func TestLoad(t *testing.T) {
|
|||
defer os.RemoveAll(tempDir)
|
||||
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(jsonnetfileContent), os.ModePerm)
|
||||
err = ioutil.WriteFile(tempFile, []byte(v1JSON), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
jf, err := jsonnetfile.Load(tempFile)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, jsonnetFileExpected, jf)
|
||||
assert.Equal(t, v1Jsonnetfile, jf)
|
||||
}
|
||||
|
||||
func TestLoadEmpty(t *testing.T) {
|
||||
|
|
@ -88,18 +208,18 @@ func TestLoadEmpty(t *testing.T) {
|
|||
|
||||
// write empty json file
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(`{}`), os.ModePerm)
|
||||
err = ioutil.WriteFile(tempFile, []byte(`{"version":1}`), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// expect it to be loaded properly
|
||||
got, err := jsonnetfile.Load(tempFile)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, spec.New(), got)
|
||||
assert.Equal(t, v1.New(), got)
|
||||
}
|
||||
|
||||
func TestLoadNotExist(t *testing.T) {
|
||||
jf, err := jsonnetfile.Load(notExist)
|
||||
assert.Equal(t, spec.New(), jf)
|
||||
assert.Equal(t, v1.New(), jf)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue