Skip to content

Commit

Permalink
feat: Run separate cairo integration test files as a separate Go test (
Browse files Browse the repository at this point in the history
…#392)

* feat: Run separate cairo integration test files as a separate Go test

* feat: Allow multiple files to be provided in the .env variable and separated by comma

* feat: Support to read environment variable from console by first

* Refactor into an object Filter

* Fix golangci-lint error
  • Loading branch information
xiaolou86 committed May 13, 2024
1 parent 0a743d9 commit 17152cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ Integration tests are run with:
make integration
```

Integration tests are run with filters in the following two methods, with the first method having higher priority.

```bash
#1) set global environment variable `INTEGRATION_TESTS_FILTERS`
export INTEGRATION_TESTS_FILTERS=fib,alloc
make integration

#2) set by editing `INTEGRATION_TESTS_FILTERS=` in the `./integration_tests/.env` file
make integration
```

If you want to execute all tests of the project:

```bash
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o=
github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set to run some specific file tests (ex. fib.cairo,alloc.cairo)
INTEGRATION_TESTS_FILTERS=
40 changes: 38 additions & 2 deletions integration_tests/cairozero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,52 @@ import (

"github.com/NethermindEth/cairo-vm-go/pkg/vm"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type Filter struct {
filters []string
}

func (f *Filter) init() {
filtersRaw := os.Getenv("INTEGRATION_TESTS_FILTERS")
if filtersRaw == "" {
_ = godotenv.Load("./.env")
filtersRaw = os.Getenv("INTEGRATION_TESTS_FILTERS")
}
filters := strings.Split(filtersRaw, ",")
for _, filter := range filters {
trimmed := strings.TrimSpace(filter)
if trimmed != "" {
f.filters = append(f.filters, trimmed)
}
}
}

func (f *Filter) filtered(testFile string) bool {
if len(f.filters) == 0 {
return true
}

for _, filter := range f.filters {
if strings.Contains(testFile, filter) {
return true
}
}

return false
}

func TestCairoZeroFiles(t *testing.T) {
root := "./cairo_files/"
testFiles, err := os.ReadDir(root)
require.NoError(t, err)

// filter is for debugging purposes
filter := ""
filter := Filter{}
filter.init()

for _, dirEntry := range testFiles {
if dirEntry.IsDir() || isGeneratedFile(dirEntry.Name()) {
Expand All @@ -29,9 +64,10 @@ func TestCairoZeroFiles(t *testing.T) {

path := filepath.Join(root, dirEntry.Name())

if !strings.Contains(path, filter) {
if !filter.filtered(dirEntry.Name()) {
continue
}

t.Logf("testing: %s\n", path)

compiledOutput, err := compileZeroCode(path)
Expand Down

0 comments on commit 17152cb

Please sign in to comment.