Skip to content

Commit

Permalink
Merge pull request #134 from debricked/122-add-support-for-exclusion-…
Browse files Browse the repository at this point in the history
…through-env

Add option to use comma seperated env var for default exclusion
  • Loading branch information
sweoggy authored Oct 19, 2023
2 parents 2712cae + 8992be3 commit bb1b824
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 42 deletions.
4 changes: 3 additions & 1 deletion internal/cmd/files/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

var exclusions = file.DefaultExclusions()
var exclusions = file.Exclusions()
var jsonPrint bool
var lockfileOnly bool
var strictness int
Expand Down Expand Up @@ -46,6 +46,8 @@ Special Terms | Meaning
"[class]" | matches any single non-Separator character against a class of characters ([see "character classes"])
"{alt1,...}" | matches a sequence of characters if one of the comma-separated alternatives matches
Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2,path3".
Example:
$ debricked files find . `+exampleFlags)

Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/viper"
)

var exclusions = file.DefaultExclusions()
var exclusions = file.Exclusions()

const (
ExclusionFlag = "exclusion"
Expand Down Expand Up @@ -41,6 +41,8 @@ Special Terms | Meaning
"[class]" | matches any single non-Separator character against a class of characters ([see "character classes"])
"{alt1,...}" | matches a sequence of characters if one of the comma-separated alternatives matches
Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2,path3".
Example:
$ debricked files resolve . `+exampleFlags)

Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var branchName string
var commitAuthor string
var repositoryUrl string
var integrationName string
var exclusions = file.DefaultExclusions()
var exclusions = file.Exclusions()
var noResolve bool
var noFingerprint bool
var passOnDowntime bool
Expand Down Expand Up @@ -79,6 +79,8 @@ Special Terms | Meaning
"[class]" | matches any single non-Separator character against a class of characters ([see "character classes"])
"{alt1,...}" | matches a sequence of characters if one of the comma-separated alternatives matches
Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2,path3".
Examples:
$ debricked scan . `+exampleFlags)
cmd.Flags().BoolVarP(&passOnDowntime, PassOnTimeOut, "p", false, "pass scan if there is a service access timeout")
Expand Down
38 changes: 0 additions & 38 deletions internal/file/default_exclusion_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package file

import "path/filepath"
import (
"os"
"path/filepath"
"strings"
)

const debrickedExclusionEnvVar = "DEBRICKED_EXCLUSIONS"

func DefaultExclusions() []string {
return []string{
Expand All @@ -11,6 +17,17 @@ func DefaultExclusions() []string {
}
}

func Exclusions() []string {
values := DefaultExclusions()

envValue := os.Getenv(debrickedExclusionEnvVar)
if envValue != "" {
values = strings.Split(envValue, ",")
}

return values
}

var EXCLUDED_DIRS_FINGERPRINT = []string{
"nbproject", "nbbuild", "nbdist", "node_modules",
"__pycache__", "_yardoc", "eggs",
Expand Down
85 changes: 85 additions & 0 deletions internal/file/exclusion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package file

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDefaultExclusions(t *testing.T) {
separator := string(os.PathSeparator)
for _, ex := range DefaultExclusions() {
exParts := strings.Split(ex, separator)
assert.Greaterf(t, len(exParts), 0, "failed to assert that %s used correct separator. Proper separator %s", ex, separator)
}
}

func TestExclusionsWithTokenEnvVariable(t *testing.T) {
oldEnvValue := os.Getenv(debrickedExclusionEnvVar)
err := os.Setenv(debrickedExclusionEnvVar, "*/**.lock,**/node_modules/**,*\\**.ex")

if err != nil {
t.Fatalf("failed to set env var %s", debrickedExclusionEnvVar)
}

defer func(key, value string) {
err := os.Setenv(key, value)
if err != nil {
t.Fatalf("failed to reset env var %s", debrickedExclusionEnvVar)
}
}(debrickedExclusionEnvVar, oldEnvValue)

gt := []string{"*/**.lock", "**/node_modules/**", "*\\**.ex"}
exclusions := Exclusions()
assert.Equal(t, gt, exclusions)

}

func TestExclusionsWithEmptyTokenEnvVariable(t *testing.T) {
oldEnvValue := os.Getenv(debrickedExclusionEnvVar)
err := os.Setenv(debrickedExclusionEnvVar, "")

if err != nil {
t.Fatalf("failed to set env var %s", debrickedExclusionEnvVar)
}

defer func(key, value string) {
err := os.Setenv(key, value)
if err != nil {
t.Fatalf("failed to reset env var %s", debrickedExclusionEnvVar)
}
}(debrickedExclusionEnvVar, oldEnvValue)

gt := []string{
filepath.Join("**", "node_modules", "**"),
filepath.Join("**", "vendor", "**"),
filepath.Join("**", ".git", "**"),
filepath.Join("**", "obj", "**"),
}
defaultExclusions := Exclusions()
assert.Equal(t, gt, defaultExclusions)
}

func TestDefaultExclusionsFingerprint(t *testing.T) {
expectedExclusions := []string{
filepath.Join("**", "nbproject", "**"),
filepath.Join("**", "nbbuild", "**"),
filepath.Join("**", "nbdist", "**"),
filepath.Join("**", "node_modules", "**"),
filepath.Join("**", "__pycache__", "**"),
filepath.Join("**", "_yardoc", "**"),
filepath.Join("**", "eggs", "**"),
filepath.Join("**", "wheels", "**"),
filepath.Join("**", "htmlcov", "**"),
filepath.Join("**", "__pypackages__", "**"),
"**/*.egg-info/**",
"**/*venv/**",
}

exclusions := DefaultExclusionsFingerprint()

assert.ElementsMatch(t, expectedExclusions, exclusions, "DefaultExclusionsFingerprint did not return the expected exclusions")
}

0 comments on commit bb1b824

Please sign in to comment.