Skip to content

Commit

Permalink
chore: add check if path to file is valid (#2)
Browse files Browse the repository at this point in the history
- added check to local actions
- added invalid tests
- removed code from preset
- removed test from http api
  • Loading branch information
leszek-vechain authored Jun 13, 2024
1 parent d49d7ab commit c0bfbb8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 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,11 @@ 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
if !fileExists(n.ExecArtifact) {
return "", fmt.Errorf("file does not exist at path: %s", n.ExecArtifact)
}
}

return l.id, nil
Expand Down
14 changes: 14 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,19 @@ var networkJSON = fmt.Sprintf(`{
]
}`, genesis, genesis, genesis)

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

localEnv := NewLocalEnv()
_, err = localEnv.LoadConfig(networkCfg)
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
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

0 comments on commit c0bfbb8

Please sign in to comment.