Skip to content

Commit 0a0e119

Browse files
committed
Add check for invalid sources
1 parent 14b5584 commit 0a0e119

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cmd/casper/context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/url"
66
"os"
7+
"strings"
78

89
"github.com/miracl/casper"
910
"github.com/miracl/casper/source"
@@ -46,6 +47,10 @@ func (c *context) withSources(sources []string) error {
4647
}
4748

4849
if u.Scheme == "" {
50+
if !strings.Contains(s, "=") {
51+
return fmt.Errorf("invalid source: %v", s)
52+
}
53+
4954
// default to config
5055
u = &url.URL{
5156
Scheme: configScheme,

cmd/casper/main_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ func TestExample(t *testing.T) {
5151
out string // expected output
5252
pwd string // change the directory relative to working dir if set
5353
copy bool // copy the example folder in temporary dir if set
54+
err string // string representation of expected error
5455
}{
5556
{cmd: "casper fetch", out: outputFile + "\n", pwd: "../../example"},
5657
{cmd: "casper diff -key somekey", out: "No changes for key somekey\n", pwd: "../../example"},
5758

5859
// without config file
5960
{cmd: "casper build -t ../../example/template.yaml -s placeholder1=val1 -s placeholder2=val2", out: outputFile},
6061

62+
// with bad source
63+
{cmd: "casper build -t ../../example/template.yaml -s placeholder1=val1 -s source.yaml", out: outputFile, err: "creating context failed: invalid source: source.yaml"},
64+
6165
// without sources
6266
{cmd: "casper build -t ../../example/output.yaml", out: outputFile},
6367
{cmd: "casper diff -t ../../example/output.yaml -storage file -file-path ../../example/output.yaml", out: noChanges},
@@ -112,6 +116,15 @@ func TestExample(t *testing.T) {
112116
}
113117
}()
114118

119+
if tc.err != "" {
120+
app := newApp()
121+
err := app.Run(strings.Split(tc.cmd, " "))
122+
if err.Error() != tc.err {
123+
t.Fatalf("\nunexpected error: %v\n\texpected: %v", err, tc.err)
124+
}
125+
return
126+
}
127+
115128
if tc.pwd != "" {
116129
os.Chdir(tc.pwd)
117130
}

0 commit comments

Comments
 (0)