moby/builder/dockerfile/copy_test.go
Sebastiaan van Stijn 632fc235d6
builder/dockerfile: use string-literals for easier grep'ing
Use string-literal for reduce escaped quotes, which makes for easier grepping.
While at it, also changed http -> https to keep some linters at bay.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 202907b14c)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-15 00:48:58 +02:00

147 lines
2.9 KiB
Go

package dockerfile // import "github.com/docker/docker/builder/dockerfile"
import (
"net/http"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/fs"
)
func TestIsExistingDirectory(t *testing.T) {
tmpfile := fs.NewFile(t, "file-exists-test", fs.WithContent("something"))
defer tmpfile.Remove()
tmpdir := fs.NewDir(t, "dir-exists-test")
defer tmpdir.Remove()
var testcases = []struct {
doc string
path string
expected bool
}{
{
doc: "directory exists",
path: tmpdir.Path(),
expected: true,
},
{
doc: "path doesn't exist",
path: "/bogus/path/does/not/exist",
expected: false,
},
{
doc: "file exists",
path: tmpfile.Path(),
expected: false,
},
}
for _, testcase := range testcases {
result, err := isExistingDirectory(testcase.path)
if !assert.Check(t, err) {
continue
}
assert.Check(t, is.Equal(testcase.expected, result), testcase.doc)
}
}
func TestGetFilenameForDownload(t *testing.T) {
var testcases = []struct {
path string
disposition string
expected string
}{
{
path: "https://www.example.com/",
expected: "",
},
{
path: "https://www.example.com/xyz",
expected: "xyz",
},
{
path: "https://www.example.com/xyz.html",
expected: "xyz.html",
},
{
path: "https://www.example.com/xyz/",
expected: "",
},
{
path: "https://www.example.com/xyz/uvw",
expected: "uvw",
},
{
path: "https://www.example.com/xyz/uvw.html",
expected: "uvw.html",
},
{
path: "https://www.example.com/xyz/uvw/",
expected: "",
},
{
path: "/",
expected: "",
},
{
path: "/xyz",
expected: "xyz",
},
{
path: "/xyz.html",
expected: "xyz.html",
},
{
path: "/xyz/",
expected: "",
},
{
path: "/xyz/",
disposition: "attachment; filename=xyz.html",
expected: "xyz.html",
},
{
disposition: "",
expected: "",
},
{
disposition: "attachment; filename=xyz",
expected: "xyz",
},
{
disposition: "attachment; filename=xyz.html",
expected: "xyz.html",
},
{
disposition: `attachment; filename="xyz"`,
expected: "xyz",
},
{
disposition: `attachment; filename="xyz.html"`,
expected: "xyz.html",
},
{
disposition: `attachment; filename="/xyz.html"`,
expected: "xyz.html",
},
{
disposition: `attachment; filename="/xyz/uvw"`,
expected: "uvw",
},
{
disposition: `attachment; filename="Naïve file.txt"`,
expected: "Naïve file.txt",
},
}
for _, testcase := range testcases {
resp := http.Response{
Header: make(map[string][]string),
}
if testcase.disposition != "" {
resp.Header.Add("Content-Disposition", testcase.disposition)
}
filename := getFilenameForDownload(testcase.path, &resp)
assert.Check(t, is.Equal(testcase.expected, filename))
}
}