-
Notifications
You must be signed in to change notification settings - Fork 297
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
[fx-annotate] Verify invalid tags passed to fx.Annotate #835 #1051
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1474,6 +1474,131 @@ func TestAnnotate(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAnnotateApplyFail(t *testing.T) { | ||
type a struct{} | ||
type b struct{ a *a } | ||
newA := func() *a { return &a{} } | ||
newB := func(a *a) *b { | ||
return &b{a} | ||
} | ||
|
||
var ( | ||
errTagSyntaxSpace = `multiple tags are not separated by space` | ||
errTagKeySyntax = "tag key is invalid, Use group, name or optional as tag keys" | ||
errTagValueSyntaxQuote = `tag value should start with double quote. i.e. key:"value" ` | ||
errTagValueSyntaxEndingQuote = `tag value should end in double quote. i.e. key:"value" ` | ||
) | ||
tests := []struct { | ||
give string | ||
wantErr string | ||
giveAnnotationParam fx.Annotation | ||
giveAnnotationResult fx.Annotation | ||
}{ | ||
{ | ||
give: "Tags value invalid ending quote", | ||
wantErr: errTagValueSyntaxEndingQuote, | ||
giveAnnotationParam: fx.ParamTags(`name:"something'`), | ||
giveAnnotationResult: fx.ResultTags(`name:"something'`), | ||
}, | ||
{ | ||
give: "Tags value wrong starting quote", | ||
wantErr: errTagValueSyntaxQuote, | ||
giveAnnotationParam: fx.ParamTags(`name:"something" optional:'true"`), | ||
giveAnnotationResult: fx.ResultTags(`name:"something" optional:'true"`), | ||
}, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The set of tests for ResultTags looks similar to for ParamTags, can we de-duplicate these tests? |
||
give: "Tags multiple tags not separated by space", | ||
wantErr: errTagSyntaxSpace, | ||
giveAnnotationParam: fx.ParamTags(`name:"something"group:"something"`), | ||
giveAnnotationResult: fx.ResultTags(`name:"something"group:"something"`), | ||
}, | ||
{ | ||
give: "Tags key not equal to group, name or optional", | ||
wantErr: errTagKeySyntax, | ||
giveAnnotationParam: fx.ParamTags(`name1:"something"`), | ||
giveAnnotationResult: fx.ResultTags(`name1:"something"`), | ||
}, | ||
{ | ||
give: "Tags key empty", | ||
wantErr: errTagKeySyntax, | ||
giveAnnotationParam: fx.ParamTags(`:"something"`), | ||
giveAnnotationResult: fx.ResultTags(`:"something"`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run("Param "+tt.give, func(t *testing.T) { | ||
app := NewForTest(t, | ||
fx.Provide( | ||
fx.Annotate( | ||
newA, | ||
tt.giveAnnotationParam, | ||
), | ||
), | ||
fx.Invoke(newB), | ||
) | ||
assert.ErrorContains(t, app.Err(), tt.wantErr) | ||
}) | ||
t.Run("Result "+tt.give, func(t *testing.T) { | ||
app := NewForTest(t, | ||
fx.Provide( | ||
fx.Annotate( | ||
newA, | ||
tt.giveAnnotationResult, | ||
), | ||
), | ||
fx.Invoke(newB), | ||
) | ||
assert.ErrorContains(t, app.Err(), tt.wantErr) | ||
}) | ||
} | ||
} | ||
|
||
func TestAnnotateApplySuccess(t *testing.T) { | ||
type a struct{} | ||
type b struct{ a *a } | ||
newA := func() *a { return &a{} } | ||
newB := func(a *a) *b { | ||
return &b{a} | ||
} | ||
|
||
tests := []struct { | ||
give string | ||
giveAnnotationParam fx.Annotation | ||
giveAnnotationResult fx.Annotation | ||
}{ | ||
{ | ||
give: "ParamTags Tag Empty", | ||
giveAnnotationParam: fx.ParamTags(` `), | ||
giveAnnotationResult: fx.ResultTags(` `), | ||
}, | ||
{ | ||
give: "ParamTags Tag Empty with extra spaces", | ||
giveAnnotationParam: fx.ParamTags(`name:"versionNum"`, ` `), | ||
giveAnnotationResult: fx.ResultTags(` `, `group:"versionNum"`), | ||
}, | ||
{ | ||
give: "ParamTags Tag with \\ ", | ||
giveAnnotationParam: fx.ParamTags(`name:"version\\Num"`, ` `), | ||
giveAnnotationResult: fx.ResultTags(``, `group:"version\\Num"`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.give, func(t *testing.T) { | ||
app := NewForTest(t, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you're also using t.Parallel() at the top-level. You'll also need to call t.Parallel() within these subtests if you want them to run in parallel. |
||
fx.Provide( | ||
fx.Annotate( | ||
newA, | ||
tt.giveAnnotationParam, | ||
tt.giveAnnotationResult, | ||
), | ||
), | ||
fx.Invoke(newB), | ||
) | ||
require.NoError(t, app.Err()) | ||
}) | ||
} | ||
|
||
} | ||
func assertApp( | ||
t *testing.T, | ||
app interface { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just check if the value string is prefixed and suffixed with double quotes. Could the step to validate tag values is within quotes be replaced by counting the total number of double quotes and ensuring that is exactly 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The total number of double quotes can be more than 2, as it's valid for a struct string to have quotes in it.
key:"val/"ue"
would be a valid tag with more than 2 double quotes. Hence, I don't think it'd be possible unless I misunderstood?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah understood that this approach does not work because value actually contains the rest of the tag strings.