-
Notifications
You must be signed in to change notification settings - Fork 12
add template funcs #212
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
Open
coryb
wants to merge
2
commits into
openllb:master
Choose a base branch
from
coryb:template-funcs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add template funcs #212
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package codegen | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTemplateFuncs(t *testing.T) { | ||
for _, tt := range []struct { | ||
templateFunc string | ||
ref string | ||
expected string | ||
}{ | ||
{"dockerDomain", "docker.io/library/busybox:latest", "docker.io"}, | ||
{"dockerDomain", "docker.io/library/busybox", "docker.io"}, | ||
{"dockerDomain", "docker.io/library/busybox:tag", "docker.io"}, | ||
{"dockerDomain", "busybox:tag", ""}, | ||
{"dockerDomain", "busybox", ""}, | ||
{"dockerDomain", "host.com:8443/library/busybox:tag", "host.com:8443"}, | ||
{"dockerDomain", "host.com:8443/library/busybox", "host.com:8443"}, | ||
{"dockerDomain", "library/busybox:tag", ""}, | ||
{"dockerDomain", "library/busybox", ""}, | ||
|
||
{"dockerPath", "docker.io/library/busybox:latest", "library/busybox"}, | ||
{"dockerPath", "docker.io/library/busybox", "library/busybox"}, | ||
{"dockerPath", "docker.io/library/busybox:tag", "library/busybox"}, | ||
{"dockerPath", "busybox:tag", "busybox"}, | ||
{"dockerPath", "busybox", "busybox"}, | ||
{"dockerPath", "host.com:8443/library/busybox:tag", "library/busybox"}, | ||
{"dockerPath", "host.com:8443/library/busybox", "library/busybox"}, | ||
{"dockerPath", "library/busybox:tag", "library/busybox"}, | ||
{"dockerPath", "library/busybox", "library/busybox"}, | ||
|
||
{"dockerRepository", "docker.io/library/busybox:latest", "docker.io/library/busybox"}, | ||
{"dockerRepository", "docker.io/library/busybox", "docker.io/library/busybox"}, | ||
{"dockerRepository", "docker.io/library/busybox:tag", "docker.io/library/busybox"}, | ||
{"dockerRepository", "busybox:tag", "busybox"}, | ||
{"dockerRepository", "busybox", "busybox"}, | ||
{"dockerRepository", "host.com:8443/library/busybox:tag", "host.com:8443/library/busybox"}, | ||
{"dockerRepository", "host.com:8443/library/busybox", "host.com:8443/library/busybox"}, | ||
{"dockerRepository", "library/busybox:tag", "library/busybox"}, | ||
{"dockerRepository", "library/busybox", "library/busybox"}, | ||
|
||
{"dockerTag", "docker.io/library/busybox:latest", "latest"}, | ||
{"dockerTag", "docker.io/library/busybox", "latest"}, | ||
{"dockerTag", "docker.io/library/busybox:tag", "tag"}, | ||
{"dockerTag", "busybox:tag", "tag"}, | ||
{"dockerTag", "busybox", "latest"}, | ||
{"dockerTag", "host.com:8443/library/busybox:tag", "tag"}, | ||
{"dockerTag", "host.com:8443/library/busybox", "latest"}, | ||
{"dockerTag", "library/busybox:tag", "tag"}, | ||
{"dockerTag", "library/busybox", "latest"}, | ||
} { | ||
tmpl, err := template.New("hlb").Funcs(templateFuncs()).Parse( | ||
fmt.Sprintf(`{{%s .}}`, tt.templateFunc), | ||
) | ||
require.NoError(t, err) | ||
buf := bytes.NewBufferString("") | ||
err = tmpl.Execute(buf, tt.ref) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expected, buf.String(), fmt.Sprintf("{{%s %q}} == %q", tt.templateFunc, tt.ref, tt.expected)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it have to be
dockerFoo
? Could this beimageRefDomain
or something a bit more OCI-friendly? 😉There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was chatting on slack, but I was suggesting since we had interpolation support in heredocs, I prefer if we use regular string functions rather than template functions.
Along with the array support proposal, you could do something like: