Skip to content

Commit

Permalink
feat: support parsing Allure labels from tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm committed Jan 17, 2025
1 parent fe947a7 commit f7a9144
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 8 deletions.
3 changes: 3 additions & 0 deletions _testdata/Failed.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@allure.label.epic:test
@allure.label.feature:failing scenarios
Feature: failing scenarios

@allure.label.epic:test
Scenario: pass then fail 1
When I pass
And I sleep for a bit
Expand Down
21 changes: 13 additions & 8 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,24 @@ type scenarioContext struct {
// Pickle receives scenario.
func (f *formatter) Pickle(scenario *godog.Scenario) {
feature := f.Storage.MustGetFeature(scenario.Uri)

labels := GetAllureLabelsFromTags(scenario.Tags)
labels = append(labels, []report.Label{
{Name: "feature", Value: feature.Feature.Name},
{Name: "suite", Value: f.Container.Name},
{Name: "epic", Value: f.Container.Name},
{Name: "framework", Value: "godog"},
{Name: "language", Value: "Go"},
}...)

res := report.Result{
Name: scenario.Name,
HistoryID: feature.Feature.Name + ": " + scenario.Name + scenario.Id,
FullName: scenario.Uri + ":" + scenario.Name,
Description: scenario.Uri,
Labels: []report.Label{
{Name: "feature", Value: feature.Feature.Name},
{Name: "suite", Value: f.Container.Name},
{Name: "framework", Value: "godog"},
{Name: "language", Value: "Go"},
},
Start: report.GetTimestampMs(),
UUID: uuid.New().String(),
Labels: labels,
Start: report.GetTimestampMs(),
UUID: uuid.New().String(),
}

f.mu.Lock()
Expand Down
53 changes: 53 additions & 0 deletions label.go
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
}
121 changes: 121 additions & 0 deletions label_test.go
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 added suite_test.go
Empty file.

0 comments on commit f7a9144

Please sign in to comment.