Skip to content

Commit

Permalink
add exclude files in the configuration (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-c4t authored Dec 12, 2024
1 parent d403b71 commit 419f95f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
go-version: 1.22
cache: false
- name: run test
run: go test ./...
run: go test -p 1 ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ _test-files
.vscode

camino-license
config.yaml
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ custom-headers:

exclude-paths:
- "./**/camino_visitor2.go"

headers-excluded-paths:
- "./**/excluded_*.go"

```
`default-headers`: If the file is not specified in the custom-headers paths, then it should contain one of the default headers
Expand All @@ -60,4 +63,6 @@ custom-headers:

`exclude-paths`: list of path pattern to identify files which will be excluded from `include-paths`. Use `**`to include all sub directories. Use `*` to include all matching pattern in the same directory.

`headers-excluded-paths`: list of path pattern to identify files which will be excluded from license header checking. Use `**`to include all sub directories. Use `*` to include all matching pattern in the same directory.

`{YEAR}`: It will be automatically replaced with current year when the check runs.
3 changes: 3 additions & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ custom-headers:

exclude-paths:
- "./**/camino_visitor2.go"

headers-excluded-paths:
- "./**/excluded_*.go"
28 changes: 13 additions & 15 deletions pkg/camino-license/camino-license.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ func (h CaminoLicenseHeader) CheckLicense(files []string) ([]WrongLicenseHeader,
continue
}
for _, path := range pathFiles {
// TODO: set license exclusions to be configured in the configuration file
match, matchErr := filepath.Match("mock_*.go", filepath.Base(path))
if strings.HasSuffix(path, ".pb.go") || matchErr != nil || match {
continue
}
isWrong, wrongFile := h.checkFileLicense(path)
if isWrong {
wrongFiles = append(wrongFiles, wrongFile)
Expand All @@ -76,16 +71,23 @@ func (h CaminoLicenseHeader) CheckLicense(files []string) ([]WrongLicenseHeader,

// To check if a file should have a custom license header or one of the default ones
func (h CaminoLicenseHeader) checkFileLicense(f string) (bool, WrongLicenseHeader) {
isCustomHeader, headerName, header := h.checkCustomHeader(f)
absFile, fileErr := filepath.Abs(f)
if fileErr != nil {
return true, WrongLicenseHeader{f, "Couldn't get the absolute path of this file"}
}
if slices.Contains(h.Config.ExcludedFiles, absFile) {
return false, WrongLicenseHeader{}
}
isCustomHeader, headerName, header := h.checkCustomHeader(absFile)
if isCustomHeader {
correctLicense, reason := verifyCustomLicenseHeader(f, headerName, header)
correctLicense, reason := verifyCustomLicenseHeader(absFile, headerName, header)
if !correctLicense {
return true, WrongLicenseHeader{f, reason}
return true, WrongLicenseHeader{absFile, reason}
}
} else {
correctLicense, reason := h.verifyDefaultLicenseHeader(f)
correctLicense, reason := h.verifyDefaultLicenseHeader(absFile)
if !correctLicense {
return true, WrongLicenseHeader{f, reason}
return true, WrongLicenseHeader{absFile, reason}
}
}
return false, WrongLicenseHeader{}
Expand All @@ -94,11 +96,7 @@ func (h CaminoLicenseHeader) checkFileLicense(f string) (bool, WrongLicenseHeade
// to check if the file is included in a custom header path
func (h CaminoLicenseHeader) checkCustomHeader(file string) (bool, string, string) {
for _, customHeader := range h.Config.CustomHeaders {
absFile, fileErr := filepath.Abs(file)
if fileErr != nil {
absFile = file
}
if slices.Contains(customHeader.AllFiles, absFile) && !slices.Contains(customHeader.ExcludedFiles, absFile) {
if slices.Contains(customHeader.AllFiles, file) && !slices.Contains(customHeader.ExcludedFiles, file) {
return true, customHeader.Name, customHeader.Header
}
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/camino-license/camino-license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package caminolicense
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -53,9 +54,10 @@ func TestWrongDefaultLicense(t *testing.T) {
h := CaminoLicenseHeader{Config: headersConfig}
wrongFiles, err := h.CheckLicense([]string{"./test_wrong_default.go"})
require.ErrorIs(t, err, CheckErr)
currentPath, _ := filepath.Abs(".")
expectedWrongFiles := []WrongLicenseHeader{
{
File: "./test_wrong_default.go",
File: currentPath + "/test_wrong_default.go",
Reason: defaultHeaderError,
},
}
Expand All @@ -82,17 +84,28 @@ func TestWrongCustomLicense(t *testing.T) {
h := CaminoLicenseHeader{Config: headersConfig2}
wrongFiles, err := h.CheckLicense([]string{"./camino_test_wrong_custom.go", "./camino_test_exclude.go"})
require.ErrorIs(t, err, CheckErr)
currentPath, _ := filepath.Abs(".")
expectedWrongFiles := []WrongLicenseHeader{
{
File: "./camino_test_wrong_custom.go",
File: currentPath + "/camino_test_wrong_custom.go",
Reason: customHeaderError + headersConfig2.CustomHeaders[0].Name,
},
{
File: "./camino_test_exclude.go",
File: currentPath + "/camino_test_exclude.go",
Reason: defaultHeaderError,
},
}
require.Equal(t, expectedWrongFiles, wrongFiles)
require.NoError(t, os.Remove("./camino_test_wrong_custom.go"))
require.NoError(t, os.Remove("./camino_test_exclude.go"))
}

func TestExcludedFiles(t *testing.T) {
require.NoError(t, os.WriteFile("./excluded_wrong_default_1.go", []byte(fmt.Sprintf("// Copyright (C) 1000-%d, Chain4Travel AG. All rights reserved.\n// Lexclude\n\n package caminolicense", time.Now().Year())), 0o600))
headersConfig2, _ := config.GetHeadersConfig("../config_test.yaml")
h := CaminoLicenseHeader{Config: headersConfig2}
wrongFiles, err := h.CheckLicense([]string{"./excluded_wrong_default_1.go"})
require.NoError(t, err)
require.Empty(t, wrongFiles)
require.NoError(t, os.Remove("./excluded_wrong_default_1.go"))
}
28 changes: 26 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type CustomHeader struct {
}

type HeadersConfig struct {
DefaultHeaders []DefaultHeader `yaml:"default-headers"`
CustomHeaders []CustomHeader `yaml:"custom-headers"`
DefaultHeaders []DefaultHeader `yaml:"default-headers"`
CustomHeaders []CustomHeader `yaml:"custom-headers"`
ExcludedHeadersPaths []string `yaml:"headers-excluded-paths"`
ExcludedFiles []string
}

// read configuration file
Expand Down Expand Up @@ -62,6 +64,11 @@ func GetHeadersConfig(configPath string) (HeadersConfig, error) {
}
headersConfig.CustomHeaders[i].ExcludedFiles = excludedFiles
}
excludedFiles, err := getExcludedFiles(headersConfig.ExcludedHeadersPaths, filepath.Dir(configAbsPath))
if err != nil {
return HeadersConfig{}, fmt.Errorf("failed to read config file %s: %w", configPath, err)
}
headersConfig.ExcludedFiles = excludedFiles
return *headersConfig, nil
}

Expand Down Expand Up @@ -98,3 +105,20 @@ func getCustomHeaderExcludedFiles(customHeader CustomHeader, dir string) ([]stri
}
return files, nil
}

// walk through directories of headers-excluded-paths to get all possible files that matches the pattern
func getExcludedFiles(paths []string, dir string) ([]string, error) {
var files []string
for _, path := range paths {
absPath := path
if !filepath.IsAbs(path) {
absPath = filepath.Join(dir, path)
}
pathFiles, err := filepathx.Glob(absPath)
if err != nil {
return files, errors.New("Cannot get file matches of the custom header excluded path: " + path)
}
files = append(files, pathFiles...)
}
return files, nil
}
2 changes: 2 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func TestReadConfig(t *testing.T) {
AllFiles: []string{currentPath + "/camino-license/camino-license.go", currentPath + "/camino-license/camino-license_test.go"},
},
},
[]string{"./**/excluded_*.go"},
[]string(nil),
}
require.Equal(t, expectedHeadersConfig, headersConfig)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ custom-headers:

exclude-paths:
- "./**/camino_*exclude.go"

headers-excluded-paths:
- "./**/excluded_*.go"

0 comments on commit 419f95f

Please sign in to comment.