Exemple #1
0
func main() {
	application := console.NewApplication("eidolon/console", "0.1.0")
	application.Logo = `
                                             #
                              ###            ##
######## ### ####### #######  ###   #######  ###  ##
         ###       ##      ## ###         ## #### ##
 ####### ###  ###  ## ##   ## ###    ##   ## #######
 ###     ###  ###  ## ##   ## ###    ##   ## ### ###
 ####### ###  ######   #####  ####### #####  ###  ##
                                                   #
`

	application.AddCommand(console.Command{
		Name:        "greet:example",
		Description: "Greet's the given user, or the world.",
		Help:        "You don't have to specify a name.",
		Configure: func(definition *console.Definition) {
			definition.AddOption(
				parameters.NewStringValue(&name),
				"-n, --name=NAME",
				"Provide a name for the greeting.",
			)

			definition.AddArgument(
				parameters.NewIntValue(&favNum),
				"FAVOURITE_NUMBER",
				"Provide your favourite number.",
			)
		},
		Execute: func(input *console.Input, output *console.Output) error {
			output.Printf("Hello, %s!\n", name)
			output.Printf("Your favourite number is %d.\n", favNum)
			return nil
		},
	})

	code := application.Run(os.Args[1:])

	os.Exit(code)
}
func TestDescribeCommand(t *testing.T) {
	t.Run("should return command usage information", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test",
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "USAGE:"), "Expected usage information.")
	})

	t.Run("should include the command name", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "test-command-name"), "Expected command name.")
	})

	t.Run("should show the command description", func(t *testing.T) {
		description := "This is the test-command-name description."

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name:        "test-command-name",
			Description: description,
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, description), "Expected command description.")
	})

	t.Run("should return command help if there is some", func(t *testing.T) {
		help := "This is some help for the test-command-name command."

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
			Help: help,
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "HELP:"), "Expected command help header.")
		assert.True(t, strings.Contains(result, help), "Expected command help.")
	})

	t.Run("should show arguments if there are any", func(t *testing.T) {
		var s1 string
		var s2 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
			Configure: func(definition *console.Definition) {
				definition.AddArgument(parameters.NewStringValue(&s1), "STRING_ARG_S1", "")
				definition.AddArgument(parameters.NewStringValue(&s2), "STRING_ARG_S2", "")
			},
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "STRING_ARG_S1"), "Expected argument name.")
		assert.True(t, strings.Contains(result, "STRING_ARG_S2"), "Expected argument name.")
	})

	t.Run("should show optional arguments wrapped in brackets", func(t *testing.T) {
		var s1 string
		var s2 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
			Configure: func(definition *console.Definition) {
				definition.AddArgument(parameters.NewStringValue(&s1), "[STRING_ARG_S1]", "")
				definition.AddArgument(parameters.NewStringValue(&s2), "[STRING_ARG_S2]", "")
			},
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "[STRING_ARG_S1]"), "Expected argument name.")
		assert.True(t, strings.Contains(result, "[STRING_ARG_S2]"), "Expected argument name.")
	})

	t.Run("should show that there are options if there are any", func(t *testing.T) {
		var s1 string
		var s2 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Configure = func(definition *console.Definition) {
			definition.AddOption(parameters.NewStringValue(&s1), "--s1=VALUE", "")
		}

		command := console.Command{
			Name: "test-command-name",
			Configure: func(definition *console.Definition) {
				definition.AddOption(parameters.NewStringValue(&s2), "--s2=VALUE", "")
			},
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "[OPTIONS...]"), "Expected options.")
	})
}
func TestDescribeApplication(t *testing.T) {
	t.Run("should show the application logo if there is one", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Logo = "Eidolon Console\n"

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, application.Logo), "Expected application logo.")
	})

	t.Run("should show the application name", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, application.Name), "Expected application name.")
	})

	t.Run("should show the application version", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, application.Version), "Expected application version.")
	})

	t.Run("should show the application usage", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.UsageName = "eidolon_console_binary"

		usage := application.UsageName + " COMMAND [OPTIONS...] [ARGUMENTS...]"

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "USAGE:"), "Expected application usage title.")
		assert.True(t, strings.Contains(result, usage), "Expected application usage.")
	})

	t.Run("should show the application options if there are any", func(t *testing.T) {
		var s1 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Configure = func(definition *console.Definition) {
			definition.AddOption(parameters.NewStringValue(&s1), "--s1", "S1 option for testing.")
		}

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "OPTIONS:"), "Expected application options title.")
		assert.True(t, strings.Contains(result, "-h"), "Expected application options.")
		assert.True(t, strings.Contains(result, "--help"), "Expected application options.")
		assert.True(t, strings.Contains(result, "--s1"), "Expected application options.")
	})

	t.Run("should show the application commands if there are any", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.AddCommands([]console.Command{
			{
				Name: "foo-cmd",
			},
			{
				Name: "bar-cmd",
			},
		})

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "COMMANDS:"), "Expected application commands title.")
		assert.True(t, strings.Contains(result, "foo-cmd"), "Expected application commands.")
		assert.True(t, strings.Contains(result, "bar-cmd"), "Expected application commands.")

	})

	t.Run("should not show the commands title if there are no commands", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Logo = "Eidolon Console\n"

		result := console.DescribeApplication(application)

		assert.False(t, strings.Contains(result, "COMMANDS:"), "Expected no commands title.")
	})

	t.Run("should show the application help if there is any", func(t *testing.T) {
		help := "This is some application help right here. Lorem ipsum dolor sit amet."

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Help = help

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "HELP:"), "Expected help title.")
		assert.True(t, strings.Contains(result, help), "Expected help.")
	})

	t.Run("should not show the help title if there is no application help", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Logo = "Eidolon Console\n"

		result := console.DescribeApplication(application)

		assert.False(t, strings.Contains(result, "HELP:"), "Expected no help title.")
	})
}
Exemple #4
0
func TestApplication(t *testing.T) {
	createApplication := func(writer io.Writer) *console.Application {
		application := console.NewApplication("eidolon/console", "1.2.3.+testing")
		application.Writer = writer

		return application
	}

	createTestCommand := func(a *string, b *int) console.Command {
		return console.Command{
			Name: "test",
			Configure: func(definition *console.Definition) {
				definition.AddArgument(parameters.NewStringValue(a), "STRINGARG", "")
				definition.AddOption(parameters.NewIntValue(b), "--int-opt=VALUE", "")
			},
			Execute: func(input *console.Input, output *console.Output) error {
				output.Printf("STRINGARG = %s", *a)
				output.Printf("--int-opt = %v", *b)
				return nil
			},
		}
	}

	t.Run("Run()", func(t *testing.T) {
		t.Run("should return exit code 2 if no command was asked for", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			code := application.Run([]string{})

			assert.Equal(t, 100, code)
		})

		t.Run("should return exit code 2 if no command was found", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			code := application.Run([]string{"foo"})

			assert.Equal(t, 100, code)
		})

		t.Run("should return exit code 100 if the help flag is set", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			code := application.Run([]string{"--help"})

			assert.Equal(t, 100, code)
		})

		t.Run("should show application help if the help flag is set", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.Run([]string{"--help"})

			output := writer.String()
			containsUsage := strings.Contains(output, "USAGE:")
			containsArguments := strings.Contains(output, "ARGUMENTS:")
			containsOptions := strings.Contains(output, "OPTIONS:")
			containsHelp := containsUsage && containsOptions && !containsArguments

			assert.True(t, containsHelp, "Expected help output.")
		})

		t.Run("should show command help if the help flag is set when running a command", func(t *testing.T) {
			var a string
			var b int

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(createTestCommand(&a, &b))
			application.Run([]string{"test", "--help"})

			output := writer.String()
			containsUsage := strings.Contains(output, "USAGE:")
			containsArguments := strings.Contains(output, "ARGUMENTS:")
			containsOptions := strings.Contains(output, "OPTIONS:")
			containsHelp := containsUsage && containsOptions && containsArguments

			assert.True(t, containsHelp, "Expected help output.")
		})

		t.Run("should return exit code 0 if a command was found, and ran OK", func(t *testing.T) {
			var a string
			var b int

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(createTestCommand(&a, &b))

			code := application.Run([]string{"test", "aval", "--int-opt=384"})

			assert.Equal(t, 0, code)
		})

		t.Run("should return exit code 101 if mapping input fails", func(t *testing.T) {
			var a string
			var b int

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(createTestCommand(&a, &b))

			code := application.Run([]string{"test", "aval", "--int-opt=hello"})

			assert.Equal(t, 101, code)
		})

		t.Run("should return exit code 102 if the command execution fails", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(console.Command{
				Name: "test",
				Execute: func(input *console.Input, output *console.Output) error {
					return errors.New("Testing errors")
				},
			})

			code := application.Run([]string{"test", "aval", "--int-opt=hello"})

			assert.Equal(t, 102, code)
		})

		t.Run("should configure the application definition", func(t *testing.T) {
			var a string
			var b int
			var foo string

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.Configure = func(definition *console.Definition) {
				definition.AddOption(parameters.NewStringValue(&foo), "--foo=FOO", "")
			}

			application.AddCommand(createTestCommand(&a, &b))
			application.Run([]string{"test", "aval", "--foo=bar"})

			assert.Equal(t, "bar", foo)
		})
	})

	t.Run("AddCommands()", func(t *testing.T) {
		t.Run("should work when adding 1 command", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)

			assert.Equal(t, 0, len(application.Commands))

			application.AddCommands([]console.Command{
				{
					Name: "test1",
				},
			})

			assert.Equal(t, 1, len(application.Commands))
		})

		t.Run("should work when adding no commands", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)

			assert.Equal(t, 0, len(application.Commands))

			application.AddCommands([]console.Command{})

			assert.Equal(t, 0, len(application.Commands))
		})

		t.Run("should work when adding more than 1 command", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)

			assert.Equal(t, 0, len(application.Commands))

			application.AddCommands([]console.Command{
				{
					Name: "test1",
				},
				{
					Name: "test2",
				},
				{
					Name: "test3",
				},
			})

			assert.Equal(t, 3, len(application.Commands))
		})
	})

	t.Run("AddCommand()", func(t *testing.T) {
		writer := bytes.Buffer{}
		application := createApplication(&writer)

		assert.Equal(t, 0, len(application.Commands))

		application.AddCommand(console.Command{
			Name: "test1",
		})

		assert.Equal(t, 1, len(application.Commands))
	})
}
Exemple #5
0
func TestNewApplication(t *testing.T) {
	application := console.NewApplication("eidolon/console", "1.2.3+testing")
	assert.True(t, application != nil, "Application should not be nil")
}