Create LocalSource as type for local dependencies

This commit is contained in:
Matthias Loibl 2019-07-23 13:28:58 -07:00
parent 6ee790d911
commit 07801936c0
No known key found for this signature in database
GPG key ID: 78A796CA74CA38BA
6 changed files with 71 additions and 23 deletions

View file

@ -40,26 +40,28 @@ func TestInstallCommand(t *testing.T) {
ExpectedJsonnetFile: []byte(`{"dependencies":null}`),
ExpectedJsonnetLockFile: []byte(`{"dependencies":null}`),
}, {
Name: "OneURL",
URLs: []*url.URL{
{
Scheme: "https",
Host: "github.com",
Path: "jsonnet-bundler/jsonnet-bundler@v0.1.0",
},
},
Name: "OneURL",
URLs: []string{"github.com/jsonnet-bundler/jsonnet-bundler@v0.1.0"},
ExpectedCode: 0,
ExpectedJsonnetFile: []byte(`{"dependencies": [{"name": "jsonnet-bundler", "source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "v0.1.0"}]}`),
ExpectedJsonnetLockFile: []byte(`{"dependencies": [{"name": "jsonnet-bundler", "source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "080f157c7fb85ad0281ea78f6c641eaa570a582f"}]}`),
}, {
Name: "Relative",
URLs: []string{"test/jsonnet/foobar"},
ExpectedCode: 0,
ExpectedJsonnetFile: []byte(`{"dependencies":null}`),
ExpectedJsonnetLockFile: []byte(`{"dependencies":null}`),
},
}
for _, tc := range testcases {
t.Run(tc.Name, func(t *testing.T) {
_ = t.Run(tc.Name, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "jb-install")
assert.NoError(t, err)
err = os.MkdirAll(filepath.Join(tempDir, "test/jsonnet/foobar"), os.ModePerm)
assert.NoError(t, err)
defer os.Remove(tempDir)
defer os.RemoveAll("vendor") // delete test vendor folder
defer os.RemoveAll("vendor") // cloning jsonnet-bundler will create this folder
jsonnetFile := filepath.Join(tempDir, jsonnetfile.File)
jsonnetLockFile := filepath.Join(tempDir, jsonnetfile.LockFile)

View file

@ -226,8 +226,6 @@ func parseLocalDependency(p string) *spec.Dependency {
info, err := os.Stat(clean)
if err != nil {
wd, _ := os.Getwd()
fmt.Println(err, wd)
return nil
}
@ -238,11 +236,10 @@ func parseLocalDependency(p string) *spec.Dependency {
return &spec.Dependency{
Name: info.Name(),
Source: spec.Source{
GitSource: &spec.GitSource{
Remote: ".",
Subdir: clean,
LocalSource: &spec.LocalSource{
Directory: clean,
},
},
Version: ".",
Version: "",
}
}

View file

@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestParseDepedency(t *testing.T) {
func TestParseDependency(t *testing.T) {
const testFolder = "test/jsonnet/foobar"
err := os.MkdirAll(testFolder, os.ModePerm)
if err != nil {
@ -79,12 +79,11 @@ func TestParseDepedency(t *testing.T) {
want: &spec.Dependency{
Name: "foobar",
Source: spec.Source{
GitSource: &spec.GitSource{
Remote: ".",
Subdir: "test/jsonnet/foobar",
LocalSource: &spec.LocalSource{
Directory: "test/jsonnet/foobar",
},
},
Version: ".",
Version: "",
},
},
}

42
pkg/local.go Normal file
View file

@ -0,0 +1,42 @@
// 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 pkg
import (
"context"
"fmt"
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
)
type LocalPackage struct {
Source *spec.LocalSource
}
func NewLocalPackage(source *spec.LocalSource) Interface {
return &LocalPackage{
Source: source,
}
}
func (p *LocalPackage) Install(ctx context.Context, dir, version string) (lockVersion string, err error) {
fmt.Println("SYMLINK THIS SHIT, HAHA")
// TODO: Where do I get the name of the package?
fmt.Println(ctx, dir, version)
return "", nil
}

View file

@ -54,6 +54,9 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
p = NewGitPackage(dep.Source.GitSource)
subdir = dep.Source.GitSource.Subdir
}
if dep.Source.LocalSource != nil {
p = NewLocalPackage(dep.Source.LocalSource)
}
lockVersion, err := p.Install(ctx, tmpDir, dep.Version)
if err != nil {
@ -100,7 +103,7 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
return nil, err
}
depsDeps, err := LoadJsonnetfile(filepath)
// It is ok for depedencies not to have a JsonnetFile, it just means
// It is ok for dependencies not to have a JsonnetFile, it just means
// they do not have transitive dependencies of their own.
if err != nil && !os.IsNotExist(err) {
return nil, err

View file

@ -26,10 +26,15 @@ type Dependency struct {
}
type Source struct {
GitSource *GitSource `json:"git"`
GitSource *GitSource `json:"git,omitempty"`
LocalSource *LocalSource `json:"local,omitempty"`
}
type GitSource struct {
Remote string `json:"remote"`
Subdir string `json:"subdir"`
}
type LocalSource struct {
Directory string `json:"directory"`
}