-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAJOR: add aspell as additional check for spelling errors
- Loading branch information
Showing
17 changed files
with
763 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
mode: all | ||
min_length: 3 | ||
ignore: | ||
- '*_test.go' | ||
allowed: | ||
- aspell | ||
- repo | ||
- yaml | ||
- config | ||
- Github | ||
- Gitlab | ||
- env | ||
- failsafe | ||
- golang | ||
- mkdir | ||
- WORKDIR | ||
- apk | ||
- ENTRYPOINT | ||
- ubuntu | ||
- golangci | ||
- splitted | ||
- sudo | ||
- mri | ||
- IID | ||
- repo | ||
- OPTIM | ||
- Submatch | ||
- Lshortfile | ||
- MAXSUBJECTLEN | ||
- MAXSUBJECTPARTS | ||
- MINSUBJECTLEN | ||
- MINSUBJECTPARTS | ||
- malformatted | ||
- cmdline |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
FROM golang:alpine as builder | ||
FROM golang:alpine AS builder | ||
RUN mkdir /build | ||
ADD . /build/ | ||
WORKDIR /build | ||
RUN go build -o check | ||
|
||
FROM alpine:latest | ||
RUN apk --no-cache add aspell aspell-en | ||
COPY --from=builder /build/check /check | ||
WORKDIR / | ||
ENTRYPOINT ["/check"] |
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,163 @@ | ||
package aspell | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
"slices" | ||
"sort" | ||
"strings" | ||
|
||
"check-commit/match" | ||
|
||
"github.com/fatih/camelcase" | ||
) | ||
|
||
type Aspell struct { | ||
Mode mode `yaml:"mode"` | ||
MinLength int `yaml:"min_length"` | ||
Ignore []string `yaml:"ignore"` | ||
AllowedWords []string `yaml:"allowed"` | ||
HelpText string `yaml:"-"` | ||
} | ||
|
||
var ( | ||
camelCaseOK = map[string]struct{}{ | ||
"HAProxy": {}, | ||
"golang": {}, | ||
"ascii": {}, | ||
"api": {}, | ||
} | ||
camelCaseNotOK = map[string]struct{}{} | ||
) | ||
|
||
func (a Aspell) checkSingle(data string, allowedWords []string) error { | ||
var words []string | ||
var badWords []string | ||
|
||
checkRes, err := checkWithAspellExec(data) | ||
if checkRes != "" { | ||
words = strings.Split(checkRes, "\n") | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, word := range words { | ||
wordLower := strings.ToLower(word) | ||
if len(word) < a.MinLength { | ||
continue | ||
} | ||
if _, ok := camelCaseNotOK[wordLower]; ok { | ||
badWords = append(badWords, wordLower) | ||
continue | ||
} | ||
if _, ok := camelCaseOK[wordLower]; ok { | ||
continue | ||
} | ||
if slices.Contains(a.AllowedWords, wordLower) || slices.Contains(allowedWords, wordLower) { | ||
continue | ||
} | ||
splitted := camelcase.Split(word) | ||
if len(splitted) < 2 { | ||
splitted = strings.FieldsFunc(word, func(r rune) bool { | ||
return r == '_' || r == '-' | ||
}) | ||
} | ||
if len(splitted) > 1 { | ||
for _, s := range splitted { | ||
er := a.checkSingle(s, allowedWords) | ||
if er != nil { | ||
camelCaseNotOK[wordLower] = struct{}{} | ||
badWords = append(badWords, word+":"+s) | ||
break | ||
} | ||
} | ||
} else { | ||
camelCaseNotOK[wordLower] = struct{}{} | ||
badWords = append(badWords, word) | ||
} | ||
} | ||
|
||
if len(badWords) > 0 { | ||
m := map[string]struct{}{} | ||
for _, w := range badWords { | ||
m[w] = struct{}{} | ||
} | ||
badWords = []string{} | ||
for k := range m { | ||
badWords = append(badWords, k) | ||
} | ||
sort.Strings(badWords) | ||
return fmt.Errorf("aspell: %s", badWords) | ||
} | ||
return nil | ||
} | ||
|
||
func (a Aspell) Check(subjects []string, commitsFull []string, content []map[string]string) error { | ||
var response string | ||
var checks []string | ||
switch a.Mode { | ||
case modeDisabled: | ||
return nil | ||
case modeSubject: | ||
checks = subjects | ||
case modeCommit: | ||
checks = commitsFull | ||
case modeAll: | ||
for _, file := range content { | ||
for name, v := range file { | ||
nextFile := false | ||
for _, filter := range a.Ignore { | ||
if match.MatchFilter(name, filter) { | ||
// log.Println("File", name, "in ignore list") | ||
nextFile = true | ||
continue | ||
} | ||
} | ||
if nextFile { | ||
continue | ||
} | ||
var imports []string | ||
if strings.HasSuffix(name, ".go") { | ||
imports = match.GetImportWordsFromGoFile(name) | ||
} | ||
if err := a.checkSingle(v, imports); err != nil { | ||
log.Println(name, err.Error()) | ||
response += fmt.Sprintf("%s\n", err) | ||
} | ||
} | ||
} | ||
checks = []string{} | ||
default: | ||
checks = subjects | ||
} | ||
|
||
for _, subject := range checks { | ||
if err := a.checkSingle(subject, []string{}); err != nil { | ||
response += fmt.Sprintf("%s\n", err) | ||
} | ||
} | ||
|
||
if len(response) > 0 { | ||
return fmt.Errorf("%s", response) | ||
} | ||
return nil | ||
} | ||
|
||
func checkWithAspellExec(subject string) (string, error) { | ||
cmd := exec.Command("aspell", "--list") | ||
cmd.Stdin = strings.NewReader(subject) | ||
|
||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
if err != nil { | ||
log.Printf("aspell error: %s, stderr: %s", err, stderr.String()) | ||
return "", err | ||
} | ||
|
||
return stdout.String() + stderr.String(), 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,33 @@ | ||
package aspell | ||
|
||
import "testing" | ||
|
||
func Test_checkWithAspell(t *testing.T) { | ||
aspell := Aspell{ | ||
Mode: modeSubject, | ||
MinLength: 3, | ||
AllowedWords: []string{"config"}, | ||
} | ||
tests := []struct { | ||
name string | ||
subject string | ||
wantErr bool | ||
}{ | ||
{"OK 1", "BUG/MEDIUM: config: add default location of path to the configuration file", false}, | ||
{"OK 2", "BUG/MEDIUM: config: add default location of path to the configuration file xtra", false}, | ||
{"error - flie", "BUG/MEDIUM: config: add default location of path to the configuration flie", true}, | ||
{"error - locatoin", "CLEANUP/MEDIUM: config: add default locatoin of path to the configuration file", true}, | ||
{"error - locatoin+flie", "CLEANUP/MEDIUM: config: add default locatoin of path to the configuration flie", true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := aspell.checkSingle(tt.subject, []string{"xtra"}) | ||
if tt.wantErr && err == nil { | ||
t.Errorf("checkWithAspell() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
if !tt.wantErr && err != nil { | ||
t.Errorf("checkWithAspell() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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,10 @@ | ||
package aspell | ||
|
||
type mode string | ||
|
||
const ( | ||
modeDisabled mode = "disabled" | ||
modeSubject mode = "subject" | ||
modeCommit mode = "commit" | ||
modeAll mode = "all" | ||
) |
Oops, something went wrong.