Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include extensions flag #13

Merged
merged 2 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Flags:
-e, --exclude strings Exclude directories or files from being scanned
--exclude-extensions strings Exclude extensions from being scanned
-h, --help help for gcloc
--include-extensions strings Include the extensions to be scanned
-o, --order string Sorting order <ASC,DESC> (default "DESC")
--order-by-blank Show results ordered by blank lines
--order-by-code Show results ordered by lines of code
Expand Down
12 changes: 9 additions & 3 deletions internal/app/gcloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewGClocCmd() *cobra.Command {
gclocCmd := &cobra.Command{
Use: "gcloc",
Short: "GCloc is a simple tool to count lines of code of many programming languages",
Version: "1.0.0",
Version: "1.1.1",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Expand Down Expand Up @@ -54,6 +54,11 @@ func getParams(cmd *cobra.Command, args []string) (gcloc.Params, error) {
pathToScan = args[0]
}

byFile, err := cmd.Flags().GetBool(constants.ByFileFlag)
if err != nil {
return gcloc.Params{}, err
}

excludePaths, err := cmd.Flags().GetStringSlice(constants.ExcludePathsFlag)
if err != nil {
return gcloc.Params{}, err
Expand All @@ -64,7 +69,7 @@ func getParams(cmd *cobra.Command, args []string) (gcloc.Params, error) {
return gcloc.Params{}, err
}

byFile, err := cmd.Flags().GetBool(constants.ByFileFlag)
includeExtensions, err := cmd.Flags().GetStringSlice(constants.IncludeExtensionsFlag)
if err != nil {
return gcloc.Params{}, err
}
Expand Down Expand Up @@ -106,9 +111,10 @@ func getParams(cmd *cobra.Command, args []string) (gcloc.Params, error) {

params := gcloc.Params{
Path: pathToScan,
ByFile: byFile,
ExcludePaths: excludePaths,
ExcludeExtensions: excludeExtensions,
ByFile: byFile,
IncludeExtensions: includeExtensions,
OrderByLang: orderByLang,
OrderByFile: orderByFile,
OrderByCode: orderByCode,
Expand Down
13 changes: 9 additions & 4 deletions internal/app/gcloc_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
)

var gclocFlags = map[string]flags.Flag{
constants.ByFileFlag: {
Kind: reflect.Bool,
DefaultValue: false,
Description: "Show result by file",
},
constants.ExcludePathsFlag: {
ShortName: "e",
Kind: reflect.Slice,
Expand All @@ -19,10 +24,10 @@ var gclocFlags = map[string]flags.Flag{
DefaultValue: []string{},
Description: "Exclude extensions from being scanned",
},
constants.ByFileFlag: {
Kind: reflect.Bool,
DefaultValue: false,
Description: "Show result by file",
constants.IncludeExtensionsFlag: {
Kind: reflect.Slice,
DefaultValue: []string{},
Description: "Include the extensions to be scanned",
},
constants.OrderByLangFlag: {
Kind: reflect.Bool,
Expand Down
3 changes: 2 additions & 1 deletion internal/constants/gcloc_flags_name.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package constants

const (
ByFileFlag = "by-file"
ExcludePathsFlag = "exclude"
ExcludeExtensionsFlag = "exclude-extensions"
ByFileFlag = "by-file"
IncludeExtensionsFlag = "include-extensions"
OrderByLangFlag = "order-by-lang"
OrderByFileFlag = "order-by-file"
OrderByCodeFlag = "order-by-code"
Expand Down
8 changes: 8 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Analyzer struct {
path string
excludePaths []string
excludeExtensions map[string]bool
includeExtensions map[string]bool
}

type FileMetadata struct {
Expand All @@ -23,13 +24,15 @@ func NewAnalyzer(
path string,
excludePaths []string,
excludeExtensions map[string]bool,
includeExtensions map[string]bool,
extensions map[string]string,
) *Analyzer {
return &Analyzer{
SupportedExtensions: extensions,
path: path,
excludePaths: excludePaths,
excludeExtensions: excludeExtensions,
includeExtensions: includeExtensions,
}
}

Expand Down Expand Up @@ -72,6 +75,11 @@ func (a *Analyzer) getFileExtension(path string) string {
}

func (a *Analyzer) canAdd(path string, extension string) bool {
if len(a.includeExtensions) > 0 {
_, ok := a.includeExtensions[a.getFileExtension(path)]
return ok
}

for _, pathToExclude := range a.excludePaths {
if strings.HasPrefix(path, pathToExclude) {
return false
Expand Down
56 changes: 49 additions & 7 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ func TestNewAnalyzer(t *testing.T) {
path: "test/",
excludePaths: []string{"test"},
excludeExtensions: map[string]bool{".go": true},
includeExtensions: map[string]bool{},
}
analyser := NewAnalyzer("test/", []string{"test"}, map[string]bool{".go": true}, extensions)
analyser := NewAnalyzer(
"test/",
[]string{"test"},
map[string]bool{".go": true},
map[string]bool{},
extensions,
)
require.NotNil(t, analyser)
require.Equal(t, expected, analyser)
}
Expand All @@ -31,8 +38,14 @@ func TestMatchingFiles(t *testing.T) {
want []FileMetadata
}{
{
name: "Should return matching files without errors",
analyzer: NewAnalyzer(codeSamplesDir, []string{}, map[string]bool{}, extensions),
name: "Should return matching files without errors",
analyzer: NewAnalyzer(
codeSamplesDir,
[]string{},
map[string]bool{},
map[string]bool{},
extensions,
),
want: []FileMetadata{
{
FilePath: filepath.Join(codeSamplesDir, "index.html"),
Expand All @@ -47,8 +60,14 @@ func TestMatchingFiles(t *testing.T) {
},
},
{
name: "Should exclude files or dirs without errors",
analyzer: NewAnalyzer(codeSamplesDir, []string{filepath.Join(codeSamplesDir, "main.go")}, map[string]bool{}, extensions),
name: "Should exclude files or dirs without errors",
analyzer: NewAnalyzer(
codeSamplesDir,
[]string{filepath.Join(codeSamplesDir, "main.go")},
map[string]bool{},
map[string]bool{},
extensions,
),
want: []FileMetadata{
{
FilePath: filepath.Join(codeSamplesDir, "index.html"),
Expand All @@ -58,8 +77,14 @@ func TestMatchingFiles(t *testing.T) {
},
},
{
name: "Should exclude extensions without errors",
analyzer: NewAnalyzer(codeSamplesDir, []string{}, map[string]bool{".go": true}, extensions),
name: "Should exclude extensions without errors",
analyzer: NewAnalyzer(
codeSamplesDir,
[]string{},
map[string]bool{".go": true},
map[string]bool{},
extensions,
),
want: []FileMetadata{
{
FilePath: filepath.Join(codeSamplesDir, "index.html"),
Expand All @@ -68,6 +93,23 @@ func TestMatchingFiles(t *testing.T) {
},
},
},
{
name: "Should include extensions without errors",
analyzer: NewAnalyzer(
codeSamplesDir,
[]string{},
map[string]bool{},
map[string]bool{".go": true},
extensions,
),
want: []FileMetadata{
{
FilePath: filepath.Join(codeSamplesDir, "main.go"),
Extension: ".go",
Language: "Golang",
},
},
},
}

for _, tt := range tests {
Expand Down
23 changes: 7 additions & 16 deletions pkg/gcloc/gcloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/JoaoDanielRufino/gcloc/pkg/reporter/prompt"
"github.com/JoaoDanielRufino/gcloc/pkg/scanner"
"github.com/JoaoDanielRufino/gcloc/pkg/sorter"
"github.com/JoaoDanielRufino/gcloc/pkg/utils"
)

type Params struct {
Path string
ByFile bool
ExcludePaths []string
ExcludeExtensions []string
ByFile bool
IncludeExtensions []string
OrderByLang bool
OrderByFile bool
OrderByCode bool
Expand All @@ -35,16 +37,15 @@ type GCloc struct {
func NewGCloc(params Params, languages language.Languages) (*GCloc, error) {
excludePaths, err := filesystem.GetExcludePaths(params.Path, params.ExcludePaths)
if err != nil {
return &GCloc{}, err
return nil, err
}

extensions := getExtensionsMap(languages)

analyzer := analyzer.NewAnalyzer(
params.Path,
excludePaths,
getExcludeExtensionsMap(params.ExcludeExtensions),
extensions,
utils.ConvertToMap(params.ExcludeExtensions),
utils.ConvertToMap(params.IncludeExtensions),
getExtensionsMap(languages),
)

scanner := scanner.NewScanner(languages)
Expand Down Expand Up @@ -134,16 +135,6 @@ func getExtensionsMap(languages language.Languages) map[string]string {
return extensions
}

func getExcludeExtensionsMap(excludeExtensionsParam []string) map[string]bool {
excludeExtensions := map[string]bool{}

for _, ex := range excludeExtensionsParam {
excludeExtensions[ex] = true
}

return excludeExtensions
}

func getSorter(byFile bool, order string) sorter.Sorter {
if byFile {
return sorter.NewFileSorter(order)
Expand Down
3 changes: 3 additions & 0 deletions pkg/gcloc/gcloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestNewGCloc(t *testing.T) {
filepath.Join("..", "..", "test", "fixtures", "code_samples"),
nil,
map[string]bool{},
map[string]bool{},
getExtensionsMap(constants.Languages),
),
scanner: scanner.NewScanner(constants.Languages),
Expand Down Expand Up @@ -63,6 +64,7 @@ func TestNewGCloc(t *testing.T) {
filepath.Join("..", "..", "test", "fixtures", "code_samples"),
nil,
map[string]bool{".go": true},
map[string]bool{},
getExtensionsMap(constants.Languages),
),
scanner: scanner.NewScanner(constants.Languages),
Expand All @@ -76,6 +78,7 @@ func TestNewGCloc(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := NewGCloc(tt.params, tt.languages)
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, tt.want, got)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewScanner(t *testing.T) {

func TestScan(t *testing.T) {
codeSamplesDir := filepath.Join("..", "..", "test", "fixtures", "code_samples")
fileAnalyzer := analyzer.NewAnalyzer(codeSamplesDir, []string{}, map[string]bool{}, extensions)
fileAnalyzer := analyzer.NewAnalyzer(codeSamplesDir, []string{}, map[string]bool{}, map[string]bool{}, extensions)
scanner := NewScanner(languages)

files, _ := fileAnalyzer.MatchingFiles()
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/convert_to_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

func ConvertToMap(arr []string) map[string]bool {
mp := map[string]bool{}

for _, x := range arr {
mp[x] = true
}

return mp
}
41 changes: 41 additions & 0 deletions pkg/utils/convert_to_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package utils

import (
"testing"

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

func TestConvertToMap(t *testing.T) {
tests := []struct {
name string
arr []string
want map[string]bool
}{
{
name: "Should convert array to map",
arr: []string{".go", ".c", ".cpp"},
want: map[string]bool{
".go": true,
".c": true,
".cpp": true,
},
},
{
name: "Should convert array(with repeated values) to map",
arr: []string{".go", ".c", ".c", ".cpp"},
want: map[string]bool{
".go": true,
".c": true,
".cpp": true,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ConvertToMap(tt.arr)
require.Equal(t, tt.want, got)
})
}
}