Skip to content

Commit

Permalink
Automated update of test vectors (0xPolygonHermez#456)
Browse files Browse the repository at this point in the history
* Automate test-vector update

* run update action on PR to test

* remove gh action testing

* Fix apply tx in e2e tests

* refactor getTargetPath and log entries

* fix typo

* run tests on pushes to update-external-dependencies branch
  • Loading branch information
fgimenez authored Mar 7, 2022
1 parent 4adc524 commit ec2efdd
Show file tree
Hide file tree
Showing 34 changed files with 1,677 additions and 823 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- main
- master
- develop
- update-external-dependencies
pull_request:
jobs:
lint:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- main
- master
- develop
- update-external-dependencies
pull_request:
jobs:
test:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/updatedeps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ssh-key: ${{ secrets.UPDATE_DEPS_SSH_PK }}
- name: Install Go
uses: actions/setup-go@v1
with:
Expand All @@ -20,6 +22,7 @@ jobs:
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}
UPDATE_DEPS_SSH_PK: ${{ secrets.UPDATE_DEPS_SSH_PK }}
run: make update-external-dependencies
- name: Create Pull Request
uses: peter-evans/create-pull-request@v3
Expand Down
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
/dist/
/test/e2e/keystore
/test/e2e/keystore

/test/vectors/src/**/*md
/test/vectors/src/**/*js
/test/vectors/src/**/*sol
/test/vectors/src/**/*sh
/test/vectors/src/package.json
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM golang:1.17 AS build
ENV CGO_ENABLED=1

# INSTALL DEPENDENCIES
RUN go get -u github.com/gobuffalo/packr/v2/packr2
RUN go install github.com/gobuffalo/packr/v2/packr2@v2.8.3
COPY go.mod go.sum /src/
RUN cd /src && go mod download

Expand Down
69 changes: 69 additions & 0 deletions cmd/dependencies/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dependencies

import (
"io"
"os"
"path"
"runtime"
"strings"

"github.com/hermeznetwork/hermez-core/log"
"github.com/spf13/afero"
)

func updateFiles(fs afero.Fs, sourceDir, targetDir string) error {
const bufferSize = 20
err := afero.Walk(fs, targetDir, func(wpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info == nil || info.IsDir() {
return nil
}
relativePath := strings.Replace(wpath, targetDir, "", -1)
sourcePath := path.Join(sourceDir, relativePath)

sourceFile, err := fs.Open(sourcePath)
if err != nil {
return err
}
defer func() {
if err := sourceFile.Close(); err != nil {
log.Errorf("Could not close %s: %v", sourceFile.Name(), err)
}
}()
destinationFile, err := fs.OpenFile(wpath, os.O_RDWR, 0644)
if err != nil {
return err
}
defer func() {
if err := destinationFile.Close(); err != nil {
log.Errorf("Could not close %s: %v", destinationFile.Name(), err)
}
}()
buf := make([]byte, bufferSize)
for {
n, err := sourceFile.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := destinationFile.Write(buf[:n]); err != nil {
return err
}
}
return nil
})
return err
}

func getTargetPath(targetPath string) string {
if strings.HasPrefix(targetPath, "/") {
return targetPath
}
_, filename, _, _ := runtime.Caller(1)

return path.Join(path.Dir(filename), targetPath)
}
118 changes: 118 additions & 0 deletions cmd/dependencies/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package dependencies

import (
"testing"

"github.com/hermeznetwork/hermez-core/log"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)

func Test_updateFiles(t *testing.T) {
const (
defaultTargetDir = "/a/b/src"
defaultSourceDir = "/tmp/src"
)

var appFs = afero.NewMemMapFs()

tcs := []struct {
description string
initialSourceFiles map[string]string
initialTargetFiles map[string]string
expectedTargetFiles map[string]string
}{
{
description: "single file matching file",
initialSourceFiles: map[string]string{
"/tmp/src/a": "new-a-content",
},
initialTargetFiles: map[string]string{
"/a/b/src/a": "old-a-content",
},
expectedTargetFiles: map[string]string{
"/a/b/src/a": "new-a-content",
},
},
{
description: "single file matching file with non-matching files",
initialSourceFiles: map[string]string{
"/tmp/src/a": "new-a-content",
"/tmp/src/b": "new-b-content",
},
initialTargetFiles: map[string]string{
"/a/b/src/a": "old-a-content",
},
expectedTargetFiles: map[string]string{
"/a/b/src/a": "new-a-content",
},
},
{
description: "multiple matching files",
initialSourceFiles: map[string]string{
"/tmp/src/a.json": "new-a-content",
"/tmp/src/subdir1/subdir2/b.json": "new-b-content",
},
initialTargetFiles: map[string]string{
"/a/b/src/a.json": "old-a-content",
"/a/b/src/subdir1/subdir2/b.json": "old-b-content",
},
expectedTargetFiles: map[string]string{
"/a/b/src/a.json": "new-a-content",
"/a/b/src/subdir1/subdir2/b.json": "new-b-content",
},
},
{
description: "multiple matching files with non matching files",
initialSourceFiles: map[string]string{
"/tmp/src/subdira1/a.json": "new-a-content",
"/tmp/src/subdirb1/subdirb2/b.json": "new-b-content",
"/tmp/src/c.json": "new-c-content",
},
initialTargetFiles: map[string]string{
"/a/b/src/subdira1/a.json": "old-a-content",
"/a/b/src/subdir1/subdir2/b.json": "old-b-content",
},
expectedTargetFiles: map[string]string{
"/a/b/src/subdira1/a.json": "new-a-content",
"/a/b/src/subdir1/subdir2/b.json": "new-b-content",
},
},
}

for _, tc := range tcs {
tc := tc
t.Run(tc.description, func(t *testing.T) {
require.NoError(t, createTestFiles(appFs, tc.initialSourceFiles))
require.NoError(t, createTestFiles(appFs, tc.initialTargetFiles))

require.NoError(t, updateFiles(appFs, defaultSourceDir, defaultTargetDir))
a := afero.Afero{Fs: appFs}
for path, expectedContent := range tc.expectedTargetFiles {
actualContent, err := a.ReadFile(path)
require.NoError(t, err)
require.Equal(t, expectedContent, string(actualContent))
}
})
}
}

func createTestFiles(appFs afero.Fs, files map[string]string) error {
for path, content := range files {
f, err := appFs.Create(path)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
log.Errorf("Could not close %s: %v", f.Name(), err)
}
}()
_, err = f.WriteString(content)

if err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit ec2efdd

Please sign in to comment.