Merge pull request #88 from jsonnet-bundler/fix-install

fix: proper parsing of v0
This commit is contained in:
Frederic Branczyk 2020-03-01 08:36:02 +01:00 committed by GitHub
commit cb0a14f809
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 119 additions and 83 deletions

View file

@ -28,7 +28,7 @@ import (
"github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps" "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
) )
const initContents = `{"version": 1, "dependencies": [], "legacyImports": false}` const initContents = `{"version": 1, "dependencies": [], "legacyImports": true}`
func TestInstallCommand(t *testing.T) { func TestInstallCommand(t *testing.T) {
testInstallCommandWithJsonnetHome(t, "vendor") testInstallCommandWithJsonnetHome(t, "vendor")
@ -59,14 +59,14 @@ func testInstallCommandWithJsonnetHome(t *testing.T, jsonnetHome string) {
Name: "OneURL", Name: "OneURL",
URIs: []string{"github.com/jsonnet-bundler/jsonnet-bundler@v0.1.0"}, URIs: []string{"github.com/jsonnet-bundler/jsonnet-bundler@v0.1.0"},
ExpectedCode: 0, ExpectedCode: 0,
ExpectedJsonnetFile: []byte(`{"version": 1, "dependencies": [{"source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "v0.1.0"}], "legacyImports": false}`), ExpectedJsonnetFile: []byte(`{"version": 1, "dependencies": [{"source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "v0.1.0"}], "legacyImports": true}`),
ExpectedJsonnetLockFile: []byte(`{"version": 1, "dependencies": [{"source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "080f157c7fb85ad0281ea78f6c641eaa570a582f", "sum": "W1uI550rQ66axRpPXA2EZDquyPg/5PHZlvUz1NEzefg="}], "legacyImports": false}`), ExpectedJsonnetLockFile: []byte(`{"version": 1, "dependencies": [{"source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "080f157c7fb85ad0281ea78f6c641eaa570a582f", "sum": "W1uI550rQ66axRpPXA2EZDquyPg/5PHZlvUz1NEzefg="}], "legacyImports": false}`),
}, },
{ {
Name: "Local", Name: "Local",
URIs: []string{"jsonnet/foobar"}, URIs: []string{"jsonnet/foobar"},
ExpectedCode: 0, ExpectedCode: 0,
ExpectedJsonnetFile: []byte(`{"version": 1, "dependencies": [{"source": {"local": {"directory": "jsonnet/foobar"}}, "version": ""}], "legacyImports": false}`), ExpectedJsonnetFile: []byte(`{"version": 1, "dependencies": [{"source": {"local": {"directory": "jsonnet/foobar"}}, "version": ""}], "legacyImports": true}`),
ExpectedJsonnetLockFile: []byte(`{"version": 1, "dependencies": [{"source": {"local": {"directory": "jsonnet/foobar"}}, "version": ""}], "legacyImports": false}`), ExpectedJsonnetLockFile: []byte(`{"version": 1, "dependencies": [{"source": {"local": {"directory": "jsonnet/foobar"}}, "version": ""}], "legacyImports": false}`),
}, },
} }

View file

@ -23,7 +23,6 @@ import (
v0 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v0" v0 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v0"
v1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1" v1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1"
depsv1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
) )
const ( const (
@ -63,39 +62,20 @@ func Unmarshal(bytes []byte) (v1.JsonnetFile, error) {
return m, err return m, err
} }
if versions.Version > v1.Version { switch versions.Version {
return m, ErrUpdateJB case v0.Version:
}
if versions.Version == v1.Version {
if err := json.Unmarshal(bytes, &m); err != nil {
return m, errors.Wrap(err, "failed to unmarshal v1 file")
}
return m, nil
} else {
var mv0 v0.JsonnetFile var mv0 v0.JsonnetFile
if err := json.Unmarshal(bytes, &mv0); err != nil { if err := json.Unmarshal(bytes, &mv0); err != nil {
return m, errors.Wrap(err, "failed to unmarshal jsonnetfile") return m, errors.Wrap(err, "failed to unmarshal jsonnetfile")
} }
return v1.FromV0(mv0)
for name, dep := range mv0.Dependencies { case v1.Version:
var d depsv1.Dependency if err := json.Unmarshal(bytes, &m); err != nil {
if dep.Source.GitSource != nil { return m, errors.Wrap(err, "failed to unmarshal v1 file")
d = *depsv1.Parse("", dep.Source.GitSource.Remote)
d.Source.GitSource.Subdir = dep.Source.GitSource.Subdir
}
if dep.Source.LocalSource != nil {
d = *depsv1.Parse(dep.Source.LocalSource.Directory, dep.Source.GitSource.Remote)
}
d.Sum = dep.Sum
d.Version = dep.Version
m.Dependencies[name] = d
} }
return m, nil return m, nil
default:
return m, ErrUpdateJB
} }
} }

View file

@ -31,58 +31,60 @@ const notExist = "/this/does/not/exist"
const v0JSON = `{ const v0JSON = `{
"dependencies": [ "dependencies": [
{ {
"name": "grafana-builder", "name": "grafana-builder",
"source": { "source": {
"git": { "git": {
"remote": "https://github.com/grafana/jsonnet-libs", "remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "grafana-builder" "subdir": "grafana-builder"
} }
}, },
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648", "version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=" "sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
}, },
{ {
"name": "prometheus-mixin", "name": "prometheus-mixin",
"source": { "source": {
"git": { "git": {
"remote": "https://github.com/prometheus/prometheus", "remote": "https://github.com/prometheus/prometheus",
"subdir": "documentation/prometheus-mixin" "subdir": "documentation/prometheus-mixin"
} }
}, },
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684", "version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=" "sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
} }
] ]
}` }`
var v0Jsonnetfile = v1.JsonnetFile{ var v0Jsonnetfile = v1.JsonnetFile{
Dependencies: map[string]deps.Dependency{ Dependencies: map[string]deps.Dependency{
"grafana-builder": { "github.com/grafana/jsonnet-libs/grafana-builder": {
Source: deps.Source{ Source: deps.Source{
GitSource: &deps.Git{ GitSource: &deps.Git{
Scheme: deps.GitSchemeHTTPS, Scheme: deps.GitSchemeHTTPS,
Host: "github.com", Host: "github.com",
User: "grafana", User: "grafana",
Repo: "jsonnet-libs", Repo: "jsonnet-libs",
Subdir: "grafana-builder", Subdir: "/grafana-builder",
}, },
}, },
Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648", Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=", Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=",
LegacyNameCompat: "grafana-builder",
}, },
"prometheus-mixin": { "github.com/prometheus/prometheus/documentation/prometheus-mixin": {
Source: deps.Source{ Source: deps.Source{
GitSource: &deps.Git{ GitSource: &deps.Git{
Scheme: deps.GitSchemeHTTPS, Scheme: deps.GitSchemeHTTPS,
Host: "github.com", Host: "github.com",
User: "prometheus", User: "prometheus",
Repo: "prometheus", Repo: "prometheus",
Subdir: "documentation/prometheus-mixin", Subdir: "/documentation/prometheus-mixin",
}, },
}, },
Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684", Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=", Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=",
LegacyNameCompat: "prometheus-mixin",
}, },
}, },
LegacyImports: true, LegacyImports: true,
@ -91,27 +93,27 @@ var v0Jsonnetfile = v1.JsonnetFile{
const v1JSON = `{ const v1JSON = `{
"version": 1, "version": 1,
"dependencies": [ "dependencies": [
{ {
"source": { "source": {
"git": { "git": {
"remote": "https://github.com/grafana/jsonnet-libs", "remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "grafana-builder" "subdir": "grafana-builder"
} }
}, },
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648", "version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=" "sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
}, },
{ {
"name": "prometheus", "name": "prometheus",
"source": { "source": {
"git": { "git": {
"remote": "https://github.com/prometheus/prometheus", "remote": "https://github.com/prometheus/prometheus",
"subdir": "documentation/prometheus-mixin" "subdir": "documentation/prometheus-mixin"
} }
}, },
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684", "version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=" "sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
} }
], ],
"legacyImports": false "legacyImports": false
}` }`
@ -208,7 +210,7 @@ func TestLoadEmpty(t *testing.T) {
// write empty json file // write empty json file
tempFile := filepath.Join(tempDir, jsonnetfile.File) tempFile := filepath.Join(tempDir, jsonnetfile.File)
err = ioutil.WriteFile(tempFile, []byte(`{"version":1}`), os.ModePerm) err = ioutil.WriteFile(tempFile, []byte(`{}`), os.ModePerm)
assert.Nil(t, err) assert.Nil(t, err)
// expect it to be loaded properly // expect it to be loaded properly

View file

@ -19,6 +19,8 @@ import (
"sort" "sort"
) )
const Version = 0
// JsonnetFile is the structure of a `.json` file describing a set of jsonnet // JsonnetFile is the structure of a `.json` file describing a set of jsonnet
// dependencies. It is used for both, the jsonnetFile and the lockFile. // dependencies. It is used for both, the jsonnetFile and the lockFile.
type JsonnetFile struct { type JsonnetFile struct {

View file

@ -73,6 +73,7 @@ func (jf JsonnetFile) MarshalJSON() ([]byte, error) {
var s jsonFile var s jsonFile
s.Version = Version s.Version = Version
s.LegacyImports = jf.LegacyImports
for _, d := range jf.Dependencies { for _, d := range jf.Dependencies {
s.Dependencies = append(s.Dependencies, d) s.Dependencies = append(s.Dependencies, d)

51
spec/v1/v0.go Normal file
View file

@ -0,0 +1,51 @@
// Copyright 2018 jsonnet-bundler authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
import (
"path/filepath"
v0 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v0"
"github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
)
func FromV0(mv0 v0.JsonnetFile) (JsonnetFile, error) {
m := New()
m.LegacyImports = true
for name, old := range mv0.Dependencies {
var d deps.Dependency
switch {
case old.Source.GitSource != nil:
d = *deps.Parse("", old.Source.GitSource.Remote)
if old.Source.GitSource.Subdir != "" {
subdir := filepath.Clean("/" + old.Source.GitSource.Subdir)
d.Source.GitSource.Subdir = subdir
}
case old.Source.LocalSource != nil:
d = *deps.Parse("", old.Source.LocalSource.Directory)
}
d.Sum = old.Sum
d.Version = old.Version
d.LegacyNameCompat = name
m.Dependencies[d.Name()] = d
}
return m, nil
}