Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
yajsv - Yet Another Json Schema Validator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
SmartDataLab
public
CI-CD tools
yajsv - Yet Another Json Schema Validator
Commits
bbafa19b
Commit
bbafa19b
authored
5 years ago
by
Neil Pankey
Browse files
Options
Downloads
Patches
Plain Diff
test: Table-driven tests
parent
506f5ad5
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.go
+18
-23
18 additions, 23 deletions
main.go
main_test.go
+81
-107
81 additions, 107 deletions
main_test.go
with
99 additions
and
130 deletions
main.go
+
18
−
23
View file @
bbafa19b
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"bufio"
"bufio"
"flag"
"flag"
"fmt"
"fmt"
"io"
"io/ioutil"
"io/ioutil"
"log"
"log"
"os"
"os"
...
@@ -37,13 +38,13 @@ func init() {
...
@@ -37,13 +38,13 @@ func init() {
func
main
()
{
func
main
()
{
log
.
SetFlags
(
0
)
log
.
SetFlags
(
0
)
os
.
Exit
(
realMain
(
os
.
Args
[
1
:
]))
os
.
Exit
(
realMain
(
os
.
Args
[
1
:
]
,
os
.
Stdout
))
}
}
func
realMain
(
args
[]
string
)
int
{
func
realMain
(
args
[]
string
,
w
io
.
Writer
)
int
{
flag
.
CommandLine
.
Parse
(
args
)
flag
.
CommandLine
.
Parse
(
args
)
if
*
versionFlag
{
if
*
versionFlag
{
fmt
.
P
rintln
(
version
)
fmt
.
Fp
rintln
(
w
,
version
)
return
0
return
0
}
}
if
*
schemaFlag
==
""
{
if
*
schemaFlag
==
""
{
...
@@ -70,7 +71,6 @@ func realMain(args []string) int {
...
@@ -70,7 +71,6 @@ func realMain(args []string) int {
if
!
filepath
.
IsAbs
(
pattern
)
{
if
!
filepath
.
IsAbs
(
pattern
)
{
pattern
=
filepath
.
Join
(
dir
,
pattern
)
pattern
=
filepath
.
Join
(
dir
,
pattern
)
}
}
docs
=
append
(
docs
,
glob
(
pattern
)
...
)
docs
=
append
(
docs
,
glob
(
pattern
)
...
)
}
}
if
err
:=
scanner
.
Err
();
err
!=
nil
{
if
err
:=
scanner
.
Err
();
err
!=
nil
{
...
@@ -89,9 +89,9 @@ func realMain(args []string) int {
...
@@ -89,9 +89,9 @@ func realMain(args []string) int {
}
}
for
_
,
ref
:=
range
refFlags
{
for
_
,
ref
:=
range
refFlags
{
for
_
,
p
:=
range
glob
(
ref
)
{
for
_
,
p
:=
range
glob
(
ref
)
{
absPath
,
absPathE
rr
:=
filepath
.
Abs
(
p
)
absPath
,
e
rr
:=
filepath
.
Abs
(
p
)
if
absPathE
rr
!=
nil
{
if
e
rr
!=
nil
{
log
.
Fatalf
(
"%s: unable to convert to absolute path: %s
\n
"
,
absPath
,
absPathE
rr
)
log
.
Fatalf
(
"%s: unable to convert to absolute path: %s
\n
"
,
absPath
,
e
rr
)
}
}
if
absPath
==
schemaPath
{
if
absPath
==
schemaPath
{
...
@@ -125,7 +125,6 @@ func realMain(args []string) int {
...
@@ -125,7 +125,6 @@ func realMain(args []string) int {
failures
:=
make
([]
string
,
0
)
failures
:=
make
([]
string
,
0
)
errors
:=
make
([]
string
,
0
)
errors
:=
make
([]
string
,
0
)
for
_
,
p
:=
range
docs
{
for
_
,
p
:=
range
docs
{
//fmt.Println(p)
wg
.
Add
(
1
)
wg
.
Add
(
1
)
go
func
(
path
string
)
{
go
func
(
path
string
)
{
defer
wg
.
Done
()
defer
wg
.
Done
()
...
@@ -135,8 +134,8 @@ func realMain(args []string) int {
...
@@ -135,8 +134,8 @@ func realMain(args []string) int {
loader
,
err
:=
jsonLoader
(
path
)
loader
,
err
:=
jsonLoader
(
path
)
if
err
!=
nil
{
if
err
!=
nil
{
msg
:=
fmt
.
Sprintf
(
"%s: error: load doc %s
\n
"
,
path
,
err
)
msg
:=
fmt
.
Sprintf
(
"%s: error: load doc
:
%s"
,
path
,
err
)
fmt
.
P
rintln
(
msg
)
fmt
.
Fp
rintln
(
w
,
msg
)
errors
=
append
(
errors
,
msg
)
errors
=
append
(
errors
,
msg
)
return
return
}
}
...
@@ -144,7 +143,7 @@ func realMain(args []string) int {
...
@@ -144,7 +143,7 @@ func realMain(args []string) int {
switch
{
switch
{
case
err
!=
nil
:
case
err
!=
nil
:
msg
:=
fmt
.
Sprintf
(
"%s: error: validate: %s"
,
path
,
err
)
msg
:=
fmt
.
Sprintf
(
"%s: error: validate: %s"
,
path
,
err
)
fmt
.
P
rintln
(
msg
)
fmt
.
Fp
rintln
(
w
,
msg
)
errors
=
append
(
errors
,
msg
)
errors
=
append
(
errors
,
msg
)
case
!
result
.
Valid
()
:
case
!
result
.
Valid
()
:
...
@@ -153,11 +152,11 @@ func realMain(args []string) int {
...
@@ -153,11 +152,11 @@ func realMain(args []string) int {
lines
[
i
]
=
fmt
.
Sprintf
(
"%s: fail: %s"
,
path
,
desc
)
lines
[
i
]
=
fmt
.
Sprintf
(
"%s: fail: %s"
,
path
,
desc
)
}
}
msg
:=
strings
.
Join
(
lines
,
"
\n
"
)
msg
:=
strings
.
Join
(
lines
,
"
\n
"
)
fmt
.
P
rintln
(
msg
)
fmt
.
Fp
rintln
(
w
,
msg
)
failures
=
append
(
failures
,
msg
)
failures
=
append
(
failures
,
msg
)
case
!*
quietFlag
:
case
!*
quietFlag
:
fmt
.
P
rintf
(
"%s: pass
\n
"
,
path
)
fmt
.
Fp
rintf
(
w
,
"%s: pass
\n
"
,
path
)
}
}
}(
p
)
}(
p
)
}
}
...
@@ -166,12 +165,12 @@ func realMain(args []string) int {
...
@@ -166,12 +165,12 @@ func realMain(args []string) int {
// Summarize results (e.g. errors)
// Summarize results (e.g. errors)
if
!*
quietFlag
{
if
!*
quietFlag
{
if
len
(
failures
)
>
0
{
if
len
(
failures
)
>
0
{
fmt
.
P
rintf
(
"%d of %d failed validation
\n
"
,
len
(
failures
),
len
(
docs
))
fmt
.
Fp
rintf
(
w
,
"%d of %d failed validation
\n
"
,
len
(
failures
),
len
(
docs
))
fmt
.
P
rintln
(
strings
.
Join
(
failures
,
"
\n
"
))
fmt
.
Fp
rintln
(
w
,
strings
.
Join
(
failures
,
"
\n
"
))
}
}
if
len
(
errors
)
>
0
{
if
len
(
errors
)
>
0
{
fmt
.
P
rintf
(
"%d of %d malformed documents
\n
"
,
len
(
errors
),
len
(
docs
))
fmt
.
Fp
rintf
(
w
,
"%d of %d malformed documents
\n
"
,
len
(
errors
),
len
(
docs
))
fmt
.
P
rintln
(
strings
.
Join
(
errors
,
"
\n
"
))
fmt
.
Fp
rintln
(
w
,
strings
.
Join
(
errors
,
"
\n
"
))
}
}
}
}
exit
:=
0
exit
:=
0
...
@@ -235,18 +234,14 @@ func glob(pattern string) []string {
...
@@ -235,18 +234,14 @@ func glob(pattern string) []string {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
universalPaths
:=
make
([]
string
,
0
)
paths
,
err
:=
filepath
.
Glob
(
pattern
)
paths
,
err
:=
filepath
.
Glob
(
pattern
)
for
_
,
mypath
:=
range
paths
{
universalPaths
=
append
(
universalPaths
,
filepath
.
ToSlash
(
mypath
))
}
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
if
len
(
universalP
aths
)
==
0
{
if
len
(
p
aths
)
==
0
{
log
.
Fatalf
(
"%s: no such file or directory"
,
pattern
)
log
.
Fatalf
(
"%s: no such file or directory"
,
pattern
)
}
}
return
universalP
aths
return
p
aths
}
}
type
stringFlags
[]
string
type
stringFlags
[]
string
...
...
This diff is collapsed.
Click to expand it.
main_test.go
+
81
−
107
View file @
bbafa19b
// +build windows !windows
package
main
package
main
import
(
import
(
"log"
"path/filepath"
"sort"
"strings"
"testing"
)
)
func
ExampleMain_pass_ymlschema_ymldoc
()
{
func
TestMain
(
t
*
testing
.
T
)
{
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.yml"
,
"testdata/data-pass.yml"
})
tests
:=
[]
struct
{
if
exit
!=
0
{
in
string
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
out
[]
string
}
exit
int
// Output:
}
{
// testdata/data-pass.yml: pass
{
}
"-s testdata/schema.yml testdata/data-pass.yml"
,
[]
string
{
"testdata/data-pass.yml: pass"
},
func
ExampleMain_pass_jsonschema_ymldoc
()
{
0
,
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.json"
,
"testdata/data-pass.yml"
})
},
{
if
exit
!=
0
{
"-s testdata/schema.json testdata/data-pass.yml"
,
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
[]
string
{
"testdata/data-pass.yml: pass"
},
}
0
,
// Output:
},
{
// testdata/data-pass.yml: pass
"-s testdata/schema.json testdata/data-pass.json"
,
}
[]
string
{
"testdata/data-pass.json: pass"
},
0
,
func
ExampleMain_pass_jsonschema_jsondoc
()
{
},
{
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.json"
,
"testdata/data-pass.json"
})
"-s testdata/schema.yml testdata/data-pass.json"
,
if
exit
!=
0
{
[]
string
{
"testdata/data-pass.json: pass"
},
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
0
,
}
},
{
// Output:
"-q -s testdata/schema.yml testdata/data-fail.yml"
,
// testdata/data-pass.json: pass
[]
string
{
"testdata/data-fail.yml: fail: (root): foo is required"
},
}
1
,
},
{
func
ExampleMain_pass_ymlschema_jsondoc
()
{
"-q -s testdata/schema.json testdata/data-fail.yml"
,
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.yml"
,
"testdata/data-pass.json"
})
[]
string
{
"testdata/data-fail.yml: fail: (root): foo is required"
},
if
exit
!=
0
{
1
,
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
},
{
}
"-q -s testdata/schema.json testdata/data-fail.json"
,
// Output:
[]
string
{
"testdata/data-fail.json: fail: (root): foo is required"
},
// testdata/data-pass.json: pass
1
,
}
},
{
"-q -s testdata/schema.yml testdata/data-fail.json"
,
func
ExampleMain_fail_ymlschema_ymldoc
()
{
[]
string
{
"testdata/data-fail.json: fail: (root): foo is required"
},
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-fail.yml"
})
1
,
if
exit
!=
1
{
},
{
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
"-q -s testdata/schema.json testdata/data-error.json"
,
}
[]
string
{
"testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')"
},
// Output:
2
,
// testdata/data-fail.yml: fail: (root): foo is required
},
{
}
"-q -s testdata/schema.yml testdata/data-error.yml"
,
[]
string
{
"testdata/data-error.yml: error: load doc: yaml: found unexpected end of stream"
},
func
ExampleMain_fail_jsonschema_ymldoc
()
{
2
,
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-fail.yml"
})
},
{
if
exit
!=
1
{
"-q -s testdata/schema.json testdata/data-*.json"
,
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
[]
string
{
}
"testdata/data-fail.json: fail: (root): foo is required"
,
// Output:
"testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')"
,
// testdata/data-fail.yml: fail: (root): foo is required
},
3
,
}
},
{
"-q -s testdata/schema.yml testdata/data-*.yml"
,
func
ExampleMain_fail_jsonschema_jsondoc
()
{
[]
string
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-fail.json"
})
"testdata/data-error.yml: error: load doc: yaml: found unexpected end of stream"
,
if
exit
!=
1
{
"testdata/data-fail.yml: fail: (root): foo is required"
,
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
},
3
,
}
},
// Output:
// testdata/data-fail.json: fail: (root): foo is required
}
func
ExampleMain_fail_ymlschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-fail.json"
})
if
exit
!=
1
{
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
}
// Output:
// testdata/data-fail.json: fail: (root): foo is required
}
}
func
ExampleMain_error_jsonschema_jsondoc
()
{
for
_
,
tt
:=
range
tests
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-error.json"
})
in
:=
strings
.
ReplaceAll
(
tt
.
in
,
"/"
,
string
(
filepath
.
Separator
))
if
exit
!=
2
{
sort
.
Strings
(
tt
.
out
)
log
.
Fatalf
(
"exit: got %d, want 2"
,
exit
)
out
:=
strings
.
Join
(
tt
.
out
,
"
\n
"
)
}
out
=
strings
.
ReplaceAll
(
out
,
"/"
,
string
(
filepath
.
Separator
))
// Output:
// testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')
}
func
ExampleMain_error_ymlschema_ymldoc
()
{
t
.
Run
(
in
,
func
(
t
*
testing
.
T
)
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-error.yml"
})
var
w
strings
.
Builder
if
exit
!=
2
{
exit
:=
realMain
(
strings
.
Split
(
in
,
" "
),
&
w
)
log
.
Fatalf
(
"exit: got %d, want 2"
,
exit
)
if
exit
!=
tt
.
exit
{
t
.
Fatalf
(
"exit: got %d, want %d"
,
exit
,
tt
.
exit
)
}
}
// Output:
lines
:=
strings
.
Split
(
w
.
String
(),
"
\n
"
)
// testdata/data-error.yml: error: load doc yaml: found unexpected end of stream
sort
.
Strings
(
lines
)
got
:=
strings
.
Join
(
lines
[
1
:
],
"
\n
"
)
if
got
!=
out
{
t
.
Errorf
(
"got
\n
%s
\n
want
\n
%s"
,
got
,
out
)
}
}
})
func
ExampleMain_glob_jsonschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-*.json"
})
if
exit
!=
3
{
log
.
Fatalf
(
"exit: got %d, want 3"
,
exit
)
}
}
// Unordered output:
// testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')
// testdata/data-fail.json: fail: (root): foo is required
}
}
func
ExampleMain_glob_ymlschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-*.yml"
})
if
exit
!=
3
{
log
.
Fatalf
(
"exit: got %d, want 3"
,
exit
)
}
// Unordered output:
// testdata/data-fail.yml: fail: (root): foo is required
//
// testdata/data-error.yml: error: load doc yaml: found unexpected end of stream
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment