-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: maximize test coverage, improve -h output
* refactor: flags * refactor: process.File * refactor: remove process.getLicense() * refactor: files, fileHandler, tests * refactor: consolidate everything into process pkg * test: process.File + fix channel mgmt * chore: make channel buffered for performance
- Loading branch information
Showing
17 changed files
with
837 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* MIT License | ||
Copyright (c) 2020 Lluis Sanchez | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/lsm-dev/license-header-checker/internal/process" | ||
) | ||
|
||
// Options are the process.Options parsed from command line flags/args | ||
type Options struct { | ||
ShowVersion bool | ||
Verbose bool | ||
Process *process.Options | ||
} | ||
|
||
// parseOptions returns the parsed Options from command line flags/args | ||
func parseOptions(osArgs []string) (*Options, error) { | ||
|
||
flagSet := flag.NewFlagSet("lhc", flag.ExitOnError) | ||
flagSet.Usage = func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "\033[1;4mSYNOPSIS\033[0m\n\n") | ||
fmt.Fprintf(flag.CommandLine.Output(), "license-header-checker [-a] [-r] [-v] [-i path1,...] license-header-path src-path extensions...\n\n") | ||
fmt.Fprintf(flag.CommandLine.Output(), "\033[1;4mOPTIONS\033[0m\n\n") | ||
flagSet.PrintDefaults() | ||
fmt.Fprintf(flag.CommandLine.Output(), "\033[1;4mEXAMPLE\033[0m\n\n") | ||
fmt.Fprintf(flag.CommandLine.Output(), "license-header-checker -a -r -v -i folder,ignore/path license-header-path project-src-path extension1 extension2\n\n") | ||
} | ||
|
||
addFlag := flagSet.Bool("a", false, "Add the target license in case the file does not have any.") | ||
replaceFlag := flagSet.Bool("r", false, "Replace the existing license by the target one in case they are different.") | ||
ignorePathsFlag := flagSet.String("i", "", "A comma separated list of the folders, files and/or paths that should be ignored. Does not support wildcards.") | ||
verboseFlag := flagSet.Bool("v", false, "Be verbose during execution printing options, files being processed, execution time, ...") | ||
showVersionFlag := flagSet.Bool("version", false, "Display version number") | ||
|
||
flagSet.Parse(osArgs[1:]) | ||
|
||
args := flagSet.Args() | ||
|
||
if *showVersionFlag { | ||
return &Options{ | ||
true, | ||
false, | ||
nil, | ||
}, nil | ||
} | ||
|
||
if len(args) < 3 { | ||
return nil, errors.New("Missing arguments, please see documentation") | ||
} | ||
|
||
licensePath := args[0] | ||
path := args[1] | ||
|
||
extensions := []string{} | ||
for _, e := range args[2:] { | ||
extensions = append(extensions, "."+e) | ||
} | ||
|
||
ignorePaths := []string{} | ||
for _, p := range strings.Split(*ignorePathsFlag, ",") { | ||
if len(p) > 0 { | ||
ignorePaths = append(ignorePaths, p) | ||
} | ||
} | ||
|
||
processOptions := &process.Options{ | ||
Add: *addFlag, | ||
Replace: *replaceFlag, | ||
Path: path, | ||
LicensePath: licensePath, | ||
Extensions: extensions, | ||
IgnorePaths: ignorePaths, | ||
} | ||
|
||
return &Options{ | ||
*showVersionFlag, | ||
*verboseFlag, | ||
processOptions, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* MIT License | ||
Copyright (c) 2020 Lluis Sanchez | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrorIfMissingArgs(t *testing.T) { | ||
args := []string{"license-header-checker"} | ||
_, err := parseOptions(args) | ||
assert.NotNil(t, err) | ||
|
||
args = []string{"license-header-checker", "license-path"} | ||
_, err = parseOptions(args) | ||
assert.NotNil(t, err) | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path"} | ||
_, err = parseOptions(args) | ||
assert.NotNil(t, err) | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "js"} | ||
_, err = parseOptions(args) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestVersion(t *testing.T) { | ||
args := []string{"license-header-checker", "-version", "license-path", "source-path", "js"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, options.ShowVersion) | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "js"} | ||
options, _ = parseOptions(args) | ||
assert.False(t, options.ShowVersion) | ||
} | ||
|
||
func TestAdd(t *testing.T) { | ||
args := []string{"license-header-checker", "-a", "license-path", "source-path", "js"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, options.Process.Add) | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "js"} | ||
options, _ = parseOptions(args) | ||
assert.False(t, options.Process.Add) | ||
} | ||
|
||
func TestReplace(t *testing.T) { | ||
args := []string{"license-header-checker", "-r", "license-path", "source-path", "js"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, options.Process.Replace) | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "js"} | ||
options, _ = parseOptions(args) | ||
assert.False(t, options.Process.Replace) | ||
} | ||
|
||
func TestVerbose(t *testing.T) { | ||
args := []string{"license-header-checker", "-v", "license-path", "source-path", "js"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, options.Verbose) | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "js"} | ||
options, _ = parseOptions(args) | ||
assert.False(t, options.Verbose) | ||
} | ||
|
||
func TestPath(t *testing.T) { | ||
args := []string{"license-header-checker", "license-path", "source-path", "js", "ts"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, options.Process.Path == "source-path") | ||
} | ||
|
||
func TestLicensePath(t *testing.T) { | ||
args := []string{"license-header-checker", "license-path", "source-path", "js", "ts"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, options.Process.LicensePath == "license-path") | ||
} | ||
|
||
func TestExtensions(t *testing.T) { | ||
args := []string{"license-header-checker", "license-path", "source-path", "js", "ts"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, len(options.Process.Extensions) == 2) | ||
assert.True(t, options.Process.Extensions[0] == ".js") | ||
assert.True(t, options.Process.Extensions[1] == ".ts") | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "cpp"} | ||
options, _ = parseOptions(args) | ||
assert.True(t, len(options.Process.Extensions) == 1) | ||
assert.True(t, options.Process.Extensions[0] == ".cpp") | ||
} | ||
|
||
func TestIgnorePaths(t *testing.T) { | ||
args := []string{"license-header-checker", "-i", "node_modules,client/assets", "license-path", "source-path", "js"} | ||
options, _ := parseOptions(args) | ||
assert.True(t, len(options.Process.IgnorePaths) == 2) | ||
assert.True(t, options.Process.IgnorePaths[0] == "node_modules") | ||
assert.True(t, options.Process.IgnorePaths[1] == "client/assets") | ||
|
||
args = []string{"license-header-checker", "license-path", "source-path", "js"} | ||
options, _ = parseOptions(args) | ||
assert.True(t, len(options.Process.IgnorePaths) == 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.