-
Notifications
You must be signed in to change notification settings - Fork 262
Basic execution plugin module #1178
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
Merged
Merged
Changes from all commits
Commits
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
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,49 @@ | ||
| // +build e2e | ||
| // Copyright © 2020 The Tekton Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package e2e | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/tektoncd/cli/test/cli" | ||
| "gotest.tools/v3/assert" | ||
| "gotest.tools/v3/env" | ||
| "gotest.tools/v3/icmd" | ||
| ) | ||
|
|
||
| func TestTknPlugin(t *testing.T) { | ||
| tkn, err := cli.NewTknRunner("any-namespace") | ||
| assert.NilError(t, err) | ||
| currentpath, err := os.Getwd() | ||
| assert.NilError(t, err) | ||
| defer env.Patch(t, "TKN_PLUGINS_DIR", currentpath)() | ||
| t.Run("Success", func(t *testing.T) { | ||
| tkn.MustSucceed(t, "success") | ||
| tkn.MustSucceed(t, "success", "with", "args") | ||
| }) | ||
| t.Run("Failure", func(t *testing.T) { | ||
| tkn.Run("failure").Assert(t, icmd.Expected{ | ||
| ExitCode: 12, | ||
| }) | ||
| tkn.Run("failure", "with", "args").Assert(t, icmd.Expected{ | ||
| ExitCode: 12, | ||
| }) | ||
| tkn.Run("failure", "exit20").Assert(t, icmd.Expected{ | ||
| ExitCode: 20, | ||
| }) | ||
| }) | ||
| } |
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,6 @@ | ||
| #!/usr/bin/env bash | ||
| echo "This is a failure" | ||
| if [[ "$1" == "exit20" ]]; then | ||
| exit 20 | ||
| fi | ||
| exit 12 |
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,3 @@ | ||
| #!/usr/bin/env bash | ||
| echo "This is a success" | ||
| exit 0 |
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,118 @@ | ||
| /*Package env provides functions to test code that read environment variables | ||
| or the current working directory. | ||
| */ | ||
| package env // import "gotest.tools/v3/env" | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| "gotest.tools/v3/internal/cleanup" | ||
| ) | ||
|
|
||
| type helperT interface { | ||
| Helper() | ||
| } | ||
|
|
||
| // Patch changes the value of an environment variable, and returns a | ||
| // function which will reset the the value of that variable back to the | ||
| // previous state. | ||
| // | ||
| // When used with Go 1.14+ the unpatch function will be called automatically | ||
| // when the test ends, unless the TEST_NOCLEANUP env var is set to true. | ||
| func Patch(t assert.TestingT, key, value string) func() { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| oldValue, envVarExists := os.LookupEnv(key) | ||
| assert.NilError(t, os.Setenv(key, value)) | ||
| clean := func() { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| if !envVarExists { | ||
| assert.NilError(t, os.Unsetenv(key)) | ||
| return | ||
| } | ||
| assert.NilError(t, os.Setenv(key, oldValue)) | ||
| } | ||
| cleanup.Cleanup(t, clean) | ||
| return clean | ||
| } | ||
|
|
||
| // PatchAll sets the environment to env, and returns a function which will | ||
| // reset the environment back to the previous state. | ||
| // | ||
| // When used with Go 1.14+ the unpatch function will be called automatically | ||
| // when the test ends, unless the TEST_NOCLEANUP env var is set to true. | ||
| func PatchAll(t assert.TestingT, env map[string]string) func() { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| oldEnv := os.Environ() | ||
| os.Clearenv() | ||
|
|
||
| for key, value := range env { | ||
| assert.NilError(t, os.Setenv(key, value), "setenv %s=%s", key, value) | ||
| } | ||
| clean := func() { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| os.Clearenv() | ||
| for key, oldVal := range ToMap(oldEnv) { | ||
| assert.NilError(t, os.Setenv(key, oldVal), "setenv %s=%s", key, oldVal) | ||
| } | ||
| } | ||
| cleanup.Cleanup(t, clean) | ||
| return clean | ||
| } | ||
|
|
||
| // ToMap takes a list of strings in the format returned by os.Environ() and | ||
| // returns a mapping of keys to values. | ||
| func ToMap(env []string) map[string]string { | ||
| result := map[string]string{} | ||
| for _, raw := range env { | ||
| key, value := getParts(raw) | ||
| result[key] = value | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func getParts(raw string) (string, string) { | ||
| if raw == "" { | ||
| return "", "" | ||
| } | ||
| // Environment variables on windows can begin with = | ||
| // http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx | ||
| parts := strings.SplitN(raw[1:], "=", 2) | ||
| key := raw[:1] + parts[0] | ||
| if len(parts) == 1 { | ||
| return key, "" | ||
| } | ||
| return key, parts[1] | ||
| } | ||
|
|
||
| // ChangeWorkingDir to the directory, and return a function which restores the | ||
| // previous working directory. | ||
| // | ||
| // When used with Go 1.14+ the previous working directory will be restored | ||
| // automatically when the test ends, unless the TEST_NOCLEANUP env var is set to | ||
| // true. | ||
| func ChangeWorkingDir(t assert.TestingT, dir string) func() { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| cwd, err := os.Getwd() | ||
| assert.NilError(t, err) | ||
| assert.NilError(t, os.Chdir(dir)) | ||
| clean := func() { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| assert.NilError(t, os.Chdir(cwd)) | ||
| } | ||
| cleanup.Cleanup(t, clean) | ||
| return clean | ||
| } |
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,45 @@ | ||
| /*Package cleanup handles migration to and support for the Go 1.14+ | ||
| testing.TB.Cleanup() function. | ||
| */ | ||
| package cleanup | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
|
|
||
| "gotest.tools/v3/x/subtest" | ||
| ) | ||
|
|
||
| type cleanupT interface { | ||
| Cleanup(f func()) | ||
| } | ||
|
|
||
| type logT interface { | ||
| Log(...interface{}) | ||
| } | ||
|
|
||
| type helperT interface { | ||
| Helper() | ||
| } | ||
|
|
||
| var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true" | ||
|
|
||
| // Cleanup registers f as a cleanup function on t if any mechanisms are available. | ||
| // | ||
| // Skips registering f if TEST_NOCLEANUP is set to true. | ||
| func Cleanup(t logT, f func()) { | ||
| if ht, ok := t.(helperT); ok { | ||
| ht.Helper() | ||
| } | ||
| if noCleanup { | ||
| t.Log("skipping cleanup because TEST_NOCLEANUP was enabled.") | ||
| return | ||
| } | ||
| if ct, ok := t.(cleanupT); ok { | ||
| ct.Cleanup(f) | ||
| return | ||
| } | ||
| if tc, ok := t.(subtest.TestContext); ok { | ||
| tc.AddCleanup(f) | ||
| } | ||
| } |
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,84 @@ | ||
| /*Package subtest provides a TestContext to subtests which handles cleanup, and | ||
| provides a testing.TB, and context.Context. | ||
|
|
||
| This package was inspired by github.com/frankban/quicktest. | ||
| */ | ||
| package subtest // import "gotest.tools/v3/x/subtest" | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| ) | ||
|
|
||
| type testcase struct { | ||
| testing.TB | ||
| ctx context.Context | ||
| cleanupFuncs []cleanupFunc | ||
| } | ||
|
|
||
| type cleanupFunc func() | ||
|
|
||
| func (tc *testcase) Ctx() context.Context { | ||
| if tc.ctx == nil { | ||
| var cancel func() | ||
| tc.ctx, cancel = context.WithCancel(context.Background()) | ||
| tc.AddCleanup(cancel) | ||
| } | ||
| return tc.ctx | ||
| } | ||
|
|
||
| // cleanup runs all cleanup functions. Functions are run in the opposite order | ||
| // in which they were added. Cleanup is called automatically before Run exits. | ||
| func (tc *testcase) cleanup() { | ||
| for _, f := range tc.cleanupFuncs { | ||
| // Defer all cleanup functions so they all run even if one calls | ||
| // t.FailNow() or panics. Deferring them also runs them in reverse order. | ||
| defer f() | ||
| } | ||
| tc.cleanupFuncs = nil | ||
| } | ||
|
|
||
| func (tc *testcase) AddCleanup(f func()) { | ||
| tc.cleanupFuncs = append(tc.cleanupFuncs, f) | ||
| } | ||
|
|
||
| func (tc *testcase) Parallel() { | ||
| tp, ok := tc.TB.(parallel) | ||
| if !ok { | ||
| panic("Parallel called with a testing.B") | ||
| } | ||
| tp.Parallel() | ||
| } | ||
|
|
||
| type parallel interface { | ||
| Parallel() | ||
| } | ||
|
|
||
| // Run a subtest. When subtest exits, every cleanup function added with | ||
| // TestContext.AddCleanup will be run. | ||
| func Run(t *testing.T, name string, subtest func(t TestContext)) bool { | ||
| return t.Run(name, func(t *testing.T) { | ||
| tc := &testcase{TB: t} | ||
| defer tc.cleanup() | ||
| subtest(tc) | ||
| }) | ||
| } | ||
|
|
||
| // TestContext provides a testing.TB and a context.Context for a test case. | ||
| type TestContext interface { | ||
| testing.TB | ||
| // AddCleanup function which will be run when before Run returns. | ||
| // | ||
| // Deprecated: Go 1.14+ now includes a testing.TB.Cleanup(func()) which | ||
| // should be used instead. AddCleanup will be removed in a future release. | ||
| AddCleanup(f func()) | ||
| // Ctx returns a context for the test case. Multiple calls from the same subtest | ||
| // will return the same context. The context is cancelled when Run | ||
| // returns. | ||
| Ctx() context.Context | ||
| // Parallel calls t.Parallel on the testing.TB. Panics if testing.TB does | ||
| // not implement Parallel. | ||
| Parallel() | ||
| } | ||
|
|
||
| var _ TestContext = &testcase{} |
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.
Why
tkn.Find(args)doesn't return an error when an invalid command is passed?I tried with another CLI
Findreturns err when the command is invalid, it happens only withtkn. 😄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.
@sm43 my guess is the
suggestpart but I am not sure 😓