From ccd1eabc66d3fef316cee95dce744260d7c891dd Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 25 Sep 2022 18:58:35 -0400 Subject: [PATCH] Clarify git URL docs - stop implying subpaths starting with '//' are a good idea Signed-off-by: Dave Henderson --- docs/content/datasources.md | 6 +-- .../tests/integration/datasources_git_test.go | 52 +++++++++++++------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/docs/content/datasources.md b/docs/content/datasources.md index aac4b0797..8531ad378 100644 --- a/docs/content/datasources.md +++ b/docs/content/datasources.md @@ -491,7 +491,7 @@ namespace is: env Accessing a file from a local repo (using arguments): ```console -$ gomplate -d which=git+file:///repos/go-which -i 'GOPATH on Windows is {{ (datasource "which" "//appveyor.yml").environment.GOPATH }}' +$ gomplate -d which=git+file:///repos/go-which/ -i 'GOPATH on Windows is {{ (datasource "which" ".//appveyor.yml").environment.GOPATH }}' GOPATH on Windows is c:\gopath ``` @@ -503,13 +503,13 @@ $ gomplate -d 'cmd=git+https://github.com/hairyhenderson/go-which//cmd/which#ref Authenticating with the SSH Agent ```console -$ gomplate -d 'which=git+ssh://git@github.com/hairyhenderson/go-which' -i '{{ len (ds "which") }}' +$ gomplate -d 'which=git+ssh://git@github.com/hairyhenderson/go-which/' -i '{{ len (ds "which") }}' 18 ``` Using arguments to specify different repos ```console -$ gomplate -d 'hairyhenderson=git+https://github.com/hairyhenderson' -i '{{ (ds "hairyhenderson" "/gomplate//docs-src/content/functions/env.yml").ns }}' +$ gomplate -d 'hairyhenderson=git+https://github.com/hairyhenderson/' -i '{{ (ds "hairyhenderson" "gomplate//docs-src/content/functions/env.yml").ns }}' env ``` diff --git a/internal/tests/integration/datasources_git_test.go b/internal/tests/integration/datasources_git_test.go index 5fcce4f60..139c3ba7d 100644 --- a/internal/tests/integration/datasources_git_test.go +++ b/internal/tests/integration/datasources_git_test.go @@ -15,25 +15,29 @@ import ( func setupDatasourcesGitTest(t *testing.T) *fs.Dir { tmpDir := fs.NewDir(t, "gomplate-inttests", - fs.WithFiles(map[string]string{ - "config.json": `{"foo": {"bar": "baz"}}`, - }), + fs.WithDir("repo", + fs.WithFiles(map[string]string{ + "config.json": `{"foo": {"bar": "baz"}}`, + "jsonfile": `{"foo": {"bar": "baz"}}`, + }), + fs.WithDir("subdir", + fs.WithFiles(map[string]string{ + "foo.txt": "hello world", + "bar.json": `{"qux": "quux"}`, + }), + ), + ), ) t.Cleanup(tmpDir.Remove) repoPath := tmpDir.Join("repo") - result := icmd.RunCommand("git", "init", repoPath) - result.Assert(t, icmd.Expected{ExitCode: 0, Out: "Initialized empty Git repository"}) - - result = icmd.RunCommand("mv", tmpDir.Join("config.json"), repoPath) - result.Assert(t, icmd.Expected{ExitCode: 0}) - - result = icmd.RunCmd(icmd.Command("git", "add", "config.json"), icmd.Dir(repoPath)) - result.Assert(t, icmd.Expected{ExitCode: 0}) - - result = icmd.RunCmd(icmd.Command("git", "commit", "-m", "Initial commit"), icmd.Dir(repoPath)) - result.Assert(t, icmd.Expected{ExitCode: 0}) + icmd.RunCommand("git", "init", repoPath). + Assert(t, icmd.Expected{Out: "Initialized empty Git repository"}) + icmd.RunCmd(icmd.Command("git", "add", "config.json"), icmd.Dir(repoPath)).Assert(t, icmd.Expected{}) + icmd.RunCmd(icmd.Command("git", "add", "jsonfile"), icmd.Dir(repoPath)).Assert(t, icmd.Expected{}) + icmd.RunCmd(icmd.Command("git", "add", "subdir"), icmd.Dir(repoPath)).Assert(t, icmd.Expected{}) + icmd.RunCmd(icmd.Command("git", "commit", "-m", "Initial commit"), icmd.Dir(repoPath)).Assert(t, icmd.Expected{}) return tmpDir } @@ -84,17 +88,35 @@ func TestDatasources_GitFileDatasource(t *testing.T) { ).run() assertSuccess(t, o, e, err, "baz") + // subpath beginning with // is an antipattern, but should work for + // backwards compatibility, params from subpath are used o, e, err = cmd(t, "-d", "repo=git+file://"+u, - "-i", `{{ (datasource "repo" "//config.json?type=application/json" ).foo.bar }}`, + "-i", `{{ (datasource "repo" "//jsonfile?type=application/json" ).foo.bar }}`, ).run() assertSuccess(t, o, e, err, "baz") + // subpath beginning with // is an antipattern, but should work for + // backwards compatibility o, e, err = cmd(t, "-d", "repo=git+file://"+u, "-i", `{{ (datasource "repo" "//config.json" ).foo.bar }}`, ).run() assertSuccess(t, o, e, err, "baz") + + // subdir in datasource URL, relative subpath + o, e, err = cmd(t, + "-d", "repo=git+file://"+u+"//subdir/", + "-i", `{{ include "repo" "foo.txt" }}`, + ).run() + assertSuccess(t, o, e, err, "hello world") + + // ds URL ends with /, relative subpath beginning with .// is preferred + o, e, err = cmd(t, + "-d", "repo=git+file://"+u+"/", + "-i", `{{ include "repo" ".//subdir/foo.txt" }}`, + ).run() + assertSuccess(t, o, e, err, "hello world") } func TestDatasources_GitDatasource(t *testing.T) {