1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Address comments

This commit is contained in:
koplas 2024-06-21 15:35:30 +02:00
parent 33bd6bd787
commit 3084cdbc37
No known key found for this signature in database
4 changed files with 47 additions and 68 deletions

View file

@ -17,15 +17,13 @@ func TestCSV(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
csvWriter := NewFullyQuotedCSWWriter(buf) csvWriter := NewFullyQuotedCSWWriter(buf)
for _, x := range [][]string{{"a", "b", "c"}, {"d", "e", "f"}} { for _, x := range [][]string{{"a", "b", "c"}, {"d", "e", "f"}} {
err := csvWriter.Write(x) if err := csvWriter.Write(x); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
} }
csvWriter.Flush() csvWriter.Flush()
err := csvWriter.Error() if err := csvWriter.Error(); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
for _, want := range []string{`"a","b","c"`, `"d","e","f"`} { for _, want := range []string{`"a","b","c"`, `"d","e","f"`} {

View file

@ -60,28 +60,25 @@ func TestConformingFileName(t *testing.T) {
func TestIDMatchesFilename(t *testing.T) { func TestIDMatchesFilename(t *testing.T) {
pathEval := NewPathEval() pathEval := NewPathEval()
doc := make(map[string]interface{}) doc := make(map[string]any)
doc["document"] = map[string]interface{}{ doc["document"] = map[string]any{
"tracking": map[string]interface{}{ "tracking": map[string]any{
"id": "valid.json", "id": "valid.json",
}, },
} }
err := IDMatchesFilename(pathEval, doc, "valid.json") if err := IDMatchesFilename(pathEval, doc, "valid.json"); err != nil {
if err != nil {
t.Errorf("IDMatchesFilename: Expected nil, got %q", err) t.Errorf("IDMatchesFilename: Expected nil, got %q", err)
} }
err = IDMatchesFilename(pathEval, doc, "different_file_name.json") if err := IDMatchesFilename(pathEval, doc, "different_file_name.json"); err == nil {
if err == nil {
t.Error("IDMatchesFilename: Expected error, got nil") t.Error("IDMatchesFilename: Expected error, got nil")
} }
doc["document"] = map[string]interface{}{ doc["document"] = map[string]any{
"tracking": map[string]interface{}{}, "tracking": map[string]any{},
} }
err = IDMatchesFilename(pathEval, doc, "valid.json") if err := IDMatchesFilename(pathEval, doc, "valid.json"); err == nil {
if err == nil {
t.Error("IDMatchesFilename: Expected error, got nil") t.Error("IDMatchesFilename: Expected error, got nil")
} }
} }
@ -130,8 +127,7 @@ func TestNWriter(t *testing.T) {
func TestWriteToFile(t *testing.T) { func TestWriteToFile(t *testing.T) {
filename := filepath.Join(t.TempDir(), "test_file") filename := filepath.Join(t.TempDir(), "test_file")
wt := bytes.NewBufferString("test_data") wt := bytes.NewBufferString("test_data")
err := WriteToFile(filename, wt) if err := WriteToFile(filename, wt); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
fileData, err := os.ReadFile(filename) fileData, err := os.ReadFile(filename)
@ -149,12 +145,10 @@ func TestMakeUniqFile(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
_, err = file.Write([]byte("test_data")) if _, err = file.Write([]byte("test_data")); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
err = file.Close() if err = file.Close(); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
} }
@ -185,16 +179,20 @@ func Test_mkUniq(t *testing.T) {
func TestDeepCopy(t *testing.T) { func TestDeepCopy(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
os.MkdirAll(filepath.Join(dir, "src/folder0"), 0755) if err := os.MkdirAll(filepath.Join(dir, "src/folder0"), 0755); err != nil {
os.MkdirAll(filepath.Join(dir, "dst"), 0755) t.Fatal(err)
os.MkdirAll(filepath.Join(dir, "dst1"), 0755) }
err := os.WriteFile(filepath.Join(dir, "src/folder0/test_file"), []byte("test_data"), 0755) if err := os.MkdirAll(filepath.Join(dir, "dst"), 0755); err != nil {
if err != nil { t.Fatal(err)
t.Error(err) }
if err := os.MkdirAll(filepath.Join(dir, "dst1"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "src/folder0/test_file"), []byte("test_data"), 0755); err != nil {
t.Fatal(err)
} }
err = DeepCopy(filepath.Join(dir, "dst"), filepath.Join(dir, "src")) if err := DeepCopy(filepath.Join(dir, "dst"), filepath.Join(dir, "src")); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
@ -207,13 +205,11 @@ func TestDeepCopy(t *testing.T) {
t.Errorf("DeepCopy: Expected test_data, got %v", fileData) t.Errorf("DeepCopy: Expected test_data, got %v", fileData)
} }
err = DeepCopy("/path/does/not/exist", filepath.Join(dir, "src")) if err = DeepCopy("/path/does/not/exist", filepath.Join(dir, "src")); err == nil {
if err == nil {
t.Error("DeepCopy: Expected error, got nil") t.Error("DeepCopy: Expected error, got nil")
} }
err = DeepCopy(filepath.Join(dir, "dst1"), "/path/does/not/exist") if err = DeepCopy(filepath.Join(dir, "dst1"), "/path/does/not/exist"); err == nil {
if err == nil {
t.Error("DeepCopy: Expected error, got nil") t.Error("DeepCopy: Expected error, got nil")
} }
} }

View file

@ -64,8 +64,7 @@ func TestWriteHashToFile(t *testing.T) {
nameArg := "name" nameArg := "name"
want := "deadbeef " + nameArg + "\n" want := "deadbeef " + nameArg + "\n"
err := WriteHashToFile(filePath, nameArg, hashArg, []byte{}) if err := WriteHashToFile(filePath, nameArg, hashArg, []byte{}); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
testFile, err := os.Open(filePath) testFile, err := os.Open(filePath)
@ -90,8 +89,7 @@ func TestWriteHashSumToFile(t *testing.T) {
nameArg := "name" nameArg := "name"
want := "deadbeef " + nameArg + "\n" want := "deadbeef " + nameArg + "\n"
err := WriteHashSumToFile(filePath, nameArg, sum) if err := WriteHashSumToFile(filePath, nameArg, sum); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
testFile, err := os.Open(filePath) testFile, err := os.Open(filePath)

View file

@ -31,7 +31,7 @@ func TestPathEval_Compile(t *testing.T) {
t.Error("PathEval_Compile: Expected cached eval") t.Error("PathEval_Compile: Expected cached eval")
} }
got, err := eval.EvalInt(context.Background(), map[string]interface{}{"foo": 5}) got, err := eval.EvalInt(context.Background(), map[string]any{"foo": 5})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -46,7 +46,7 @@ func TestPathEval_Eval(t *testing.T) {
if err == nil { if err == nil {
t.Error("PathEval_Eval: Expected error, got nil") t.Error("PathEval_Eval: Expected error, got nil")
} }
got, err := pathEval.Eval("foo", map[string]interface{}{"foo": 5}) got, err := pathEval.Eval("foo", map[string]any{"foo": 5})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -59,8 +59,7 @@ func TestReMarshalMatcher(t *testing.T) {
var intDst int var intDst int
var uintSrc uint = 2 var uintSrc uint = 2
remarshalFunc := ReMarshalMatcher(&intDst) remarshalFunc := ReMarshalMatcher(&intDst)
err := remarshalFunc(uintSrc) if err := remarshalFunc(uintSrc); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
if intDst != 2 { if intDst != 2 {
@ -71,8 +70,7 @@ func TestReMarshalMatcher(t *testing.T) {
func TestBoolMatcher(t *testing.T) { func TestBoolMatcher(t *testing.T) {
var boolDst bool var boolDst bool
boolFunc := BoolMatcher(&boolDst) boolFunc := BoolMatcher(&boolDst)
err := boolFunc(true) if err := boolFunc(true); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
@ -80,8 +78,7 @@ func TestBoolMatcher(t *testing.T) {
t.Error("BoolMatcher: Expected true got false") t.Error("BoolMatcher: Expected true got false")
} }
err = boolFunc(1) if err := boolFunc(1); err == nil {
if err == nil {
t.Error("BoolMatcher: Expected error, got nil") t.Error("BoolMatcher: Expected error, got nil")
} }
} }
@ -89,8 +86,7 @@ func TestBoolMatcher(t *testing.T) {
func TestStringMatcher(t *testing.T) { func TestStringMatcher(t *testing.T) {
var stringDst string var stringDst string
stringFunc := StringMatcher(&stringDst) stringFunc := StringMatcher(&stringDst)
err := stringFunc("test") if err := stringFunc("test"); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
@ -98,8 +94,7 @@ func TestStringMatcher(t *testing.T) {
t.Errorf("StringMatcher: Expected test, got %v", stringDst) t.Errorf("StringMatcher: Expected test, got %v", stringDst)
} }
err = stringFunc(1) if err := stringFunc(1); err == nil {
if err == nil {
t.Error("StringMatcher: Expected error, got nil") t.Error("StringMatcher: Expected error, got nil")
} }
} }
@ -107,8 +102,7 @@ func TestStringMatcher(t *testing.T) {
func TestStringTreeMatcher(t *testing.T) { func TestStringTreeMatcher(t *testing.T) {
var stringTreeDst []string var stringTreeDst []string
stringTreeFunc := StringTreeMatcher(&stringTreeDst) stringTreeFunc := StringTreeMatcher(&stringTreeDst)
err := stringTreeFunc([]any{"a", "a", "b"}) if err := stringTreeFunc([]any{"a", "a", "b"}); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
@ -117,13 +111,11 @@ func TestStringTreeMatcher(t *testing.T) {
t.Errorf("StringTreeMatcher: Expected %v, got %v", wantAnySlice, stringTreeDst) t.Errorf("StringTreeMatcher: Expected %v, got %v", wantAnySlice, stringTreeDst)
} }
err = stringTreeFunc([]string{"a", "a", "b"}) if err := stringTreeFunc([]string{"a", "a", "b"}); err == nil {
if err == nil {
t.Error("StringTreeMatcher: Expected error, got nil") t.Error("StringTreeMatcher: Expected error, got nil")
} }
err = stringTreeFunc(1) if err := stringTreeFunc(1); err == nil {
if err == nil {
t.Error("StringTreeMatcher: Expected error, got nil") t.Error("StringTreeMatcher: Expected error, got nil")
} }
} }
@ -131,8 +123,7 @@ func TestStringTreeMatcher(t *testing.T) {
func TestTimeMatcher(t *testing.T) { func TestTimeMatcher(t *testing.T) {
var timeDst time.Time var timeDst time.Time
timeFunc := TimeMatcher(&timeDst, time.RFC3339) timeFunc := TimeMatcher(&timeDst, time.RFC3339)
err := timeFunc("2024-03-18T12:57:48.236Z") if err := timeFunc("2024-03-18T12:57:48.236Z"); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
wantTime := time.Date(2024, time.March, 18, 12, 57, 48, 236_000_000, time.UTC) wantTime := time.Date(2024, time.March, 18, 12, 57, 48, 236_000_000, time.UTC)
@ -140,13 +131,11 @@ func TestTimeMatcher(t *testing.T) {
t.Errorf("TimeMatcher: Expected %v, got %v", wantTime, timeDst) t.Errorf("TimeMatcher: Expected %v, got %v", wantTime, timeDst)
} }
err = timeFunc("") if err := timeFunc(""); err == nil {
if err == nil {
t.Error("TimeMatcher: Expected error, got nil") t.Error("TimeMatcher: Expected error, got nil")
} }
err = timeFunc(1) if err := timeFunc(1); err == nil {
if err == nil {
t.Error("TimeMatcher: Expected error, got nil") t.Error("TimeMatcher: Expected error, got nil")
} }
} }
@ -155,8 +144,7 @@ func TestPathEval_Extract(t *testing.T) {
pathEval := NewPathEval() pathEval := NewPathEval()
var result string var result string
matcher := StringMatcher(&result) matcher := StringMatcher(&result)
err := pathEval.Extract("foo", matcher, true, map[string]interface{}{"foo": "bar"}) if err := pathEval.Extract("foo", matcher, true, map[string]any{"foo": "bar"}); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
if result != "bar" { if result != "bar" {
@ -166,13 +154,12 @@ func TestPathEval_Extract(t *testing.T) {
func TestPathEval_Match(t *testing.T) { func TestPathEval_Match(t *testing.T) {
var got string var got string
doc := map[string]interface{}{"foo": "bar"} doc := map[string]any{"foo": "bar"}
pe := NewPathEval() pe := NewPathEval()
pem := PathEvalMatcher{Expr: "foo", Action: StringMatcher(&got)} pem := PathEvalMatcher{Expr: "foo", Action: StringMatcher(&got)}
err := pe.Match([]PathEvalMatcher{pem}, doc) if err := pe.Match([]PathEvalMatcher{pem}, doc); err != nil {
if err != nil {
t.Error(err) t.Error(err)
} }
if got != "bar" { if got != "bar" {
@ -182,7 +169,7 @@ func TestPathEval_Match(t *testing.T) {
func TestPathEval_Strings(t *testing.T) { func TestPathEval_Strings(t *testing.T) {
pe := NewPathEval() pe := NewPathEval()
doc := map[string]interface{}{"foo": "bar"} doc := map[string]any{"foo": "bar"}
want := []string{"bar"} want := []string{"bar"}
got, err := pe.Strings([]string{"foo"}, true, doc) got, err := pe.Strings([]string{"foo"}, true, doc)
@ -196,7 +183,7 @@ func TestPathEval_Strings(t *testing.T) {
} }
func TestAsStrings(t *testing.T) { func TestAsStrings(t *testing.T) {
arg := []interface{}{"foo", "bar"} arg := []any{"foo", "bar"}
want := []string{"foo", "bar"} want := []string{"foo", "bar"}
got, valid := AsStrings(arg) got, valid := AsStrings(arg)