Skip to content

Commit

Permalink
Add option to use comma seperated env var for default exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed Oct 18, 2023
1 parent 2712cae commit e992891
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/file/default_exclusion.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package file

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

const debrickedExclusionEnvVar = "DEBRICKED_EXCLUSIONS"

func DefaultExclusions() []string {
return []string{
defaultValues := []string{
filepath.Join("**", "node_modules", "**"),
filepath.Join("**", "vendor", "**"),
filepath.Join("**", ".git", "**"),
filepath.Join("**", "obj", "**"), // nuget
}

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

return defaultValues
}

var EXCLUDED_DIRS_FINGERPRINT = []string{
Expand Down
47 changes: 47 additions & 0 deletions internal/file/default_exclusion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestDefaultExclusions(t *testing.T) {
assert.Greaterf(t, len(exParts), 0, "failed to assert that %s used correct separator. Proper separator %s", ex, separator)
}
}

func TestDefaultExclusionsFingerprint(t *testing.T) {
expectedExclusions := []string{
filepath.Join("**", "nbproject", "**"),
Expand All @@ -36,3 +37,49 @@ func TestDefaultExclusionsFingerprint(t *testing.T) {

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

func TestDefaultExclusionsWithTokenEnvVariable(t *testing.T) {
oldEnvValue := os.Getenv(debrickedExclusionEnvVar)
gt := []string{"*/**.lock", "**/node_modules/**", "*\\**.ex"}
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)

defExclusions := DefaultExclusions()
assert.Equal(t, gt, defExclusions)

}

func TestDefaultExclusionsWithEmptyTokenEnvVariable(t *testing.T) {
oldEnvValue := os.Getenv(debrickedExclusionEnvVar)
err := os.Setenv(debrickedExclusionEnvVar, "")
gt := []string{
filepath.Join("**", "node_modules", "**"),
filepath.Join("**", "vendor", "**"),
filepath.Join("**", ".git", "**"),
filepath.Join("**", "obj", "**"),
}

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)

defExclusions := DefaultExclusions()
assert.Equal(t, gt, defExclusions)
}

0 comments on commit e992891

Please sign in to comment.