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 (
|
2022-07-28 13:17:42 -04:00
|
|
|
"bytes"
|
2015-04-21 17:24:29 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-07-28 13:17:42 -04:00
|
|
|
"net/http/cgi"
|
2015-04-21 17:24:29 -04:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-04-24 18:12:45 -04:00
|
|
|
"os"
|
2022-07-28 13:17:42 -04: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 {
|
2022-10-13 17:28:02 -04:00
|
|
|
b, err := gitRepo{}.gitWithinDir("", "config", "--get", name)
|
2016-05-31 22:36:25 -04:00
|
|
|
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) {
|
2022-07-27 15:46:46 -04:00
|
|
|
root := t.TempDir()
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2022-07-28 13:17:42 -04:00
|
|
|
gitpath, err := exec.LookPath("git")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
gitversion, _ := exec.Command(gitpath, "version").CombinedOutput()
|
|
|
|
t.Logf("%s", gitversion) // E.g. "git version 2.30.2"
|
|
|
|
|
|
|
|
// Serve all repositories under root using the Smart HTTP protocol so
|
|
|
|
// they can be cloned. The Dumb HTTP protocol is incompatible with
|
|
|
|
// shallow cloning but we unconditionally shallow-clone submodules, and
|
|
|
|
// we explicitly disable the file protocol.
|
|
|
|
// (Another option would be to use `git daemon` and the Git protocol,
|
|
|
|
// but that listens on a fixed port number which is a recipe for
|
|
|
|
// disaster in CI. Funnily enough, `git daemon --port=0` works but there
|
|
|
|
// is no easy way to discover which port got picked!)
|
|
|
|
|
|
|
|
// Associate git-http-backend logs with the current (sub)test.
|
|
|
|
// Incompatible with parallel subtests.
|
|
|
|
currentSubtest := t
|
|
|
|
githttp := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var logs bytes.Buffer
|
|
|
|
(&cgi.Handler{
|
|
|
|
Path: gitpath,
|
|
|
|
Args: []string{"http-backend"},
|
|
|
|
Dir: root,
|
|
|
|
Env: []string{
|
|
|
|
"GIT_PROJECT_ROOT=" + root,
|
|
|
|
"GIT_HTTP_EXPORT_ALL=1",
|
|
|
|
},
|
|
|
|
Stderr: &logs,
|
|
|
|
}).ServeHTTP(w, r)
|
|
|
|
if logs.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
line, err := logs.ReadString('\n')
|
|
|
|
currentSubtest.Log("git-http-backend: " + line)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
server := httptest.NewServer(&githttp)
|
|
|
|
defer server.Close()
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:46:46 -04:00
|
|
|
must := func(out []byte, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if len(out) > 0 {
|
|
|
|
t.Logf("%s", out)
|
|
|
|
}
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2022-07-27 15:46:46 -04:00
|
|
|
gitDir := filepath.Join(root, "repo")
|
2022-10-13 17:28:02 -04:00
|
|
|
must(gitRepo{}.gitWithinDir(root, "-c", "init.defaultBranch=master", "init", gitDir))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "config", "user.email", "test@docker.com"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "config", "user.name", "Docker test"))
|
2022-07-27 15:46:46 -04:00
|
|
|
assert.NilError(t, os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644))
|
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))
|
2022-07-27 15:46:46 -04:00
|
|
|
assert.NilError(t, os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644))
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
if runtime.GOOS != "windows" {
|
2022-07-27 15:46:46 -04:00
|
|
|
assert.NilError(t, os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")))
|
|
|
|
assert.NilError(t, os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")))
|
2015-06-01 14:41:45 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:28:02 -04:00
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "add", "-A"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "commit", "-am", "First commit"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "checkout", "-b", "test"))
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2022-07-27 15:46:46 -04:00
|
|
|
assert.NilError(t, os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644))
|
|
|
|
assert.NilError(t, os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644))
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2022-10-13 17:28:02 -04:00
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "add", "-A"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "commit", "-am", "Branch commit"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "checkout", "master"))
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-12-07 16:44:08 -05:00
|
|
|
// set up submodule
|
|
|
|
subrepoDir := filepath.Join(root, "subrepo")
|
2022-10-13 17:28:02 -04:00
|
|
|
must(gitRepo{}.gitWithinDir(root, "-c", "init.defaultBranch=master", "init", subrepoDir))
|
|
|
|
must(gitRepo{}.gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com"))
|
|
|
|
must(gitRepo{}.gitWithinDir(subrepoDir, "config", "user.name", "Docker test"))
|
2017-12-07 16:44:08 -05:00
|
|
|
|
2022-07-27 15:46:46 -04:00
|
|
|
assert.NilError(t, os.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644))
|
2017-12-07 16:44:08 -05:00
|
|
|
|
2022-10-13 17:28:02 -04:00
|
|
|
must(gitRepo{}.gitWithinDir(subrepoDir, "add", "-A"))
|
|
|
|
must(gitRepo{}.gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial"))
|
2017-12-07 16:44:08 -05:00
|
|
|
|
2022-10-13 17:28:02 -04:00
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "submodule", "add", server.URL+"/subrepo", "sub"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "add", "-A"))
|
|
|
|
must(gitRepo{}.gitWithinDir(gitDir, "commit", "-am", "With submodule"))
|
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 {
|
2022-07-27 15:46:46 -04:00
|
|
|
t.Run(c.frag, func(t *testing.T) {
|
2022-07-28 13:17:42 -04:00
|
|
|
currentSubtest = t
|
2022-07-27 15:46:46 -04:00
|
|
|
ref, subdir := getRefAndSubdir(c.frag)
|
2022-10-13 17:28:02 -04:00
|
|
|
r, err := gitRepo{remote: server.URL + "/repo", ref: ref, subdir: subdir}.clone()
|
2022-07-27 15:46:46 -04:00
|
|
|
|
|
|
|
if c.fail {
|
|
|
|
assert.Check(t, is.ErrorContains(err, ""))
|
|
|
|
return
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2022-07-27 15:46:46 -04:00
|
|
|
defer os.RemoveAll(r)
|
|
|
|
if c.submodule {
|
|
|
|
b, err := os.ReadFile(filepath.Join(r, "sub/subfile"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("subcontents", string(b)))
|
|
|
|
} else {
|
|
|
|
_, err := os.Stat(filepath.Join(r, "sub/subfile"))
|
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
|
|
|
assert.Assert(t, os.IsNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := os.ReadFile(filepath.Join(r, "Dockerfile"))
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
}
|