Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from jsonnet-bundler:master #6

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions .drone.jsonnet

This file was deleted.

75 changes: 0 additions & 75 deletions .drone.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/01-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
go_version: [1.16, 1.17, 1.18]
go_version: [1.21, 1.22, 1.23]

steps:
- name: Checkout Repository
Expand Down
1 change: 1 addition & 0 deletions cmd/jb/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration
// +build integration

package main
Expand Down
7 changes: 4 additions & 3 deletions cmd/jb/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ func installCommand(dir, jsonnetHome string, uris []string, single bool, legacyN
d.LegacyNameCompat = legacyName
}

if !depEqual(jsonnetFile.Dependencies[d.Name()], *d) {
jd, _ := jsonnetFile.Dependencies.Get(d.Name())
if !depEqual(jd, *d) {
// the dep passed on the cli is different from the jsonnetFile
jsonnetFile.Dependencies[d.Name()] = *d
jsonnetFile.Dependencies.Set(d.Name(), *d)

// we want to install the passed version (ignore the lock)
delete(lockFile.Dependencies, d.Name())
lockFile.Dependencies.Delete(d.Name())
}
}

Expand Down
122 changes: 111 additions & 11 deletions cmd/jb/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package main

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
v1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1"
Expand Down Expand Up @@ -140,32 +143,30 @@ func TestWriteChangedJsonnetFile(t *testing.T) {
Name: "NoDiffNotEmpty",
JsonnetFileBytes: []byte(`{"dependencies": [{"version": "master"}]}`),
NewJsonnetFile: v1.JsonnetFile{
Dependencies: map[string]deps.Dependency{
"": {
Dependencies: addDependencies(deps.NewOrdered(),
deps.Dependency{
Version: "master",
},
},
}),
},
ExpectWrite: false,
},
{
Name: "DiffVersion",
JsonnetFileBytes: []byte(`{"dependencies": [{"version": "1.0"}]}`),
NewJsonnetFile: v1.JsonnetFile{
Dependencies: map[string]deps.Dependency{
"": {
Dependencies: addDependencies(deps.NewOrdered(),
deps.Dependency{
Version: "2.0",
},
},
}),
},
ExpectWrite: true,
},
{
Name: "Diff",
JsonnetFileBytes: []byte(`{}`),
NewJsonnetFile: v1.JsonnetFile{
Dependencies: map[string]deps.Dependency{
"github.com/foobar/foobar": {
Dependencies: addDependencies(deps.NewOrdered(),
deps.Dependency{
Source: deps.Source{
GitSource: &deps.Git{
Scheme: deps.GitSchemeHTTPS,
Expand All @@ -176,7 +177,7 @@ func TestWriteChangedJsonnetFile(t *testing.T) {
},
},
Version: "master",
}},
}),
},
ExpectWrite: true,
},
Expand Down Expand Up @@ -204,3 +205,102 @@ func TestWriteChangedJsonnetFile(t *testing.T) {
})
}
}

func TestInstallTransitive(t *testing.T) {
const (
frozenLibFirstCommit = "9f40207f668e382b706e1822f2d46ce2cd0a57cc"
frozenLibSecondCommit = "ed7c1aff9e10d3b42fb130446d495f1c769ecd7b"
)

baseDir := t.TempDir()
subDirA := filepath.Join(baseDir, "a")
subDirB := filepath.Join(baseDir, "b")

writeDepFileTree(t, map[string]v1.JsonnetFile{
baseDir: {
Dependencies: addDependencies(deps.NewOrdered(),
localDependency(subDirA),
localDependency(subDirB),
)},
subDirA: jsonnetFileWithFrozenLib(frozenLibFirstCommit, ""),
subDirB: jsonnetFileWithFrozenLib(frozenLibSecondCommit, ""),
})

require.Equal(t, 0, installCommand(baseDir, "vendor", nil, false, ""))

lockCheckFrozenLibVersion(t, filepath.Join(baseDir, "jsonnetfile.lock.json"), frozenLibFirstCommit)
require.NoError(t, os.RemoveAll(filepath.Join(baseDir, "jsonnetfile.lock.json")))

// Reverse the order of the dependencies. The lock file should now contain the second commit in its version field.
writeDepFileTree(t, map[string]v1.JsonnetFile{
subDirA: jsonnetFileWithFrozenLib(frozenLibSecondCommit, ""),
subDirB: jsonnetFileWithFrozenLib(frozenLibFirstCommit, ""),
})

require.Equal(t, 0, installCommand(baseDir, "vendor", nil, false, ""))

lockCheckFrozenLibVersion(t, filepath.Join(baseDir, "jsonnetfile.lock.json"), frozenLibSecondCommit)
}

func lockCheckFrozenLibVersion(t *testing.T, lockPath, version string) {
t.Helper()

rawLock, err := os.ReadFile(lockPath)
require.NoError(t, err)
var lock v1.JsonnetFile
require.NoError(t, json.Unmarshal([]byte(rawLock), &lock))

lf, lfExists := lock.Dependencies.Get("github.com/jsonnet-bundler/frozen-lib")
require.True(t, lfExists, "expected to find frozen-lib in lock file")
require.Equal(t, version, lf.Version, "lock file: expected frozen-lib to have commit version of the first dependency in the base jsonnet file")
}

func addDependencies(o *deps.Ordered, ds ...deps.Dependency) *deps.Ordered {
for _, d := range ds {
o.Set(d.Name(), d)
}
return o
}

func jsonnetFileWithFrozenLib(version, sum string) v1.JsonnetFile {
return v1.JsonnetFile{
Dependencies: addDependencies(deps.NewOrdered(),
frozenDependency(version, sum)),
}
}

func frozenDependency(version, sum string) deps.Dependency {
return deps.Dependency{
Source: deps.Source{
GitSource: &deps.Git{
Scheme: "https://",
Host: "github.com",
User: "jsonnet-bundler",
Repo: "frozen-lib",
},
},
Version: version,
Sum: sum,
}
}

func localDependency(dir string) deps.Dependency {
return deps.Dependency{
Source: deps.Source{
LocalSource: &deps.Local{
Directory: dir,
},
},
}
}

func writeDepFileTree(t *testing.T, files map[string]v1.JsonnetFile) {
t.Helper()

for dir, file := range files {
require.NoError(t, os.MkdirAll(dir, os.ModePerm))
rj, err := json.Marshal(file)
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(dir, "jsonnetfile.json"), rj, 0644))
}
}
4 changes: 2 additions & 2 deletions cmd/jb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func updateCommand(dir, jsonnetHome string, uris []string) int {
kingpin.Fatalf("Unable to parse package URI `%s`", u)
}

delete(locks, d.Name())
locks.Delete(d.Name())
}

// no uris: update all
if len(uris) == 0 {
locks = make(map[string]deps.Dependency)
locks = deps.NewOrdered()
}

newLocks, err := pkg.Ensure(jsonnetFile, filepath.Join(dir, jsonnetHome), locks)
Expand Down
1 change: 1 addition & 0 deletions cmd/jb/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration
// +build integration

package main
Expand Down
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
module github.com/jsonnet-bundler/jsonnet-bundler

go 1.12
go 1.18

require (
github.com/elliotchance/orderedmap/v2 v2.2.0
github.com/fatih/color v1.13.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.4
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/campoy/embedmd v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/stretchr/testify v1.7.4
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading