Skip to content

Commit

Permalink
Merge pull request #7 from vossenwout/feature/more-extensions-to-stan…
Browse files Browse the repository at this point in the history
…dard-exclude

Include extension filtering capability
  • Loading branch information
vossenwout authored Sep 12, 2024
2 parents 58fc382 + d48ca30 commit 7d50250
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 22 deletions.
12 changes: 7 additions & 5 deletions .crev-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Configuration for the crev tool
# specify your Code AI Review API key (necessary for review command)
api-key: "dcds"
# specify the prefixes of files and directories to ignore (paths starting with . are always ignored)
ignore: [tests, build, readme.md, LICENSE, dist]
# specify the extensions of files to include (by default all files are included)
extensions: # ex. [.go, .py, .js]
api-key: # Fill in your CREV API key here
# specify the prefixes of files and directories to ignore (by default common configuration files are ignored)
ignore-pre: # ex. [tests, readme.md, scripts]
# specify the extensions of files to ignore
ignore-ext: [.go, .py, .js, .txt]
# specify the extensions of files to include
include-ext: # ex. [.go, .py, .js]
27 changes: 19 additions & 8 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ crev generate --ignore=tests,readme.md --extensions=go,py,js

// get all file paths from the root directory
rootDir := "."
prefixesToFilter := viper.GetStringSlice("ignore")
// always ignore directories starting with "."

prefixesToFilter := viper.GetStringSlice("ignore-pre")
prefixesToFilter = append(prefixesToFilter, standardPrefixesToFilter...)
extensionsToKeep := viper.GetStringSlice("extensions")
filePaths, err := files.GetAllFilePaths(rootDir, prefixesToFilter, extensionsToKeep)

extensionsToIgnore := viper.GetStringSlice("ignore-ext")

extensionsToKeep := viper.GetStringSlice("include-ext")

filePaths, err := files.GetAllFilePaths(rootDir, prefixesToFilter,
extensionsToKeep, extensionsToIgnore)
if err != nil {
log.Fatal(err)
return
Expand Down Expand Up @@ -122,13 +127,19 @@ crev generate --ignore=tests,readme.md --extensions=go,py,js

func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().StringSlice("ignore", []string{}, "Comma-separated prefixes of paths to ignore")
generateCmd.Flags().StringSlice("extensions", []string{}, "Comma-separated file extensions to include. (default: all files)")
err := viper.BindPFlag("ignore", generateCmd.Flags().Lookup("ignore"))
// TODO Fix description with defaults
generateCmd.Flags().StringSlice("ignore-pre", []string{}, "Comma-separated prefixes of paths to ignore")
generateCmd.Flags().StringSlice("ignore-ext", []string{}, "Comma-separated file extensions to ignore")
generateCmd.Flags().StringSlice("include-ext", []string{}, "Comma-separated file extensions to include.")
err := viper.BindPFlag("ignore-pre", generateCmd.Flags().Lookup("ignore-pre"))
if err != nil {
log.Fatal(err)
}
err = viper.BindPFlag("ignore-ext", generateCmd.Flags().Lookup("ignore-ext"))
if err != nil {
log.Fatal(err)
}
err = viper.BindPFlag("extensions", generateCmd.Flags().Lookup("extensions"))
err = viper.BindPFlag("include-ext", generateCmd.Flags().Lookup("include-ext"))
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ var defaultConfig = []byte(`# Configuration for the crev tool
# specify your Code AI Review API key (necessary for review command)
api-key: # Fill in your CREV API key here
# specify the prefixes of files and directories to ignore (by default common configuration files are ignored)
ignore: # ex. [tests, readme.md, scripts]
# specify the extensions of files to include (by default all files are included)
extensions: # ex. [.go, .py, .js]
ignore-pre: # ex. [tests, readme.md, scripts]
# specify the extensions of files to ignore
ignore-ext: # ex. [.go, .py, .js]
# specify the extensions of files to include
include-ext: # ex. [.go, .py, .js]
`)

var initCmd = &cobra.Command{
Expand Down
9 changes: 8 additions & 1 deletion internal/files/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// Given a root path returns all the file paths in the root directory
// and its subdirectories.
func GetAllFilePaths(root string, prefixesToFilter []string, extensionsToKeep []string) ([]string, error) {
func GetAllFilePaths(root string, prefixesToFilter []string, extensionsToKeep []string,
extensionsToIgnore []string) ([]string, error) {
var filePaths []string
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -30,6 +31,12 @@ func GetAllFilePaths(root string, prefixesToFilter []string, extensionsToKeep []
return nil
}
}
// Filter out the files that have the extensions in extensionsToFilter.
for _, ext := range extensionsToIgnore {
if filepath.Ext(path) == ext {
return nil
}
}
// Process file based on extension filters.
if d.IsDir() || len(extensionsToKeep) == 0 {
filePaths = append(filePaths, path)
Expand Down
103 changes: 98 additions & 5 deletions tests/files/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGetAllFilePaths(t *testing.T) {
filepath.Join(subDir, "file2.txt"),
}

filePaths, err := files.GetAllFilePaths(rootDir, nil, nil)
filePaths, err := files.GetAllFilePaths(rootDir, nil, nil, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestGetAllFilePathsWithPrefixFilter(t *testing.T) {
filepath.Join(subDir2, "file3.go"),
}
// filter out full path prefix subDir1
filePaths, err := files.GetAllFilePaths(rootDir, []string{"subdir_1"}, nil)
filePaths, err := files.GetAllFilePaths(rootDir, []string{"subdir_1"}, nil, nil)

if err != nil {
t.Fatalf("expected no error, got %v", err)
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestGetAllFilePathsWithPrefixFilterNestedDir(t *testing.T) {
filepath.Join(subDir3, "file2.go"),
}

filePaths, err := files.GetAllFilePaths(rootDir, []string{"."}, nil)
filePaths, err := files.GetAllFilePaths(rootDir, []string{"."}, nil, nil)

if err != nil {
t.Fatalf("expected no error, got %v", err)
Expand All @@ -170,7 +170,7 @@ func TestGetAllFilePathsWithPrefixFilterNestedDir(t *testing.T) {
}
}

// Tests the functionality to filter out text extensions.
// Tests the functionality to include only files with specific extensions.
func TestGetAllFilePathsWithExtensionFilter(t *testing.T) {
rootDir := t.TempDir()

Expand Down Expand Up @@ -204,7 +204,7 @@ func TestGetAllFilePathsWithExtensionFilter(t *testing.T) {
subDir2,
}

filePaths, err := files.GetAllFilePaths(rootDir, nil, []string{".go"})
filePaths, err := files.GetAllFilePaths(rootDir, nil, []string{".go"}, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand All @@ -227,6 +227,99 @@ func TestGetAllFilePathsWithExtensionFilter(t *testing.T) {
}
}

// Tests the functionality to exclude files with specific extensions.

func TestGetAllFilePathsWithExtensionFilterExclude(t *testing.T) {
rootDir := t.TempDir()

// Create subdirectories and nested subdirectories
subDir1 := filepath.Join(rootDir, "subdir_1")
err := os.Mkdir(subDir1, 0755)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

nestedSubDir1 := filepath.Join(subDir1, "nested_subdir_1")
err = os.Mkdir(nestedSubDir1, 0755)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

subDir2 := filepath.Join(rootDir, "subdir_2")
err = os.Mkdir(subDir2, 0755)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

nestedSubDir2 := filepath.Join(subDir2, "nested_subdir_2")
err = os.Mkdir(nestedSubDir2, 0755)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

// Create files in various directories
err = os.WriteFile(filepath.Join(rootDir, "file1.go"), []byte("content1"), 0644)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
err = os.WriteFile(filepath.Join(subDir1, "file2.go"), []byte("content2"), 0644)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
err = os.WriteFile(filepath.Join(nestedSubDir1, "file3.go"), []byte("content3"), 0644)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
err = os.WriteFile(filepath.Join(subDir2, "file4.txt"), []byte("content4"), 0644)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
err = os.WriteFile(filepath.Join(nestedSubDir2, "file5.md"), []byte("content5"), 0644)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
err = os.WriteFile(filepath.Join(nestedSubDir2, "file6.txt"), []byte("content6"), 0644)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

// Expected result: exclude .txt files, keep the rest
expected := []string{
filepath.Join(rootDir, "file1.go"),
filepath.Join(subDir1, "file2.go"),
filepath.Join(nestedSubDir1, "file3.go"),
subDir1,
subDir2,
nestedSubDir1,
nestedSubDir2,
}

// Get all file paths excluding .txt files
filePaths, err := files.GetAllFilePaths(rootDir, nil, nil, []string{".txt", ".md"})
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

// Check the number of files found
if len(filePaths) != len(expected) {
t.Fatalf("expected %d files, got %d", len(expected), len(filePaths))
}

// Verify that each expected file is in the result
for _, exp := range expected {
found := false
for _, fp := range filePaths {
if fp == exp {
found = true
break
}
}
if !found {
t.Errorf("expected path %s not found in result", exp)
}
}
}

// Tests the functionality to read all the content of files and directories and create a
// file content map.
func TestGetContentMapOfFiles(t *testing.T) {
Expand Down

0 comments on commit 7d50250

Please sign in to comment.