2023-03-06 02:28:31 +00:00
// Copyright 2013-2023 The Cobra Authors
2022-09-16 13:55:56 +02:00
//
// 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.
2017-10-31 19:57:17 +01:00
package cobra
import (
2021-12-07 14:38:00 -08:00
"fmt"
2017-10-31 19:57:17 +01:00
"strings"
"testing"
)
2025-04-26 20:13:18 +00:00
// getCommand returns a Command object configured based on the provided PositionalArgs and a boolean indicating whether to include valid arguments.
// If withValid is true, the returned command will have ValidArgs set to ["one", "two", "three"].
// The Run field of the Command is initialized to emptyRun, which does nothing when called.
// Args determines what arguments are valid for this command.
2021-11-16 22:20:18 +00:00
func getCommand ( args PositionalArgs , withValid bool ) * Command {
c := & Command {
Use : "c" ,
Args : args ,
Run : emptyRun ,
}
if withValid {
c . ValidArgs = [ ] string { "one" , "two" , "three" }
}
return c
}
2017-10-31 19:57:17 +01:00
2025-04-26 20:13:18 +00:00
// expectSuccess checks if the provided output and error are as expected. If output is not empty, it logs an error. If there is an error, it fails the test with a fatal error.
// Parameters:
// - output: The string output to check.
// - err: The error to check.
// - t: The testing.T instance used for logging errors and failing tests.
// Returns: no value.
2021-11-16 22:20:18 +00:00
func expectSuccess ( output string , err error , t * testing . T ) {
2017-10-31 19:57:17 +01:00
if output != "" {
2021-11-16 22:20:18 +00:00
t . Errorf ( "Unexpected output: %v" , output )
2017-10-31 19:57:17 +01:00
}
if err != nil {
t . Fatalf ( "Unexpected error: %v" , err )
}
}
2025-04-26 20:13:18 +00:00
// validOnlyWithInvalidArgs checks if the given error is not nil and has the expected error message.
// It takes an error and a testing.T instance as parameters. If the error is nil, it calls t.Fatal with a message indicating that an error was expected.
// It then compares the actual error message with the expected one and calls t.Errorf if they do not match.
2022-09-10 15:33:34 +02:00
func validOnlyWithInvalidArgs ( err error , t * testing . T ) {
2017-10-31 19:57:17 +01:00
if err == nil {
t . Fatal ( "Expected an error" )
}
2021-11-16 22:20:18 +00:00
got := err . Error ( )
expected := ` invalid argument "a" for "c" `
if got != expected {
t . Errorf ( "Expected: %q, got: %q" , expected , got )
}
}
2017-10-31 19:57:17 +01:00
2025-04-26 20:13:18 +00:00
// noArgsWithArgs checks if an error is returned when calling a function with no arguments and the specified argument.
// It asserts that the error message matches the expected value for the given command.
// If no error is returned, it calls t.Fatal with an appropriate message.
// Parameters:
// - err: The error to check.
// - t: A testing.T instance used for assertions.
// - arg: The argument passed to the function that triggered the error.
2022-09-10 15:33:34 +02:00
func noArgsWithArgs ( err error , t * testing . T , arg string ) {
2021-11-16 22:20:18 +00:00
if err == nil {
t . Fatal ( "Expected an error" )
}
2017-10-31 19:57:17 +01:00
got := err . Error ( )
2022-09-10 15:33:34 +02:00
expected := ` unknown command " ` + arg + ` " for "c" `
2017-10-31 19:57:17 +01:00
if got != expected {
t . Errorf ( "Expected: %q, got: %q" , expected , got )
}
}
2025-04-26 20:13:18 +00:00
// minimumNArgsWithLessArgs checks if the provided error is due to a function requiring at least two arguments but receiving fewer.
//
// Parameters:
// - err: The error returned by the function.
// - t: A testing.T instance used for assertions.
//
// This function asserts that the error message matches the expected "requires at least 2 arg(s), only received 1".
// If the assertion fails, it calls t.Fatalf with a formatted error message.
2021-11-16 22:20:18 +00:00
func minimumNArgsWithLessArgs ( err error , t * testing . T ) {
if err == nil {
t . Fatal ( "Expected an error" )
}
got := err . Error ( )
expected := "requires at least 2 arg(s), only received 1"
if got != expected {
t . Fatalf ( "Expected %q, got %q" , expected , got )
2017-10-31 19:57:17 +01:00
}
2021-11-16 22:20:18 +00:00
}
2017-10-31 19:57:17 +01:00
2025-04-26 20:13:18 +00:00
// maximumNArgsWithMoreArgs checks if the error is related to accepting more arguments than allowed.
// It expects an error and a testing.T instance. If no error is received, it fails the test with a fatal error.
// It then compares the expected error message with the actual one; if they don't match, it fails the test with a fatal error.
// Parameters:
// - err: The error to be checked.
// - t: The testing.T instance used for assertions and logging.
// Returns:
// - No return value.
2021-11-16 22:20:18 +00:00
func maximumNArgsWithMoreArgs ( err error , t * testing . T ) {
if err == nil {
t . Fatal ( "Expected an error" )
2017-10-31 19:57:17 +01:00
}
2021-11-16 22:20:18 +00:00
got := err . Error ( )
expected := "accepts at most 2 arg(s), received 3"
if got != expected {
t . Fatalf ( "Expected %q, got %q" , expected , got )
2017-10-31 19:57:17 +01:00
}
}
2025-04-26 20:13:18 +00:00
// exactArgsWithInvalidCount checks if the given error indicates that a function expects exactly two arguments but received three.
//
// Parameters:
// - err: The error to check.
// - t: The testing.T instance used for assertions.
//
// Returns:
// This function does not return any value.
//
// Errors:
// - If err is nil, this function calls t.Fatal with an "Expected an error" message.
2021-11-16 22:20:18 +00:00
func exactArgsWithInvalidCount ( err error , t * testing . T ) {
if err == nil {
t . Fatal ( "Expected an error" )
}
got := err . Error ( )
expected := "accepts 2 arg(s), received 3"
if got != expected {
t . Fatalf ( "Expected %q, got %q" , expected , got )
2017-10-31 19:57:17 +01:00
}
2021-11-16 22:20:18 +00:00
}
2017-10-31 19:57:17 +01:00
2025-04-26 20:13:18 +00:00
// rangeArgsWithInvalidCount checks if the provided error is related to invalid argument count.
// It expects an error indicating that a function accepts between 2 and 4 arguments,
// but received only one. If the error does not match this expectation or if there is no error, it will call t.Fatal.
2021-11-16 22:20:18 +00:00
func rangeArgsWithInvalidCount ( err error , t * testing . T ) {
2017-10-31 19:57:17 +01:00
if err == nil {
t . Fatal ( "Expected an error" )
}
got := err . Error ( )
2021-11-16 22:20:18 +00:00
expected := "accepts between 2 and 4 arg(s), received 1"
2017-10-31 19:57:17 +01:00
if got != expected {
2021-11-16 22:20:18 +00:00
t . Fatalf ( "Expected %q, got %q" , expected , got )
2017-10-31 19:57:17 +01:00
}
}
2022-09-10 15:33:34 +02:00
// NoArgs
2025-04-26 20:13:18 +00:00
// TestNoArgs tests the behavior of the command when no arguments are provided. It verifies that the command executes successfully without any errors.
2021-11-16 22:20:18 +00:00
func TestNoArgs ( t * testing . T ) {
c := getCommand ( NoArgs , false )
output , err := executeCommand ( c )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestNoArgs_WithArgs tests the command execution when it expects no arguments but receives one.
// It creates a command with NoArgs flag and executes it with an argument "one".
// The function checks if the error returned is as expected for a no-arguments command receiving an argument.
2022-09-10 15:33:34 +02:00
func TestNoArgs_WithArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( NoArgs , false )
2022-09-10 15:33:34 +02:00
_ , err := executeCommand ( c , "one" )
noArgsWithArgs ( err , t , "one" )
}
2025-04-26 20:13:18 +00:00
// TestNoArgs_WithValid_WithArgs tests the behavior of a command with no arguments when provided with valid and unexpected arguments.
// It checks if the command execution returns an error for an unexpected argument.
2022-09-10 15:33:34 +02:00
func TestNoArgs_WithValid_WithArgs ( t * testing . T ) {
c := getCommand ( NoArgs , true )
_ , err := executeCommand ( c , "one" )
noArgsWithArgs ( err , t , "one" )
}
2025-04-26 20:13:18 +00:00
// TestNoArgs_WithValid_WithInvalidArgs tests the NoArgs command with valid and invalid arguments.
2022-09-10 15:33:34 +02:00
func TestNoArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( NoArgs , true )
_ , err := executeCommand ( c , "a" )
noArgsWithArgs ( err , t , "a" )
}
2025-04-26 20:13:18 +00:00
// TestNoArgs_WithValidOnly_WithInvalidArgs tests the execution of a command with both valid and invalid arguments when OnlyValidArgs and NoArgs matchers are used.
// It asserts that executing the command with an argument results in an error as expected.
// The test uses the getCommand function to create a command with specific matchers and then checks if the error returned by executeCommand is valid using validOnlyWithInvalidArgs.
2022-09-10 15:33:34 +02:00
func TestNoArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , NoArgs ) , true )
_ , err := executeCommand ( c , "a" )
validOnlyWithInvalidArgs ( err , t )
2021-11-16 22:20:18 +00:00
}
2022-09-10 15:33:34 +02:00
// OnlyValidArgs
2025-04-26 20:13:18 +00:00
// TestOnlyValidArgs tests the command with valid arguments.
2021-11-16 22:20:18 +00:00
func TestOnlyValidArgs ( t * testing . T ) {
c := getCommand ( OnlyValidArgs , true )
output , err := executeCommand ( c , "one" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestOnlyValidArgs_WithInvalidArgs tests the behavior of the command when invalid arguments are provided.
// It verifies that an error is returned when executing the command with one invalid argument.
2022-09-10 15:33:34 +02:00
func TestOnlyValidArgs_WithInvalidArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( OnlyValidArgs , true )
_ , err := executeCommand ( c , "a" )
2022-09-10 15:33:34 +02:00
validOnlyWithInvalidArgs ( err , t )
2021-11-16 22:20:18 +00:00
}
2022-09-10 15:33:34 +02:00
// ArbitraryArgs
2025-04-26 20:13:18 +00:00
// TestArbitraryArgs tests the execution of a command with arbitrary arguments.
// It creates a command with the ArbitraryArgs flag set and executes it with two arguments "a" and "b".
// It then checks if the command executed successfully by expecting no errors and a successful output.
2017-10-31 19:57:17 +01:00
func TestArbitraryArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( ArbitraryArgs , false )
2017-10-31 19:57:17 +01:00
output , err := executeCommand ( c , "a" , "b" )
2021-11-16 22:20:18 +00:00
expectSuccess ( output , err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestArbitraryArgs_WithValid tests the execution of a command with arbitrary arguments when they are valid.
// It verifies that the command executes successfully and returns expected output without errors.
2022-09-10 15:33:34 +02:00
func TestArbitraryArgs_WithValid ( t * testing . T ) {
c := getCommand ( ArbitraryArgs , true )
output , err := executeCommand ( c , "one" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestArbitraryArgs_WithValid_WithInvalidArgs tests the execution of a command with arbitrary arguments, including both valid and invalid cases.
// It verifies that the command executes successfully with valid arguments and handles errors for invalid arguments correctly.
// The function takes a testing.T instance to run assertions and checks the output and error of the executed command.
2022-09-10 15:33:34 +02:00
func TestArbitraryArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( ArbitraryArgs , true )
output , err := executeCommand ( c , "a" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestArbitraryArgs_WithValidOnly_WithInvalidArgs tests the command execution when only valid arguments are provided along with arbitrary arguments.
// It expects an error when invalid arguments are present during execution.
2022-09-10 15:33:34 +02:00
func TestArbitraryArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , ArbitraryArgs ) , true )
_ , err := executeCommand ( c , "a" )
validOnlyWithInvalidArgs ( err , t )
}
// MinimumNArgs
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs tests the MinimumNArgs command with a minimum of 2 arguments.
2017-10-31 19:57:17 +01:00
func TestMinimumNArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( MinimumNArgs ( 2 ) , false )
2017-10-31 19:57:17 +01:00
output , err := executeCommand ( c , "a" , "b" , "c" )
2021-11-16 22:20:18 +00:00
expectSuccess ( output , err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithValid tests the execution of a command with at least two arguments.
// It asserts that the command executes successfully when provided with valid arguments.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithValid ( t * testing . T ) {
c := getCommand ( MinimumNArgs ( 2 ) , true )
output , err := executeCommand ( c , "one" , "three" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithValid checks that the MinimumNArgs command works correctly with valid arguments.
// It tests both a successful execution and an error case when invalid arguments are provided.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithValid__WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MinimumNArgs ( 2 ) , true )
output , err := executeCommand ( c , "a" , "b" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithValidOnly_WithInvalidArgs tests the behavior of the command when it expects a minimum number of arguments (2) and only valid args, but receives invalid ones.
// It validates that the command execution fails with an error indicating invalid arguments.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , MinimumNArgs ( 2 ) ) , true )
_ , err := executeCommand ( c , "a" , "b" )
validOnlyWithInvalidArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithLessArgs tests the MinimumNArgs function when provided with fewer arguments than expected.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithLessArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( MinimumNArgs ( 2 ) , false )
2017-10-31 19:57:17 +01:00
_ , err := executeCommand ( c , "a" )
2021-11-16 22:20:18 +00:00
minimumNArgsWithLessArgs ( err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithLessArgs_WithValid tests the MinimumNArgs function with fewer arguments than expected.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithLessArgs_WithValid ( t * testing . T ) {
c := getCommand ( MinimumNArgs ( 2 ) , true )
_ , err := executeCommand ( c , "one" )
minimumNArgsWithLessArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs tests the MinimumNArgs validator with fewer arguments than expected.
// It verifies both valid and invalid cases to ensure proper error handling.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MinimumNArgs ( 2 ) , true )
_ , err := executeCommand ( c , "a" )
minimumNArgsWithLessArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs tests the behavior of a command when executed with less than the minimum required arguments and only valid args are allowed.
// It verifies that the command returns an error indicating invalid argument count when called with insufficient arguments.
2022-09-10 15:33:34 +02:00
func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , MinimumNArgs ( 2 ) ) , true )
_ , err := executeCommand ( c , "a" )
validOnlyWithInvalidArgs ( err , t )
}
// MaximumNArgs
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs tests the execution of a command with a maximum number of arguments.
// It verifies that the command is executed successfully with up to 3 arguments and fails when more than 3 arguments are provided.
2017-10-31 19:57:17 +01:00
func TestMaximumNArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( MaximumNArgs ( 3 ) , false )
2017-10-31 19:57:17 +01:00
output , err := executeCommand ( c , "a" , "b" )
2021-11-16 22:20:18 +00:00
expectSuccess ( output , err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithValid tests the functionality of the MaximumNArgs function with valid arguments.
// It verifies that the command is executed successfully when provided with two arguments.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithValid ( t * testing . T ) {
c := getCommand ( MaximumNArgs ( 2 ) , true )
output , err := executeCommand ( c , "one" , "three" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithValid_WithInvalidArgs tests the behavior of the MaximumNArgs function with both valid and invalid arguments.
// It ensures that the function correctly handles different sets of input arguments.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MaximumNArgs ( 2 ) , true )
output , err := executeCommand ( c , "a" , "b" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithValidOnly_WithInvalidArgs tests the behavior of a command with the MatchAll validator, OnlyValidArgs option, and MaximumNArgs set to 2 when provided with both valid and invalid arguments. It asserts that the command execution results in an error indicating the presence of invalid arguments.
// Parameters:
// - t: A pointer to a testing.T instance for running tests.
// Returns:
// None
// Errors:
// - An error if the command does not return an error when it should, or if the error message does not indicate the presence of invalid arguments.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , MaximumNArgs ( 2 ) ) , true )
_ , err := executeCommand ( c , "a" , "b" )
validOnlyWithInvalidArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithMoreArgs tests the behavior of the MaximumNArgs constraint when provided with more arguments than allowed. It checks if the resulting command, when executed, returns an error indicating too many arguments.
// getCommand creates and returns a test command with the given argument constraint and allowStdin flag.
// executeCommand executes the given command with the specified arguments and returns the result and any error encountered.
// maximumNArgsWithMoreArgs asserts that the provided error indicates the use of more arguments than allowed for MaximumNArgs constraints.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithMoreArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( MaximumNArgs ( 2 ) , false )
2017-10-31 19:57:17 +01:00
_ , err := executeCommand ( c , "a" , "b" , "c" )
2021-11-16 22:20:18 +00:00
maximumNArgsWithMoreArgs ( err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithMoreArgs_WithValid tests the execution of a command with more arguments than allowed by MaximumNArgs.
// It verifies that an error is returned when executing the command with three arguments when only two are permitted.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithMoreArgs_WithValid ( t * testing . T ) {
c := getCommand ( MaximumNArgs ( 2 ) , true )
_ , err := executeCommand ( c , "one" , "three" , "two" )
maximumNArgsWithMoreArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs tests the behavior of the MaximumNArgs decorator when called with more arguments than expected.
// It checks both valid and invalid argument scenarios.
// It uses the getCommand function to create a command with the MaximumNArgs decorator applied.
// The executeCommand function is then used to run the command with "a", "b", and "c" as arguments.
// The maximumNArgsWithMoreArgs function asserts the expected error behavior based on the number of arguments.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MaximumNArgs ( 2 ) , true )
_ , err := executeCommand ( c , "a" , "b" , "c" )
maximumNArgsWithMoreArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs tests the behavior of a command with a maximum number of arguments and only valid arguments.
// It ensures that an error is returned when more than two arguments are provided.
// The test checks if the command correctly identifies invalid arguments when they are present.
2022-09-10 15:33:34 +02:00
func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , MaximumNArgs ( 2 ) ) , true )
_ , err := executeCommand ( c , "a" , "b" , "c" )
validOnlyWithInvalidArgs ( err , t )
}
// ExactArgs
2025-04-26 20:13:18 +00:00
// TestExactArgs tests the functionality of a command that requires exactly three arguments.
// It sets up a command with ExactArgs set to 3 and executes it with three arguments.
// The output and error are then checked for success using expectSuccess.
2017-10-31 19:57:17 +01:00
func TestExactArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( ExactArgs ( 3 ) , false )
2017-10-31 19:57:17 +01:00
output , err := executeCommand ( c , "a" , "b" , "c" )
2021-11-16 22:20:18 +00:00
expectSuccess ( output , err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithValid tests the ExactArgs function with valid arguments.
// It verifies that the command is executed successfully when the correct number of arguments is provided.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithValid ( t * testing . T ) {
c := getCommand ( ExactArgs ( 3 ) , true )
output , err := executeCommand ( c , "three" , "one" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithValid_WithInvalidArgs tests the ExactArgs validator with valid and invalid arguments.
// It creates a command with ExactArgs set to 3, executes it with various argument sets,
// and checks if the execution behaves as expected for both valid and invalid cases.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( ExactArgs ( 3 ) , true )
output , err := executeCommand ( c , "three" , "a" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithValidOnly_WithInvalidArgs tests the behavior of a command that expects exactly 3 arguments and allows only valid args.
// It executes the command with invalid arguments and checks if the error returned is as expected when using OnlyValidArgs.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , ExactArgs ( 3 ) ) , true )
_ , err := executeCommand ( c , "three" , "a" , "two" )
validOnlyWithInvalidArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithInvalidCount tests the behavior of a command that expects exactly two arguments but receives an invalid count.
// It creates a command with ExactArgs(2) validator and executes it with three arguments, expecting an error due to the mismatched argument count.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithInvalidCount ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( ExactArgs ( 2 ) , false )
2017-10-31 19:57:17 +01:00
_ , err := executeCommand ( c , "a" , "b" , "c" )
2021-11-16 22:20:18 +00:00
exactArgsWithInvalidCount ( err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithInvalidCount_WithValid tests the execution of a command with an exact number of arguments when provided with an invalid count.
// It expects an error related to the argument count being incorrect.
// The test ensures that the command handling correctly identifies and reports errors for too many arguments.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithInvalidCount_WithValid ( t * testing . T ) {
c := getCommand ( ExactArgs ( 2 ) , true )
_ , err := executeCommand ( c , "three" , "one" , "two" )
exactArgsWithInvalidCount ( err , t )
2018-10-21 10:01:21 -04:00
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs tests the behavior of a command with exact arguments when executed with an invalid count and valid arguments.
// It checks if the command returns an error when provided with more than the expected number of arguments.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( ExactArgs ( 2 ) , true )
_ , err := executeCommand ( c , "three" , "a" , "two" )
2021-11-16 22:20:18 +00:00
exactArgsWithInvalidCount ( err , t )
2018-10-21 10:01:21 -04:00
}
2025-04-26 20:13:18 +00:00
// TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs tests the behavior of a command with exact argument count and valid-only filter when provided with invalid arguments.
// It expects an error due to the mismatch in the number of arguments passed.
// The test ensures that the OnlyValidArgs filter is applied correctly, allowing only valid arguments through.
2022-09-10 15:33:34 +02:00
func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , ExactArgs ( 2 ) ) , true )
2021-11-16 22:20:18 +00:00
_ , err := executeCommand ( c , "three" , "a" , "two" )
2022-09-10 15:33:34 +02:00
validOnlyWithInvalidArgs ( err , t )
2018-10-21 10:01:21 -04:00
}
2022-09-10 15:33:34 +02:00
// RangeArgs
2025-04-26 20:13:18 +00:00
// TestRangeArgs tests the functionality of RangeArgs with provided arguments.
// It creates a command using RangeArgs and executes it with given input arguments.
// Finally, it checks if the command execution was successful.
2017-10-31 19:57:17 +01:00
func TestRangeArgs ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( RangeArgs ( 2 , 4 ) , false )
2017-10-31 19:57:17 +01:00
output , err := executeCommand ( c , "a" , "b" , "c" )
2021-11-16 22:20:18 +00:00
expectSuccess ( output , err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithValid tests the RangeArgs function with valid arguments and ensures that the command execution is successful. It uses a test function to verify the output and error returned by the executeCommand function. The getCommand function is used to create a command based on the provided arguments and the debug flag.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithValid ( t * testing . T ) {
c := getCommand ( RangeArgs ( 2 , 4 ) , true )
output , err := executeCommand ( c , "three" , "one" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithValid_WithInvalidArgs tests the RangeArgs function with valid and invalid arguments.
// It verifies that the command execution behaves as expected when provided with a range of integers (2 to 4).
// It checks both valid and invalid arguments and ensures successful execution for valid inputs.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( RangeArgs ( 2 , 4 ) , true )
output , err := executeCommand ( c , "three" , "a" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithValidOnly_WithInvalidArgs tests the behavior of the command when it expects a range of arguments (2 to 4) and only valid arguments.
// It verifies that an error is returned when the provided arguments do not meet the specified criteria.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , RangeArgs ( 2 , 4 ) ) , true )
_ , err := executeCommand ( c , "three" , "a" , "two" )
validOnlyWithInvalidArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithInvalidCount tests the behavior of the RangeArgs command when provided with an invalid number of arguments.
// It creates a command with an expected range and executes it with an incorrect argument count, expecting an error related to the number of arguments being out of range.
// The test uses a helper function `rangeArgsWithInvalidCount` to verify that the correct error is returned.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithInvalidCount ( t * testing . T ) {
2021-11-16 22:20:18 +00:00
c := getCommand ( RangeArgs ( 2 , 4 ) , false )
2017-10-31 19:57:17 +01:00
_ , err := executeCommand ( c , "a" )
2021-11-16 22:20:18 +00:00
rangeArgsWithInvalidCount ( err , t )
2017-10-31 19:57:17 +01:00
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithInvalidCount_WithValid tests the RangeArgs function with an invalid count but valid arguments.
// It creates a command using the getCommand function and executes it with a single argument "two".
// The function then checks if the error returned by executeCommand matches the expected error for invalid count.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithInvalidCount_WithValid ( t * testing . T ) {
c := getCommand ( RangeArgs ( 2 , 4 ) , true )
_ , err := executeCommand ( c , "two" )
rangeArgsWithInvalidCount ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs tests the execution of a command with invalid count and valid arguments.
// It checks if the function handles errors correctly when provided with an invalid number of arguments.
// Parameters:
// - t: A pointer to a testing.T object used for test assertions.
// The function does not return any values but asserts on the behavior of the executeCommand function.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( RangeArgs ( 2 , 4 ) , true )
_ , err := executeCommand ( c , "a" )
rangeArgsWithInvalidCount ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs tests the behavior of a command with range arguments when given an invalid count and valid only option, expecting errors for invalid arguments.
// It uses a test function to verify that executing the command with "a" as an argument results in an error due to the mismatch between expected and actual arguments.
2022-09-10 15:33:34 +02:00
func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( MatchAll ( OnlyValidArgs , RangeArgs ( 2 , 4 ) ) , true )
_ , err := executeCommand ( c , "a" )
validOnlyWithInvalidArgs ( err , t )
}
// Takes(No)Args
2025-04-26 20:13:18 +00:00
// TestRootTakesNoArgs tests that calling the root command with illegal arguments returns an error.
// It creates a root command and a child command, adds the child to the root, and then attempts to execute
// the root command with invalid arguments. It expects an error indicating that the "illegal" command is unknown for "root".
2017-10-31 19:57:17 +01:00
func TestRootTakesNoArgs ( t * testing . T ) {
rootCmd := & Command { Use : "root" , Run : emptyRun }
childCmd := & Command { Use : "child" , Run : emptyRun }
rootCmd . AddCommand ( childCmd )
_ , err := executeCommand ( rootCmd , "illegal" , "args" )
if err == nil {
t . Fatal ( "Expected an error" )
}
got := err . Error ( )
expected := ` unknown command "illegal" for "root" `
if ! strings . Contains ( got , expected ) {
t . Errorf ( "expected %q, got %q" , expected , got )
}
}
2025-04-26 20:13:18 +00:00
// TestRootTakesArgs tests if the root command correctly handles arguments.
2017-10-31 19:57:17 +01:00
func TestRootTakesArgs ( t * testing . T ) {
rootCmd := & Command { Use : "root" , Args : ArbitraryArgs , Run : emptyRun }
childCmd := & Command { Use : "child" , Run : emptyRun }
rootCmd . AddCommand ( childCmd )
_ , err := executeCommand ( rootCmd , "legal" , "args" )
if err != nil {
t . Errorf ( "Unexpected error: %v" , err )
}
}
2025-04-26 20:13:18 +00:00
// TestChildTakesNoArgs tests that the command handles unexpected arguments correctly when a subcommand does not accept any arguments.
//
// It verifies that calling "child" with additional arguments results in an error.
2017-10-31 19:57:17 +01:00
func TestChildTakesNoArgs ( t * testing . T ) {
rootCmd := & Command { Use : "root" , Run : emptyRun }
childCmd := & Command { Use : "child" , Args : NoArgs , Run : emptyRun }
rootCmd . AddCommand ( childCmd )
_ , err := executeCommand ( rootCmd , "child" , "illegal" , "args" )
if err == nil {
t . Fatal ( "Expected an error" )
}
got := err . Error ( )
expected := ` unknown command "illegal" for "root child" `
if ! strings . Contains ( got , expected ) {
t . Errorf ( "expected %q, got %q" , expected , got )
}
}
2025-04-26 20:13:18 +00:00
// TestChildTakesArgs tests that a child command with arguments can be executed successfully.
// It creates a root command and adds a child command with arbitrary argument requirements.
// Then, it executes the child command with legal arguments and verifies there is no error.
2017-10-31 19:57:17 +01:00
func TestChildTakesArgs ( t * testing . T ) {
rootCmd := & Command { Use : "root" , Run : emptyRun }
childCmd := & Command { Use : "child" , Args : ArbitraryArgs , Run : emptyRun }
rootCmd . AddCommand ( childCmd )
_ , err := executeCommand ( rootCmd , "child" , "legal" , "args" )
if err != nil {
t . Fatalf ( "Unexpected error: %v" , err )
}
}
2021-12-07 14:38:00 -08:00
2025-04-26 20:13:18 +00:00
// TestMatchAll tests the MatchAll function with various test cases to ensure it correctly validates command arguments.
// It checks that the number of arguments is exactly 3 and each argument is exactly 2 bytes long.
2021-12-07 14:38:00 -08:00
func TestMatchAll ( t * testing . T ) {
// Somewhat contrived example check that ensures there are exactly 3
// arguments, and each argument is exactly 2 bytes long.
pargs := MatchAll (
ExactArgs ( 3 ) ,
func ( cmd * Command , args [ ] string ) error {
for _ , arg := range args {
if len ( [ ] byte ( arg ) ) != 2 {
return fmt . Errorf ( "expected to be exactly 2 bytes long" )
}
}
return nil
} ,
)
testCases := map [ string ] struct {
args [ ] string
fail bool
} {
"happy path" : {
[ ] string { "aa" , "bb" , "cc" } ,
false ,
} ,
"incorrect number of args" : {
[ ] string { "aa" , "bb" , "cc" , "dd" } ,
true ,
} ,
"incorrect number of bytes in one arg" : {
[ ] string { "aa" , "bb" , "abc" } ,
true ,
} ,
}
rootCmd := & Command { Use : "root" , Args : pargs , Run : emptyRun }
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
_ , err := executeCommand ( rootCmd , tc . args ... )
if err != nil && ! tc . fail {
t . Errorf ( "unexpected: %v\n" , err )
}
if err == nil && tc . fail {
t . Errorf ( "expected error" )
}
} )
}
}
2022-03-17 21:36:47 -04:00
2022-09-10 15:33:34 +02:00
// DEPRECATED
2025-04-26 20:13:18 +00:00
// TestExactValidArgs tests the behavior of a command when provided with exactly three valid arguments.
// It checks if the command executes successfully with the given arguments and validates the output.
2022-09-10 15:33:34 +02:00
func TestExactValidArgs ( t * testing . T ) {
c := getCommand ( ExactValidArgs ( 3 ) , true )
output , err := executeCommand ( c , "three" , "one" , "two" )
expectSuccess ( output , err , t )
}
2025-04-26 20:13:18 +00:00
// TestExactValidArgs_WithInvalidCount tests the ExactValidArgs function with an invalid argument count.
// It asserts that an error is returned when the number of arguments does not match the expected count.
2022-09-10 15:33:34 +02:00
func TestExactValidArgs_WithInvalidCount ( t * testing . T ) {
c := getCommand ( ExactValidArgs ( 2 ) , false )
_ , err := executeCommand ( c , "three" , "one" , "two" )
exactArgsWithInvalidCount ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestExactValidArgs_WithInvalidCount_WithInvalidArgs tests the behavior of the ExactValidArgs validator when provided with an invalid count and invalid arguments.
// It creates a command with the ExactValidArgs validator, executes it with three arguments (two valid and one invalid), and checks if the error returned is as expected for invalid argument counts.
2022-09-10 15:33:34 +02:00
func TestExactValidArgs_WithInvalidCount_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( ExactValidArgs ( 2 ) , true )
_ , err := executeCommand ( c , "three" , "a" , "two" )
exactArgsWithInvalidCount ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestExactValidArgs_WithInvalidArgs tests the behavior of ExactValidArgs when invoked with invalid arguments.
// It uses a test command and checks if the execution results in an error as expected.
2022-09-10 15:33:34 +02:00
func TestExactValidArgs_WithInvalidArgs ( t * testing . T ) {
c := getCommand ( ExactValidArgs ( 2 ) , true )
_ , err := executeCommand ( c , "three" , "a" )
validOnlyWithInvalidArgs ( err , t )
}
2025-04-26 20:13:18 +00:00
// TestLegacyArgsRootAcceptsArgs ensures that the root command accepts arguments if it does not have sub-commands.
// It verifies backwards-compatibility with respect to the legacyArgs() function.
2022-03-17 21:36:47 -04:00
func TestLegacyArgsRootAcceptsArgs ( t * testing . T ) {
rootCmd := & Command { Use : "root" , Args : nil , Run : emptyRun }
_ , err := executeCommand ( rootCmd , "somearg" )
if err != nil {
t . Fatalf ( "Unexpected error: %v" , err )
}
}
2025-04-26 20:13:18 +00:00
// TestLegacyArgsSubcmdAcceptsArgs verifies that a sub-command accepts arguments and further sub-commands while maintaining backwards-compatibility with the legacyArgs() function. It ensures that executing commands like "root child somearg" does not result in errors.
2022-03-17 21:36:47 -04:00
func TestLegacyArgsSubcmdAcceptsArgs ( t * testing . T ) {
rootCmd := & Command { Use : "root" , Args : nil , Run : emptyRun }
childCmd := & Command { Use : "child" , Args : nil , Run : emptyRun }
grandchildCmd := & Command { Use : "grandchild" , Args : nil , Run : emptyRun }
rootCmd . AddCommand ( childCmd )
childCmd . AddCommand ( grandchildCmd )
_ , err := executeCommand ( rootCmd , "child" , "somearg" )
if err != nil {
t . Fatalf ( "Unexpected error: %v" , err )
}
}