-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add check for misspelled executable name command
- Loading branch information
Showing
4 changed files
with
91 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/agext/levenshtein" | ||
) | ||
|
||
// IsSimilarWord calculates "The Levenshtein Distance" between two strings which | ||
// represents the minimum total cost of edits that would convert the first string | ||
// into the second. If the distance is less than 3, the word is considered misspelled. | ||
func IsSimilarWord(given string, suggestion string) bool { | ||
dist := levenshtein.Distance(given, suggestion, nil) | ||
if dist > 0 && dist < 3 { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,65 @@ | ||
package utils_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/runatlantis/atlantis/server/utils" | ||
. "github.com/runatlantis/atlantis/testing" | ||
) | ||
|
||
func Test_Spellcheck(t *testing.T) { | ||
t.Log("check if given executable name is misspelled or just an unrelated word") | ||
|
||
spellings := []struct { | ||
Misspelled bool | ||
Given string | ||
Want string | ||
}{ | ||
{ | ||
false, | ||
"atlantis", | ||
"atlantis", | ||
}, | ||
{ | ||
false, | ||
"maybe", | ||
"atlantis", | ||
}, | ||
{ | ||
false, | ||
"atlantis-qa", | ||
"atlantis-prod", | ||
}, | ||
{ | ||
true, | ||
"altantis", | ||
"atlantis", | ||
}, | ||
{ | ||
true, | ||
"atlants", | ||
"atlantis", | ||
}, | ||
{ | ||
true, | ||
"teraform", | ||
"terraform", | ||
}, | ||
} | ||
|
||
for _, s := range spellings { | ||
t.Run(fmt.Sprintf("given %s want %s", s.Given, s.Want), func(t *testing.T) { | ||
isMisspelled := utils.IsSimilarWord(s.Given, s.Want) | ||
|
||
if s.Misspelled { | ||
Equals(t, isMisspelled, true) | ||
} | ||
|
||
if !s.Misspelled { | ||
Equals(t, isMisspelled, false) | ||
} | ||
}) | ||
} | ||
|
||
} |