semver icon indicating copy to clipboard operation
semver copied to clipboard

NewConstraint method faulty behavior

Open benjaminbear opened this issue 2 years ago • 3 comments

Used version: 3.1.1 When I try to create a constraint with NewConstraint method I get following returns:

good Example c, cErr := semver.NewConstraint("12.3.4.1234") c.String() = nil cErr = "improper constraint: 12.3.4.1234"

faulty behavior c, cErr := semver.NewConstraint("12.23.4.1234") c.String() = "12.23.4 1234" cErr = nil

or

c, cErr := semver.NewConstraint("12.3.34.1234") c.String() = "12.3.34 1234" cErr = nil


I would expect the same behavior as with "12.3.4.1234", means telling me its not a contraint..

benjaminbear avatar Aug 10 '22 13:08 benjaminbear

FYI: the used regex to validate the contstraint fails: https://regex101.com/r/3fEGeK/1

benjaminbear avatar Aug 10 '22 13:08 benjaminbear

I can explain what's going on here. 12.3.4.1234 is invalid because it has 4 parts and semver is only 3 parts.

12.3.34 1234 is read as 2 separate constraints. 12.3.34 and 1234. Those are added together. See https://github.com/Masterminds/semver#basic-comparisons

mattfarina avatar Aug 10 '22 14:08 mattfarina

Ah I'm sorry, had to correct the 2 faulty examples, there were copy paste typos. Is this: c, cErr := semver.NewConstraint("12.23.4.1234") c.String() = "12.23.4 1234" cErr = nil

expected behavior? Its a non semver string with 4 parts like in the first example, but handled differently. The third dot is removed and its a valid range? I would expect an error here.

Wy is 12.3.4.1234 and 12.23.4.1234 handled differently? the only difference is that the second part is 2 digits

benjaminbear avatar Aug 10 '22 19:08 benjaminbear

I have verified the issue. https://go.dev/play/p/wMEsKXb5KEx

mattfarina avatar Aug 15 '22 13:08 mattfarina

@benjaminbear The fix can be found in #186 and the commit message explains what happened to cause this.

I am planning on getting a new release out by the end of this week with some fixes and new functionality.

Feel free to double check my fix and tell me if it didn't work.

mattfarina avatar Aug 15 '22 15:08 mattfarina

Great to hear! Thank you very much @mattfarina

benjaminbear avatar Aug 18 '22 07:08 benjaminbear