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"
)
const initContents = `{"version": 1, "dependencies": [], "legacyImports": false}`
const initContents = `{"version": 1, "dependencies": [], "legacyImports": true}`
func TestInstallCommand(t *testing.T) {
testInstallCommandWithJsonnetHome(t, "vendor")
@ -59,14 +59,14 @@ func testInstallCommandWithJsonnetHome(t *testing.T, jsonnetHome string) {
Name: "OneURL",
URIs: []string{"github.com/jsonnet-bundler/jsonnet-bundler@v0.1.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}`),
},
{
Name: "Local",
URIs: []string{"jsonnet/foobar"},
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}`),
},
}

View file

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

View file

@ -58,31 +58,33 @@ const v0JSON = `{
var v0Jsonnetfile = v1.JsonnetFile{
Dependencies: map[string]deps.Dependency{
"grafana-builder": {
"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",
Subdir: "/grafana-builder",
},
},
Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=",
LegacyNameCompat: "grafana-builder",
},
"prometheus-mixin": {
"github.com/prometheus/prometheus/documentation/prometheus-mixin": {
Source: deps.Source{
GitSource: &deps.Git{
Scheme: deps.GitSchemeHTTPS,
Host: "github.com",
User: "prometheus",
Repo: "prometheus",
Subdir: "documentation/prometheus-mixin",
Subdir: "/documentation/prometheus-mixin",
},
},
Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=",
LegacyNameCompat: "prometheus-mixin",
},
},
LegacyImports: true,
@ -208,7 +210,7 @@ func TestLoadEmpty(t *testing.T) {
// write empty json 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)
// expect it to be loaded properly

View file

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

View file

@ -73,6 +73,7 @@ func (jf JsonnetFile) MarshalJSON() ([]byte, error) {
var s jsonFile
s.Version = Version
s.LegacyImports = jf.LegacyImports
for _, d := range jf.Dependencies {
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
}