feat: add support for "none" scope (#1800)

This commit is contained in:
nils måsén 2023-10-21 20:57:58 +02:00 committed by GitHub
parent 40b8c77100
commit dd54055143
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 19 deletions

View file

@ -86,13 +86,14 @@ func FilterByDisabledLabel(baseFilter t.Filter) t.Filter {
// FilterByScope returns all containers that belongs to a specific scope
func FilterByScope(scope string, baseFilter t.Filter) t.Filter {
if scope == "" {
return baseFilter
}
return func(c t.FilterableContainer) bool {
containerScope, ok := c.Scope()
if ok && containerScope == scope {
containerScope, containerHasScope := c.Scope()
if !containerHasScope || containerScope == "" {
containerScope = "none"
}
if containerScope == scope {
return baseFilter(c)
}
@ -152,7 +153,13 @@ func BuildFilter(names []string, disableNames []string, enableLabel bool, scope
filter = FilterByEnableLabel(filter)
sb.WriteString("using enable label, ")
}
if scope != "" {
if scope == "none" {
// If a scope has explicitly defined as "none", containers should only be considered
// if they do not have a scope defined, or if it's explicitly set to "none".
filter = FilterByScope(scope, filter)
sb.WriteString(`without a scope, "`)
} else if scope != "" {
// If a scope has been defined, containers should only be considered
// if the scope is specifically set.
filter = FilterByScope(scope, filter)

View file

@ -111,6 +111,53 @@ func TestFilterByScope(t *testing.T) {
container.AssertExpectations(t)
}
func TestFilterByNoneScope(t *testing.T) {
scope := "none"
filter := FilterByScope(scope, NoFilter)
assert.NotNil(t, filter)
container := new(mocks.FilterableContainer)
container.On("Scope").Return("anyscope", true)
assert.False(t, filter(container))
container.AssertExpectations(t)
container = new(mocks.FilterableContainer)
container.On("Scope").Return("", false)
assert.True(t, filter(container))
container.AssertExpectations(t)
container = new(mocks.FilterableContainer)
container.On("Scope").Return("", true)
assert.True(t, filter(container))
container.AssertExpectations(t)
container = new(mocks.FilterableContainer)
container.On("Scope").Return("none", true)
assert.True(t, filter(container))
container.AssertExpectations(t)
}
func TestBuildFilterNoneScope(t *testing.T) {
filter, desc := BuildFilter(nil, nil, false, "none")
assert.Contains(t, desc, "without a scope")
scoped := new(mocks.FilterableContainer)
scoped.On("Enabled").Return(false, false)
scoped.On("Scope").Return("anyscope", true)
unscoped := new(mocks.FilterableContainer)
unscoped.On("Enabled").Return(false, false)
unscoped.On("Scope").Return("", false)
assert.False(t, filter(scoped))
assert.True(t, filter(unscoped))
scoped.AssertExpectations(t)
unscoped.AssertExpectations(t)
}
func TestFilterByDisabledLabel(t *testing.T) {
filter := FilterByDisabledLabel(NoFilter)
assert.NotNil(t, filter)