...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common/regexp_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common

     1  // Copyright 2020 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	"gopkg.in/yaml.v2"
    24  )
    25  
    26  func TestRegexpUnmarshal(t *testing.T) {
    27  	t.Run("json", func(t *testing.T) {
    28  		var r Regexp
    29  		require.NoError(t, json.Unmarshal([]byte(`"test/.*"`), &r), "failed to unmarshal json")
    30  
    31  		assert.True(t, r.Matches("test/path/to/file.txt"))
    32  		assert.False(t, r.Matches("something/else/path/to/file.txt"))
    33  	})
    34  
    35  	t.Run("jsonEmpty", func(t *testing.T) {
    36  		var r Regexp
    37  		require.NoError(t, json.Unmarshal([]byte(`""`), &r), "failed to unmarshal json")
    38  
    39  		assert.True(t, r.Matches("any string"))
    40  		assert.True(t, r.Matches(""))
    41  	})
    42  
    43  	t.Run("jsonError", func(t *testing.T) {
    44  		var r Regexp
    45  		require.Error(t, json.Unmarshal([]byte(`"this(is an unclosed [group"`), &r), "invalid regexp unmarshalled without error")
    46  	})
    47  
    48  	t.Run("yaml", func(t *testing.T) {
    49  		var r Regexp
    50  		require.NoError(t, yaml.Unmarshal([]byte(`"test/.*"`), &r), "failed to unmarshal yaml")
    51  
    52  		assert.True(t, r.Matches("test/path/to/file.txt"))
    53  		assert.False(t, r.Matches("something/else/path/to/file.txt"))
    54  	})
    55  
    56  	t.Run("yamlEmpty", func(t *testing.T) {
    57  		var r Regexp
    58  		require.NoError(t, yaml.Unmarshal([]byte(`""`), &r), "failed to unmarshal yaml")
    59  
    60  		assert.True(t, r.Matches("any string"))
    61  		assert.True(t, r.Matches(""))
    62  	})
    63  
    64  	t.Run("yamlError", func(t *testing.T) {
    65  		var r Regexp
    66  		require.Error(t, yaml.Unmarshal([]byte(`"this(is an unclosed [group"`), &r), "invalid regexp unmarshalled without error")
    67  	})
    68  }
    69  

View as plain text