-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving parameter validation logic and improving project code check…
… tests (#11)
- Loading branch information
Showing
6 changed files
with
226 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package validate | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParameterFunctionConsistency(t *testing.T) { | ||
err := checkParameterFunctionConsistency("parameter_types.go") | ||
assert.NoError(t, err, "Parameter function consistency check failed") | ||
} | ||
|
||
func checkParameterFunctionConsistency(filePath string) error { | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
caseRegex := regexp.MustCompile(`^[\t\s]*case\s+(\w+):`) | ||
scanner := bufio.NewScanner(file) | ||
|
||
lineNumber := 0 | ||
for scanner.Scan() { | ||
lineNumber++ | ||
line := scanner.Text() | ||
|
||
matches := caseRegex.FindStringSubmatch(line) | ||
if len(matches) == 2 { | ||
caseLabel := matches[1] | ||
|
||
// Move the scanner four lines down | ||
for i := 0; i < 4; i++ { | ||
if !scanner.Scan() { | ||
return fmt.Errorf("expected error message four lines down from case '%s' at line %d, but reached EOF", caseLabel, lineNumber) | ||
} | ||
lineNumber++ | ||
} | ||
|
||
errorLine := scanner.Text() | ||
if !strings.HasSuffix(errorLine, fmt.Sprintf(`"%s")`, caseLabel)) { | ||
|
||
finalStringLiteralRegex := regexp.MustCompile(`"([^"]+)"\s*\)\s*$`) | ||
stringLiteral := finalStringLiteralRegex.FindStringSubmatch(errorLine) | ||
|
||
return fmt.Errorf("mismatch found at line %d: case '%s' should be used in the error message. Have '%s', want '%s'", lineNumber, caseLabel, stringLiteral[1], caseLabel) | ||
} | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return fmt.Errorf("error scanning file: %v", err) | ||
} | ||
|
||
return 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,27 @@ | ||
package project_validation | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const projectValidationError = `this test must be run with the working directory set to the "golang" folder of the SDK project` | ||
|
||
func validateWorkingDirectory() error { | ||
currentDir, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("failed to get current directory: %v", err) | ||
} | ||
|
||
dirName := filepath.Base(currentDir) | ||
if dirName != "project-validation" { | ||
return fmt.Errorf("%v. Current directory: %v", projectValidationError, currentDir) | ||
} | ||
return nil | ||
} | ||
|
||
func vanityPath(path string) string { | ||
return strings.Replace(path, "..", "1inch-sdk/golang", 1) | ||
} |
65 changes: 65 additions & 0 deletions
65
golang/project-validation/integration_check_cleanup_test.go
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 project_validation | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const targetBlock = "helpers.Sleep()" | ||
|
||
func TestIntegrationSleepCleanup(t *testing.T) { | ||
|
||
err := validateWorkingDirectory() | ||
if err != nil { | ||
t.Fatalf("Directory error: %v", err) | ||
} | ||
|
||
err = filepath.Walk("./..", func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if strings.HasSuffix(path, "_integration_test.go") || strings.HasSuffix(path, "_e2e_test.go") { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
inTRunBlock := false | ||
openBracesCount := 0 | ||
tRunContent := "" | ||
|
||
lines := strings.Split(string(content), "\n") | ||
for _, line := range lines { | ||
line = strings.TrimSpace(line) | ||
|
||
if strings.HasPrefix(line, "t.Run(") { | ||
inTRunBlock = true | ||
} | ||
|
||
if inTRunBlock { | ||
tRunContent += line + "\n" | ||
openBracesCount += strings.Count(line, "{") - strings.Count(line, "}") | ||
|
||
if openBracesCount == 0 { | ||
if !strings.Contains(tRunContent, targetBlock) { | ||
t.Errorf(`File: %s - Missing cleanup block in one of the tests. Each test must have the following code in it: | ||
t.Cleanup(func() { | ||
helpers.Sleep() | ||
}) | ||
`, vanityPath(path)) | ||
} | ||
inTRunBlock = false | ||
tRunContent = "" | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Fatalf("Error walking through files: %v", err) | ||
} | ||
} |
Oops, something went wrong.