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

Context is set fix v1 #978

Merged
merged 4 commits into from Dec 8, 2019
Merged

Context is set fix v1 #978

merged 4 commits into from Dec 8, 2019

Conversation

asahasrabuddhe
Copy link
Member

@asahasrabuddhe asahasrabuddhe commented Dec 7, 2019

Motivation

Partly fixes #790

While the issue primarily talks about the inconsistent behavior of the StringSliceFlag, there is another bug report hidden there. Consider the following code:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.Flags = []cli.Flag{
		cli.StringSliceFlag{
			Name: "Hostnames, n",
			Usage: "`HOSTNAME`s to listen to.",
			Value: &cli.StringSlice{"localhost"},
		},
	}
	app.Action = func(c *cli.Context) error {
		if c.IsSet("Hostnames") {
			// do something
		}

		if c.IsSet("n") {
			// do something else
		}

		return nil
	}

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal()
	}
}

Suppose we are to execute this code using either --Hostnames or -n, we would expect both the c.IsSet("Hostnames") and c.IsSet("n") to return true. However, this is not the case. If we use the full flag name to initialize the flag but, pass the short name to c.IsSet, we get false.

This behavior is also inconsistent with the c.StringSlice or any c.FlagName function that returns the correct value regardless of whether we pass the full name or the short name to it. This PR aims to fix this inconsistency.

Release Notes

[BUG FIX] context.IsSet() returns true or false correctly regardless of whether the short name or the full name of the flag is passed to it.

Changes

  • .gitignore: Add .idea folder to list of ignored files. JetBrains IDEs like GoLand create this folder.
  • context.go: Add additional code that checks if a particular flag is set and, if set, marks all the other aliases of the flag in the context.
  • context_test.go: Test Cases

Testing

Added a unit test that sets the flags using both full names and short names alternatively and verifies the consistency of the context.IsSet output.

@codecov
Copy link

codecov bot commented Dec 7, 2019

Codecov Report

Merging #978 into v1 will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##               v1    #978      +/-   ##
=========================================
+ Coverage   71.65%   71.7%   +0.04%     
=========================================
  Files          30      30              
  Lines        2410    2414       +4     
=========================================
+ Hits         1727    1731       +4     
  Misses        577     577              
  Partials      106     106
Impacted Files Coverage Δ
context.go 80.12% <100%> (+0.49%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ebf9c3e...023bcca. Read the comment docs.

@asahasrabuddhe asahasrabuddhe marked this pull request as ready for review December 8, 2019 08:03
@asahasrabuddhe asahasrabuddhe requested a review from a team as a code owner December 8, 2019 08:03
@asahasrabuddhe
Copy link
Member Author

Did something change? Do we only need 1 approval now? Not that I'm complaining 😅

@coilysiren
Copy link
Member

Did something change? Do we only need 1 approval now? Not that I'm complaining 😅

=> #826 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants