2018-02-05 16:05:59 -05:00
|
|
|
package git // import "github.com/docker/docker/builder/remotecontext/git"
|
2015-04-21 17:24:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-04-24 18:12:45 -04:00
|
|
|
"os"
|
2017-12-07 16:44:08 -05:00
|
|
|
"os/exec"
|
2015-04-24 18:12:45 -04:00
|
|
|
"path/filepath"
|
2016-02-11 21:19:17 -05:00
|
|
|
"runtime"
|
2016-05-31 22:36:25 -04:00
|
|
|
"strings"
|
2015-04-21 17:24:29 -04:00
|
|
|
"testing"
|
2017-06-01 17:15:13 -04:00
|
|
|
|
2017-12-22 16:30:49 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-04-21 17:24:29 -04:00
|
|
|
)
|
|
|
|
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
func TestParseRemoteURL(t *testing.T) {
|
2019-11-04 20:36:49 -05:00
|
|
|
tests := []struct {
|
|
|
|
doc string
|
|
|
|
url string
|
|
|
|
expected gitRepo
|
|
|
|
}{
|
2019-11-05 12:26:30 -05:00
|
|
|
{
|
|
|
|
doc: "git scheme uppercase, no url-fragment",
|
|
|
|
url: "GIT://github.com/user/repo.git",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "git://github.com/user/repo.git",
|
|
|
|
ref: "master",
|
|
|
|
},
|
|
|
|
},
|
2019-11-04 20:36:49 -05:00
|
|
|
{
|
|
|
|
doc: "git scheme, no url-fragment",
|
|
|
|
url: "git://github.com/user/repo.git",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "git://github.com/user/repo.git",
|
|
|
|
ref: "master",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "git scheme, with url-fragment",
|
|
|
|
url: "git://github.com/user/repo.git#mybranch:mydir/mysubdir/",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "git://github.com/user/repo.git",
|
|
|
|
ref: "mybranch",
|
|
|
|
subdir: "mydir/mysubdir/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "https scheme, no url-fragment",
|
|
|
|
url: "https://github.com/user/repo.git",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "https://github.com/user/repo.git",
|
|
|
|
ref: "master",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "https scheme, with url-fragment",
|
|
|
|
url: "https://github.com/user/repo.git#mybranch:mydir/mysubdir/",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "https://github.com/user/repo.git",
|
|
|
|
ref: "mybranch",
|
|
|
|
subdir: "mydir/mysubdir/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "git@, no url-fragment",
|
|
|
|
url: "git@github.com:user/repo.git",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "git@github.com:user/repo.git",
|
|
|
|
ref: "master",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "git@, with url-fragment",
|
|
|
|
url: "git@github.com:user/repo.git#mybranch:mydir/mysubdir/",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "git@github.com:user/repo.git",
|
|
|
|
ref: "mybranch",
|
|
|
|
subdir: "mydir/mysubdir/",
|
|
|
|
},
|
|
|
|
},
|
2019-11-05 16:58:58 -05:00
|
|
|
{
|
|
|
|
doc: "ssh, no url-fragment",
|
|
|
|
url: "ssh://github.com/user/repo.git",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "ssh://github.com/user/repo.git",
|
|
|
|
ref: "master",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "ssh, with url-fragment",
|
|
|
|
url: "ssh://github.com/user/repo.git#mybranch:mydir/mysubdir/",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "ssh://github.com/user/repo.git",
|
|
|
|
ref: "mybranch",
|
|
|
|
subdir: "mydir/mysubdir/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "ssh, with url-fragment and user",
|
|
|
|
url: "ssh://foo%40barcorp.com@github.com/user/repo.git#mybranch:mydir/mysubdir/",
|
|
|
|
expected: gitRepo{
|
|
|
|
remote: "ssh://foo%40barcorp.com@github.com/user/repo.git",
|
|
|
|
ref: "mybranch",
|
|
|
|
subdir: "mydir/mysubdir/",
|
|
|
|
},
|
|
|
|
},
|
2019-11-04 20:36:49 -05:00
|
|
|
}
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
|
2019-11-04 20:36:49 -05:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
repo, err := parseRemoteURL(tc.url)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(tc.expected, repo, cmp.AllowUnexported(gitRepo{})))
|
|
|
|
})
|
|
|
|
}
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:24:29 -04:00
|
|
|
func TestCloneArgsSmartHttp(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
serverURL, _ := url.Parse(server.URL)
|
|
|
|
|
|
|
|
serverURL.Path = "/repo.git"
|
|
|
|
|
|
|
|
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
q := r.URL.Query().Get("service")
|
|
|
|
w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", q))
|
|
|
|
})
|
|
|
|
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
args := fetchArgs(serverURL.String(), "master")
|
2019-02-06 14:58:40 -05:00
|
|
|
exp := []string{"fetch", "--depth", "1", "origin", "--", "master"}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(exp, args))
|
2015-04-21 17:24:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneArgsDumbHttp(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
serverURL, _ := url.Parse(server.URL)
|
|
|
|
|
|
|
|
serverURL.Path = "/repo.git"
|
|
|
|
|
|
|
|
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
})
|
|
|
|
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
args := fetchArgs(serverURL.String(), "master")
|
2019-02-06 14:58:40 -05:00
|
|
|
exp := []string{"fetch", "origin", "--", "master"}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(exp, args))
|
2015-04-21 17:24:29 -04:00
|
|
|
}
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2015-04-21 17:24:29 -04:00
|
|
|
func TestCloneArgsGit(t *testing.T) {
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
args := fetchArgs("git://github.com/docker/docker", "master")
|
2019-02-06 14:58:40 -05:00
|
|
|
exp := []string{"fetch", "--depth", "1", "origin", "--", "master"}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(exp, args))
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 22:36:25 -04:00
|
|
|
func gitGetConfig(name string) string {
|
|
|
|
b, err := git([]string{"config", "--get", name}...)
|
|
|
|
if err != nil {
|
|
|
|
// since we are interested in empty or non empty string,
|
|
|
|
// we can safely ignore the err here.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(b))
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
func TestCheckoutGit(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
root, err := os.MkdirTemp("", "docker-build-git-checkout")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
2016-05-31 22:36:25 -04:00
|
|
|
autocrlf := gitGetConfig("core.autocrlf")
|
|
|
|
if !(autocrlf == "true" || autocrlf == "false" ||
|
|
|
|
autocrlf == "input" || autocrlf == "") {
|
|
|
|
t.Logf("unknown core.autocrlf value: \"%s\"", autocrlf)
|
|
|
|
}
|
2016-02-11 21:19:17 -05:00
|
|
|
eol := "\n"
|
2016-05-31 22:36:25 -04:00
|
|
|
if autocrlf == "true" {
|
2016-02-11 21:19:17 -05:00
|
|
|
eol = "\r\n"
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
gitDir := filepath.Join(root, "repo")
|
|
|
|
_, err = git("init", gitDir)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "config", "user.email", "test@docker.com")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "config", "user.name", "Docker test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
|
|
|
subDir := filepath.Join(gitDir, "subdir")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, os.Mkdir(subDir, 0755))
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err = os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-01 14:41:45 -04:00
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
if err = os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-01 14:41:45 -04:00
|
|
|
}
|
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "add", "-A")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "commit", "-am", "First commit")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "checkout", "-b", "test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "add", "-A")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "commit", "-am", "Branch commit")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-06-01 17:15:13 -04:00
|
|
|
_, err = gitWithinDir(gitDir, "checkout", "master")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-12-07 16:44:08 -05:00
|
|
|
// set up submodule
|
|
|
|
subrepoDir := filepath.Join(root, "subrepo")
|
|
|
|
_, err = git("init", subrepoDir)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
_, err = gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
_, err = gitWithinDir(subrepoDir, "config", "user.name", "Docker test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
_, err = gitWithinDir(subrepoDir, "add", "-A")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
_, err = gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
cmd := exec.Command("git", "submodule", "add", subrepoDir, "sub") // this command doesn't work with --work-tree
|
|
|
|
cmd.Dir = gitDir
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, cmd.Run())
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
_, err = gitWithinDir(gitDir, "add", "-A")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
|
|
|
_, err = gitWithinDir(gitDir, "commit", "-am", "With submodule")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-07 16:44:08 -05:00
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
type singleCase struct {
|
2017-12-07 16:44:08 -05:00
|
|
|
frag string
|
|
|
|
exp string
|
|
|
|
fail bool
|
|
|
|
submodule bool
|
2016-02-11 21:19:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := []singleCase{
|
2017-12-07 16:44:08 -05:00
|
|
|
{"", "FROM scratch", false, true},
|
|
|
|
{"master", "FROM scratch", false, true},
|
|
|
|
{":subdir", "FROM scratch" + eol + "EXPOSE 5000", false, false},
|
|
|
|
{":nosubdir", "", true, false}, // missing directory error
|
|
|
|
{":Dockerfile", "", true, false}, // not a directory error
|
|
|
|
{"master:nosubdir", "", true, false},
|
|
|
|
{"master:subdir", "FROM scratch" + eol + "EXPOSE 5000", false, false},
|
|
|
|
{"master:../subdir", "", true, false},
|
|
|
|
{"test", "FROM scratch" + eol + "EXPOSE 3000", false, false},
|
|
|
|
{"test:", "FROM scratch" + eol + "EXPOSE 3000", false, false},
|
|
|
|
{"test:subdir", "FROM busybox" + eol + "EXPOSE 5000", false, false},
|
2016-02-11 21:19:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// Windows GIT (2.7.1 x64) does not support parentlink/absolutelink. Sample output below
|
|
|
|
// git --work-tree .\repo --git-dir .\repo\.git add -A
|
|
|
|
// error: readlink("absolutelink"): Function not implemented
|
|
|
|
// error: unable to index file absolutelink
|
|
|
|
// fatal: adding files failed
|
|
|
|
cases = append(cases, singleCase{frag: "master:absolutelink", exp: "FROM scratch" + eol + "EXPOSE 5000", fail: false})
|
|
|
|
cases = append(cases, singleCase{frag: "master:parentlink", exp: "FROM scratch" + eol + "EXPOSE 5000", fail: false})
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-04-10 20:11:28 -04:00
|
|
|
ref, subdir := getRefAndSubdir(c.frag)
|
2017-12-07 16:44:08 -05:00
|
|
|
r, err := cloneGitRepo(gitRepo{remote: gitDir, ref: ref, subdir: subdir})
|
2015-04-24 18:12:45 -04:00
|
|
|
|
|
|
|
if c.fail {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, ""))
|
2015-04-24 18:12:45 -04:00
|
|
|
continue
|
2017-12-08 14:51:10 -05:00
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-08 14:51:10 -05:00
|
|
|
defer os.RemoveAll(r)
|
|
|
|
if c.submodule {
|
2021-08-24 06:10:50 -04:00
|
|
|
b, err := os.ReadFile(filepath.Join(r, "sub/subfile"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("subcontents", string(b)))
|
2017-12-08 14:51:10 -05:00
|
|
|
} else {
|
|
|
|
_, err := os.Stat(filepath.Join(r, "sub/subfile"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
|
|
|
assert.Assert(t, os.IsNotExist(err))
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
b, err := os.ReadFile(filepath.Join(r, "Dockerfile"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(c.exp, string(b)))
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-26 13:07:04 -04:00
|
|
|
|
|
|
|
func TestValidGitTransport(t *testing.T) {
|
|
|
|
gitUrls := []string{
|
|
|
|
"git://github.com/docker/docker",
|
|
|
|
"git@github.com:docker/docker.git",
|
|
|
|
"git@bitbucket.org:atlassianlabs/atlassian-docker.git",
|
|
|
|
"https://github.com/docker/docker.git",
|
|
|
|
"http://github.com/docker/docker.git",
|
|
|
|
"http://github.com/docker/docker.git#branch",
|
|
|
|
"http://github.com/docker/docker.git#:dir",
|
|
|
|
}
|
|
|
|
incompleteGitUrls := []string{
|
|
|
|
"github.com/docker/docker",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, url := range gitUrls {
|
|
|
|
if !isGitTransport(url) {
|
|
|
|
t.Fatalf("%q should be detected as valid Git prefix", url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, url := range incompleteGitUrls {
|
|
|
|
if isGitTransport(url) {
|
|
|
|
t.Fatalf("%q should not be detected as valid Git prefix", url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 14:58:40 -05:00
|
|
|
|
|
|
|
func TestGitInvalidRef(t *testing.T) {
|
|
|
|
gitUrls := []string{
|
|
|
|
"git://github.com/moby/moby#--foo bar",
|
|
|
|
"git@github.com/moby/moby#--upload-pack=sleep;:",
|
|
|
|
"git@g.com:a/b.git#-B",
|
|
|
|
"git@g.com:a/b.git#with space",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, url := range gitUrls {
|
|
|
|
_, err := Clone(url)
|
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
assert.Check(t, is.Contains(strings.ToLower(err.Error()), "invalid refspec"))
|
|
|
|
}
|
|
|
|
}
|