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 all 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
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
Loading