Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add check if path to file is valid #2

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.19.x
go-version: '1.21'

- name: Make Test
id: unit-test
Expand Down
14 changes: 14 additions & 0 deletions environments/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ type Local struct {
id string
}

func fileExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return err == nil
}

func NewLocalEnv() environments.Actions {
return &Local{
localNodes: map[string]*Node{},
Expand All @@ -35,6 +43,12 @@ func (l *Local) LoadConfig(cfg *network.Network) (string, error) {
if n.DataDir == "" {
n.DataDir = filepath.Join(baseTmpDir, n.ID, "data")
}

// check if the exec artifact path exists
execArtifactPath := n.ExecArtifact
if !fileExists(execArtifactPath) {
return "", fmt.Errorf("file does not exist at path: %s", execArtifactPath)
}
leszek-vechain marked this conversation as resolved.
Show resolved Hide resolved
}

return l.id, nil
Expand Down
15 changes: 15 additions & 0 deletions environments/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/vechain/networkhub/preset"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -121,6 +122,20 @@ var networkJSON = fmt.Sprintf(`{
]
}`, genesis, genesis, genesis)

func TestLocalInvalidExecArtifact(t *testing.T) {
networkCfg, err := network.NewNetwork(
network.WithJSON(networkJSON),
)
require.NoError(t, err)

fmt.Println(networkJSON)
localEnv := NewLocalEnv()
_, err = localEnv.LoadConfig(networkCfg)
leszek-vechain marked this conversation as resolved.
Show resolved Hide resolved
require.Error(t, err)

require.True(t, strings.HasPrefix(err.Error(), "file does not exist at path"))
}

func TestLocal(t *testing.T) {
//t.Skip()
networkCfg, err := network.NewNetwork(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.19
go 1.21
1 change: 1 addition & 0 deletions preset/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (p *Networks) Load(id string, configPayload *APIConfigPayload) (*network.Ne
if configPayload == nil || configPayload.ArtifactPath == "" {
return nil, fmt.Errorf("preset config must be set")
}

// override the default path
for _, node := range preset.Nodes {
node.ExecArtifact = configPayload.ArtifactPath
Expand Down