Skip to content
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

Possible to test long time processing CLI actions? #1159

Closed
koddr opened this issue Jul 7, 2020 · 6 comments
Closed

Possible to test long time processing CLI actions? #1159

koddr opened this issue Jul 7, 2020 · 6 comments
Labels
area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this
Milestone

Comments

@koddr
Copy link

koddr commented Jul 7, 2020

my question is...

Possible to test long time processing CLI actions?

I develop CLI for aggregation of all create-<NAME>-app and similar frontend things. Very often the execution of such a command (e.g. installation of dependencies by npm install) takes a very long time and exits the tests (e.g. on gocover.io or locally). That is, they're out of 30 or even 180 seconds limit.

This is my project source (without some extra lines of code):

// ./main.go

package main

// ...

func main() {
	// Start new CLI app
	myCmd, err := mycmd.New()
	if err != nil {
		log.Fatal(err)
	}

	// Run new CLI
	if err = myCmd.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}

Next, I separated a new CLI instance to ./pkg/mycmd/new.go file:

// ./pkg/mycmd/new.go

package mycmd

// ...

var (
	appPath             string
	appFrontend         string
)

func New() (*cli.App, error) {
	// Init CLI
	app := &cli.App{}

	// Configure CLI
	app.Name = "mycmd"
	app.Usage = "description"
	app.Version = "0.1.0"

	// Setting CLI commands
	app.Commands = []*cli.Command{
		{
			Name:  "create",
			Usage: "create a new project with the selected configuration",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:        "path",
					Aliases:     []string{"p"},
					Value:       "./my-project",
					Usage:       "path to create project, ex. ~/projects/my-app",
					Required:    false,
					Destination: &appPath,
				},
				&cli.StringFlag{
					Name:        "frontend",
					Aliases:     []string{"f"},
					Value:       "none",
					Usage:       "frontend for your project, ex. Preact, React, Vue, Svelte",
					Required:    false,
					Destination: &appFrontend,
				},
			},
			Action: CreateCLIAction, // <-- this is my action func
		},
	}

	return app, nil
}

Action func lives here:

// ./pkg/mycmd/actions.go

package mycmd

// ...

func CreateCLIAction(c *cli.Context) error {
	// ...

	options := []string{}

	// ...

	switch appFrontend {
	case "react":
		options = []string{
			// ...
		}
		break
	// ...
	}

	if appFrontend != "none" {
		// ...
		cmd := exec.Command("npm", options...) 
		// ^ execute options to install each frameworks via npm
		// ...
	}

	// ...

	return nil
}

And my question is... how can I test CreateCLIAction(c *cli.Context) func on right (recommended) way and without waiting so long time to execute npm things? Usage example to explain this moment, would be great! 😉

Thanks!

P.S. I'm newbie to testing and mocking so complex programs in Go.

@koddr koddr added area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this labels Jul 7, 2020
@stale
Copy link

stale bot commented Oct 5, 2020

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

@stale stale bot added the status/stale stale due to the age of it's last update label Oct 5, 2020
@stale
Copy link

stale bot commented Nov 5, 2020

Closing this as it has become stale.

@stale stale bot closed this as completed Nov 5, 2020
@meatballhat meatballhat reopened this Apr 22, 2022
@meatballhat meatballhat removed the status/stale stale due to the age of it's last update label Apr 22, 2022
@meatballhat meatballhat added this to the Release 2.4.x milestone Apr 23, 2022
@meatballhat meatballhat changed the title q: Possible to test long time processing CLI actions? Possible to test long time processing CLI actions? Apr 24, 2022
@dearchap
Copy link
Contributor

@koddr Does the CreateCLIAction use any functions from cli.Context ?

@dearchap
Copy link
Contributor

@meatballhat Are we planning to have context as an interface in v3 ?

@meatballhat
Copy link
Member

@dearchap I would love to see #1587 for v3, which would remove/combine the cli.Context type

@dearchap
Copy link
Contributor

dearchap commented Jun 8, 2023

@koddr There is nothing in the cli library that enforces any time limits or anything. So the command will run till the action function returns. If you are starting your cli app from the shell you can run it in the background and run your next cli app invocation.

@dearchap dearchap closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

3 participants