github.com/apache/skywalking-eyes@v0.6.0/pkg/header/check_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package header
    19  
    20  import (
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	"gopkg.in/yaml.v3"
    28  )
    29  
    30  func TestCheckFile(t *testing.T) {
    31  	type args struct {
    32  		name   string
    33  		file   string
    34  		result *Result
    35  	}
    36  
    37  	var c struct {
    38  		Header ConfigHeader `yaml:"header"`
    39  	}
    40  
    41  	require.NoError(t, os.Chdir("../.."))
    42  	content, err := os.ReadFile("test/testdata/.licenserc_for_test_check.yaml")
    43  	require.NoError(t, err)
    44  	require.NoError(t, yaml.Unmarshal(content, &c))
    45  	require.NoError(t, c.Header.Finalize())
    46  
    47  	t.Run("WithLicense", func(t *testing.T) {
    48  		tests := func() []args {
    49  			files, err := filepath.Glob("test/testdata/include_test/with_license/*")
    50  			require.NoError(t, err)
    51  			var cases []args
    52  			for _, file := range files {
    53  				cases = append(cases, args{
    54  					name:   file,
    55  					file:   file,
    56  					result: &Result{},
    57  				})
    58  			}
    59  			return cases
    60  		}()
    61  		require.NotEmpty(t, tests)
    62  
    63  		for _, tt := range tests {
    64  			t.Run(tt.name, func(t *testing.T) {
    65  				require.NotEmpty(t, strings.TrimSpace(c.Header.GetLicenseContent()))
    66  				require.NoError(t, CheckFile(tt.file, &c.Header, tt.result))
    67  				require.Len(t, tt.result.Ignored, 0)
    68  				require.False(t, tt.result.HasFailure())
    69  			})
    70  		}
    71  	})
    72  
    73  	t.Run("WithoutLicense", func(t *testing.T) {
    74  		tests := func() []args {
    75  			files, err := filepath.Glob("test/testdata/include_test/without_license/*")
    76  			require.NoError(t, err)
    77  			var cases []args
    78  			for _, file := range files {
    79  				cases = append(cases, args{
    80  					name:   file,
    81  					file:   file,
    82  					result: &Result{},
    83  				})
    84  			}
    85  			return cases
    86  		}()
    87  		require.NotEmpty(t, tests)
    88  
    89  		for _, tt := range tests {
    90  			t.Run(tt.name, func(t *testing.T) {
    91  				require.NotEmpty(t, strings.TrimSpace(c.Header.GetLicenseContent()))
    92  				require.NoError(t, CheckFile(tt.file, &c.Header, tt.result))
    93  				require.Len(t, tt.result.Ignored, 0)
    94  				require.True(t, tt.result.HasFailure())
    95  			})
    96  		}
    97  	})
    98  }