Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify git URL docs - stop implying subpaths starting with '//' are a good idea #1506

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/content/datasources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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://[email protected]/hairyhenderson/go-which' -i '{{ len (ds "which") }}'
$ gomplate -d 'which=git+ssh://[email protected]/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
```

Expand Down
52 changes: 37 additions & 15 deletions internal/tests/integration/datasources_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down