Skip to content

Commit

Permalink
Merge pull request #1506 from hairyhenderson/clarify-git-url-docs
Browse files Browse the repository at this point in the history
Clarify git URL docs - stop implying subpaths starting with '//' are a good idea
  • Loading branch information
hairyhenderson authored Sep 25, 2022
2 parents 591a6ed + ccd1eab commit c0a43e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
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

0 comments on commit c0a43e4

Please sign in to comment.