generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support parsing Allure labels from tags
- Loading branch information
Showing
5 changed files
with
190 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2025 The AlaudaDevops Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package allure | ||
|
||
import ( | ||
"regexp" | ||
|
||
messages "github.com/cucumber/messages/go/v21" | ||
"github.com/godogx/allure/report" | ||
) | ||
|
||
// AllureLabelReg is a regular expression pattern for matching allure label tags. | ||
var AllureLabelReg = regexp.MustCompile(`^@?allure\.label\.([^.]+):(.+)$`) | ||
|
||
// matchAllureLabel parses a tag string to extract allure label key and value. | ||
// | ||
// Examples: | ||
// | ||
// matchAllureLabel("@allure.label.epic:hello world") // returns "epic", "hello world", true | ||
// matchAllureLabel("allure.label.story:play") // returns "story", "play", true | ||
// matchAllureLabel("@other.tag") // returns "", "", false | ||
func matchAllureLabel(tag string) (key, value string, ok bool) { | ||
matches := AllureLabelReg.FindStringSubmatch(tag) | ||
if len(matches) != 3 { | ||
return "", "", false | ||
} | ||
return matches[1], matches[2], true | ||
} | ||
|
||
// GetAllureLabelsFromTags parse Allure labels based on tags provided by the user | ||
func GetAllureLabelsFromTags(tags []*messages.PickleTag) []report.Label { | ||
labels := []report.Label{} | ||
for _, tag := range tags { | ||
if key, value, ok := matchAllureLabel(tag.Name); ok { | ||
labels = append(labels, report.Label{Name: key, Value: value}) | ||
} | ||
} | ||
return labels | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package allure | ||
|
||
import ( | ||
"testing" | ||
|
||
messages "github.com/cucumber/messages/go/v21" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMatchAllureLabel(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
wantKey string | ||
wantVal string | ||
wantBool bool | ||
}{ | ||
{ | ||
name: "tag with @ prefix", | ||
input: "@allure.label.epic:hello world", | ||
wantKey: "epic", | ||
wantVal: "hello world", | ||
wantBool: true, | ||
}, | ||
{ | ||
name: "tag without @ prefix", | ||
input: "allure.label.story:play", | ||
wantKey: "story", | ||
wantVal: "play", | ||
wantBool: true, | ||
}, | ||
{ | ||
name: "non allure tag", | ||
input: "@other.tag", | ||
wantKey: "", | ||
wantVal: "", | ||
wantBool: false, | ||
}, | ||
{ | ||
name: "invalid tag format", | ||
input: "@allure.label.", | ||
wantKey: "", | ||
wantVal: "", | ||
wantBool: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
key, val, ok := matchAllureLabel(tt.input) | ||
assert.Equal(t, tt.wantKey, key) | ||
assert.Equal(t, tt.wantVal, val) | ||
assert.Equal(t, tt.wantBool, ok) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetAllureLabelsFromTags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputTags []*messages.PickleTag | ||
expectedCount int | ||
expectedTags []struct { | ||
name string | ||
value string | ||
} | ||
}{ | ||
{ | ||
name: "multiple valid tags", | ||
inputTags: []*messages.PickleTag{ | ||
{Name: "@allure.label.epic:MyEpic"}, | ||
{Name: "@allure.label.story:MyStory"}, | ||
{Name: "allure.label.severity:critical"}, | ||
}, | ||
expectedCount: 3, | ||
expectedTags: []struct { | ||
name string | ||
value string | ||
}{ | ||
{"epic", "MyEpic"}, | ||
{"story", "MyStory"}, | ||
{"severity", "critical"}, | ||
}, | ||
}, | ||
{ | ||
name: "mixed valid and invalid tags", | ||
inputTags: []*messages.PickleTag{ | ||
{Name: "@some.other.tag"}, | ||
{Name: "@allure.label.epic:MyEpic"}, | ||
{Name: "not a label tag"}, | ||
}, | ||
expectedCount: 1, | ||
expectedTags: []struct { | ||
name string | ||
value string | ||
}{ | ||
{"epic", "MyEpic"}, | ||
}, | ||
}, | ||
{ | ||
name: "empty tag list", | ||
inputTags: []*messages.PickleTag{}, | ||
expectedCount: 0, | ||
expectedTags: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
labels := GetAllureLabelsFromTags(tt.inputTags) | ||
assert.Equal(t, tt.expectedCount, len(labels)) | ||
|
||
if tt.expectedCount > 0 { | ||
for i, expected := range tt.expectedTags { | ||
assert.Equal(t, expected.name, labels[i].Name) | ||
assert.Equal(t, expected.value, labels[i].Value) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Empty file.